Bessie and friends have been captured and are trapped in a secret compound in a location far from their farm, and it is up to Bessie to plan their escape! The compound consists of?NKNK?holding cells arranged in an?N×KN×K?rectangular grid, where there are gates between horizontally and vertically adjacent cells. Each cell houses exactly one cow.
Bessie has hacked into the system, and is able to unlock any subset of the gates, but each gate has a cost. For the cows to escape, Bessie must open enough gates that all the cows can gather in a single cell (so that they have enough cow-power to tunnel to the surface!). Bessie wants to minimize the total unlocking cost.
But the stakes are higher than ever, and Bessie cannot be content with just one escape plan: she needs back-ups. Help her count the number of minimum-cost escape plans; two plans are considered different if some gate needs to be unlocked in one of the plans but not the other.
Since this number may be very large, only output its remainder modulo?109+7109+7.
INPUT FORMAT (file escape.in):
The first line contains two space-separated integers,?NN?and?KK?(2≤N≤30000,2≤K≤62≤N≤30000,2≤K≤6).Each of the next?NN?lines contains?K?1K?1?space-separated integers: the costs of unlocking each gate on a horizontal edge.
Each of the next?KK?lines contains?N?1N?1?space-separated integers: the costs of unlocking each gate on a vertical edge.
All costs are between?11?and?109109?inclusive.
In 20% of the test cases, it is guaranteed that?N≤500N≤500?and all weights are between?11?and?55?inclusive.
In another 20% of the test cases, it is guaranteed that?N≤5000N≤5000.
OUTPUT FORMAT (file escape.out):
A single integer: the number of minimum-cost escape plans, modulo?109+7109+7.
SAMPLE INPUT:
4 3 1 1 5 6 7 8 1 1 1 1 1 2 3 4 1 1 1
SAMPLE OUTPUT:
10
The test case presents a 4x3 grid,
1 1
+-----+-----+
| | |
1 | |2 | 1
| 5 | 6 |
+-----+-----+
| | |
1 | |3 | 1
| 7 | 8 |
+-----+-----+
| | |
1 | |4 | 1
| | |
+-----+-----+
1 1
Any minimum-cost escape plan will use the doorway of cost 2, the doorway of cost 3, and some nine of the doorways of cost 1. There are ten choices for which cost-1 edge to not use, so the answer is 10.
Problem credits: Brian Dean
中文版
Bessie和她的朋友們被抓走并關在了遠離農場的一個秘密房屋,現在該是Bessie站出來策劃脫逃的時候了!這一房屋包含NKNK個排列成N×KN×K矩形方陣的囚室,水平和垂直方向相鄰的囚室之間有門互通。每個囚室中有一頭奶牛。
Bessie黑進了系統,可以解鎖任意一部分的門,但是每個門均有其代價。為了使奶牛們能夠脫逃,Bessie必須打開足夠多的門使得所有的奶牛可以聚集在一個房間內(這樣她們就能擁有足夠的力量挖地洞通向外面!)。Bessie想要使得總的解鎖花費最小。
但是這次行動異常關鍵,Bessie不能滿足于僅僅一個脫逃方案:她還需要后備方案。幫助她計算最小代價脫逃方案的數量;如果某一扇門在一個方案中被解鎖了而在另一個方案中沒有,那么這兩個方案就被認為是不同的。
由于這個數字可能非常大,只需輸出該數對109+7109+7取模的余數。
輸入格式(文件名:escape.in):
輸入的第一行包含兩個空格分隔的整數NN和KK(2≤N≤30000,2≤K≤62≤N≤30000,2≤K≤6)。以下NN行每行包含K?1K?1個空格分隔的整數,為解鎖一條水平邊上的每扇門需要花費的代價。
以下KK行每行包含N?1N?1個空格分隔的整數,為解鎖一條垂直邊上的每扇門需要花費的代價。
所有的花費均在11到109109之間。
對于20%的測試數據,保證N≤500N≤500,所有的代價均在11到55之間。
對于另外20%的測試數據,保證N≤5000N≤5000。
輸出格式(文件名:escape.out):
一個整數:最小花費的逃脫方案的數量,對109+7109+7取模。
輸入樣例:
4 3 1 1 5 6 7 8 1 1 1 1 1 2 3 4 1 1 1
輸出樣例:
10
這個測試樣例描述了一個4x3的方陣,
1 1
+-----+-----+
| | |
1 | |2 | 1
| 5 | 6 |
+-----+-----+
| | |
1 | |3 | 1
| 7 | 8 |
+-----+-----+
| | |
1 | |4 | 1
| | |
+-----+-----+
1 1
所有的最小代價脫逃方案都會使用代價為2的門,代價為3的門,以及某九個代價為1的門。由于有十種方式選擇不被使用的代價為1的門,所以答案為10。
供題:Brian Dean

? 2025. All Rights Reserved. 滬ICP備2023009024號-1