您的位置:首页 > 其它

hdu 3035 War(平面图最小割)

2014-03-18 00:07 316 查看

War

Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 1128 Accepted Submission(s): 301



[align=left]Problem Description[/align]
Country X is under attack by enemies. Now the army of enemy has arrived at City Y. City Y consists of an N×M grid. All the paths in the grid are bidirectional, horizontal or vertical or diagonal. The upper-left corner is (0, 0) and
lower-right corner is (N, M). The army enters at (0, 0) and they must get to (N, M) in order to continue their attack to the capital of Country X. The figure below shows what does City Y looks like.



Every blackened node represents a vertex. The number beside each edge is the amount of TNT needed to destroy that road. The army of Country X is unable to beat the enemy now. The only thing they can do is to prevent them from heading to their capital so that
they can have more time to prepare for striking back. Of course they want to use the least amount of TNT to disconnect (0, 0) and (N, M). You are a talented programmer, please help them decide the least amount needed.

[align=left]Input[/align]
There are multiple test cases.

The first line of each test case contains two positive integers N and M, representing height and width of the grid.

Then N+1 lines each containing M integers, giving you the amount needed of horizontal roads in row major order.

Then N lines each containing M+1 integers, giving you the amount needed of vertical roads in row major order.

Then 2N lines each containing 2M integers, giving you the amount needed of diagonal roads in row major order.

There is a blank line after each input block. The sample input is corresponding to the figure above.

Restriction:

1 <= N, M <= 500

1 <= amount <= 1,000,000

[align=left]Output[/align]
One line for each test case the least amount of TNT needed to disconnect (0, 0) and (N, M).

[align=left]Sample Input[/align]

2 3
1 9 4
1 8 7
6 2 3
7 5 4 8
6 2 8 7
10 4 1 7 5 3
5 4 10 2 1 9
6 3 2 9 5 3
8 9 6 3 10 10


[align=left]Sample Output[/align]

18
求无向图的最小割,转化为求最短路径,参考点击打开链接,要建平面图的对偶图,建图十分麻烦。。。
AC代码:[code]#include <iostream>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <queue>
#include <stack>
#include <ctime>
#include <vector>
#include <algorithm>
#define ll long long
#define L(rt) (rt<<1)
#define R(rt)  (rt<<1|1)
#define eps 1e-6;
using namespace std;

const int INF = 1e9;
const int maxn = 1000005;

struct Edge{
int v, w, next;
}et[maxn * 3];
int n, m, s, t, num;
int dis[maxn], eh[maxn];
bool vis[maxn];
typedef pair<int,int> pii;
void init(){
memset(eh, -1, sizeof(eh));
num = 0;
}
void add(int u, int v, int w){
Edge e = {v, w, eh[u]};
et[num] = e;
eh[u] = num++;
}
int dij(){
priority_queue<pii, vector<pii>, greater<pii> >Q;
memset(vis, false, sizeof(vis));
for(int i = 0; i <= t; i++) dis[i] = INF;
dis[s] = 0;
Q.push(pii(dis[s], s));
while(!Q.empty())
{
pii x = Q.top();
Q.pop();
int u = x.second;
if(vis[u]) continue;
vis[u] = true;
for(int i = eh[u]; i != -1; i = et[i].next)
{
int v = et[i].v, w = et[i].w;
if(dis[v] > dis[u] + w)
{
dis[v] = dis[u] + w;
Q.push(pii(dis[v], v));
}
}
}
return dis[t];
}
int main()
{
int u, v, w;
while(~scanf("%d%d", &n, &m))
{
init();
s = 0;
t = 4 * n * m + 1;
for(int i = 1; i <= m; i++)
{
scanf("%d", &w);
add(s, i, w);
add(i, s, w);
}
for(int i = 1; i < n; i++)
for(int j = 1; j <= m; j++)
{
scanf("%d", &w);
u = i * 2 * m + j;
v = u - m;
add(u, v, w);
add(v, u, w);
}
for(int i = 1; i <= m; i++)
{
scanf("%d", &w);
u = 2 * n * m - m + i;
add(u, t, w);
add(t, u, w);
}
int cnt = n * m * 2;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m + 1; j++)
{
scanf("%d", &w);
if(j == 1)
{
u = cnt + (i - 1) * 2 * m + j;
add(u, t, w);
add(t, u, w);
}
else if(j == m + 1)
{
u = cnt + (i - 1) * 2 * m + 2 * m;
add(u, s, w);
add(s, u, w);
}
else
{
u = cnt + (i - 1) * 2 * m + (j - 1) * 2;
v = u + 1;
add(u, v, w);
add(v, u, w);
}
}
for(int i = 1; i <= 2 * n; i++)
for(int j = 1; j <= 2 * m; j++)
{
scanf("%d", &w);
u = (i - 1) * m + (j + 1) / 2;
v = cnt + j + ((i + 1) / 2 - 1) * 2 * m;
add(u, v, w);
add(v, u, w);
}
printf("%d\n", dij());
}
return 0;
}


[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: