HDU3035-平面图最小割转最短路
来源:互联网 发布:网络学会计推荐 编辑:程序博客网 时间:2024/05/01 15:47
PS:这是get姿势后的第一道建图稍微麻烦的题,居然写完代码没调试一次AC了~~~哈哈~~~~
War
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1386 Accepted Submission(s): 403
Problem Description
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.
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.
Input
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
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
Output
One line for each test case the least amount of TNT needed to disconnect (0, 0) and (N, M).
Sample Input
2 31 9 41 8 76 2 37 5 4 86 2 8 710 4 1 7 5 35 4 10 2 1 96 3 2 9 5 38 9 6 3 10 10
Sample Output
18
Source
2009 Multi-University Training Contest 13 - Host by HIT
题目思路:
s-t平面图最小割转最短路的裸题,具体可以参见我之前写的博客: s-t平面图最小割转最短路算法
这题我们很容易可以得到对偶图的点的个数为n*m*4+2 ,边的个数为n*m*4+n*m*2+n+m-2,
所以如果用普通的spfa的话会超时,因此我们这里要用优先队列优化,这里编号的话我们可以
以0点为S*,n*m*4+1为T*,然后中间每个大格子中的四个小格子编号可以1 2 3 4 这样按顺序排
从上到下,从左到右! 注意下细节,应改建起图还是不难的!
AC代码:
#include<algorithm>#include<iostream>#include<cstring>#include<cstdio>#include<queue>using namespace std;const int maxn = 1e6+100;const int inf = 1e9;struct nod{ int v,w,nex;}edge[maxn<<2];int n,m,nn,e,k;int dis[maxn],hed[maxn];bool vis[maxn];void add(int u,int v,int w){ edge[e].v = v,edge[e].w = w,edge[e].nex = hed[u],hed[u]=e++; edge[e].v = u,edge[e].w = w,edge[e].nex = hed[v],hed[v]=e++;}void dij(){ for(int i=0;i<=nn;i++) dis[i]=inf,vis[i]=0; dis[0]=0; priority_queue<pair<int,int > >q; q.push(make_pair(-dis[0],0)); while(!q.empty()){ int u = q.top().second;q.pop(); if(vis[u])continue;vis[u]=1; for(int i=hed[u];~i;i=edge[i].nex){ int v = edge[i].v; if(dis[v]>dis[u]+edge[i].w){ dis[v]=dis[u]+edge[i].w; if(!vis[v]){ q.push(make_pair(-dis[v],v)); } } } }}int main(){ while(~scanf("%d%d",&n,&m)){ nn = n*m*4+1;e=0; memset(hed,-1,sizeof(hed)); for(int i=1;i<=n+1;i++){ for(int j=1;j<=m;j++){ scanf("%d",&k); if(i==1) //最上面的边 add(nn,(j-1)*4+1,k); else if(i==n+1) //最下面的边 add(0,(n-1)*m*4+(j-1)*4+3,k); else //中间水平的边 add((i-2)*m*4+(j-1)*4+3,(i-1)*m*4+(j-1)*4+1,k); } } for(int i=1;i<=n;i++){ for(int j=1;j<=m+1;j++){ scanf("%d",&k); if(j==1) //最左面的边 add(0,(i-1)*m*4+2,k); else if(j==m+1) //最右面的边 add(nn,i*m*4,k); else //中间垂直的边 add((i-1)*m*4+(j-1)*4,(i-1)*m*4+(j-1)*4+2,k); } } for(int i=0;i<2*n;i++){ for(int j=0;j<2*m;j++){ scanf("%d",&k); //中间斜着的边,这个公式在图上画画应该不难推 add(i/2*m*4+j/2*4+1+(i%2)*2,i/2*m*4+j/2*4+2+(j%2)*2,k); } } dij(); printf("%d\n",dis[nn]); } return 0;}
0 0
- HDU3035-平面图最小割转最短路
- HDOJ3035平面图最小割转最短路
- 平面图最小割转最短路问题
- hdu3035 最小割转换成最短路
- 【BZOJ2007】【Noi2010】海拔 平面图最小割转最短路
- HDU3870-s-t平面图最小割转最短路
- HDU3870 Catch the Theves 题解(平面图最小割转最短路)
- 平面图的最小割转最短路(点非常多)
- 【BZOJ1001】狼抓兔子(BJOI2006)-平面图最小割转最短路
- BZOJ 1001 狼抓兔子 [最小割转最短路=平面图转对偶图]
- hdu3870 S-T平面图最小割转化为最短路
- hdu3870 S-T平面图最小割转化为最短路
- HDU 3035 War 平面图最小割转化为最短路
- 【UVALive】3661 Animal Run 平面图最小割 最短路
- UVALive 3661 Animal Run(平面图最小割,边为节点+最短路)
- BZOJ 2007 NOI 2010 海拔 平面图最小割->最短路SPFA+pq
- [BZOJ2007]NOI2010海拔|最小割|平面图转对偶图|最短路
- UVALive - 3661 Animal Run (平面图+最小割+对偶图+最短路)
- Swift 创建一个MacOS输入法
- 状态模式【State Pattern】
- (3)AngularJS 1.X 之和Input相关的指令
- CMD命令大全
- EF6+MySql code first
- HDU3035-平面图最小割转最短路
- 每周一读-《书都不会读,你还想成功》
- JavaScript字符集编码与解码_2
- Android新布局组件ConstainLayout了解使用
- 【Android】android Fragment的使用
- 蓝桥杯基础训练--区间K大数查询
- Android线程池使用终结版
- 某控件.performClick()实现点击
- Java中的文件和目录管理----Path、Files