HDU 3870Catch the Theves
来源:互联网 发布:linux jdk版本降级 编辑:程序博客网 时间:2024/05/23 11:45
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3870
题意:平面图求最小割,转化成求最短路
#include<cstdio>#include<iostream>#include<cstring>#include<queue>#include<map>#include<string>using namespace std;const int N=410*410;const int M=410*410*4+10;const int INF=INT_MAX;struct Edge{ int to,next,dis; Edge(){} Edge(int To,int Next,int Dis) { to=To,next=Next ,dis=Dis; }}e[M];int head[N];struct Spfa{ int total; int low[N]; int vis[N]; void init() { total=0; memset(head,-1,sizeof(head)); } void add_edges(int from,int to,int dis) { e[total]=Edge(to,head[from],dis); head[from]=total++; } int _spfa(int s,int t) { memset(low,-1,sizeof(low)); low[s]=0; memset(vis,0,sizeof(vis)); vis[s]=1; queue<int> qq; qq.push(s); while(!qq.empty()) { int u=qq.front();qq.pop(); vis[u]=0; for(int i=head[u];i!=-1;i=e[i].next) { int v=e[i].to; if(low[v]==-1||(low[v]>low[u]+e[i].dis)) { low[v]=low[u]+e[i].dis; if(!vis[v]) { vis[v]=1; qq.push(v); } } } } return low[t]; }}spfa;int aa[410][410];int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};int main(){ int T;scanf("%d",&T); while(T--) { int n;scanf("%d",&n); for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { scanf("%d",&aa[i][j]); } } int s=0,t=(n-1)*(n-1)+1; spfa.init(); for(int i=0;i<n-1;i++) { for(int j=0;j<n-1;j++) { int ans=i*(n-1)+j+1; for(int k=0;k<4;k++) { int x=i+dir[k][0]; int y=j+dir[k][1]; if(x>=0&&x<n-1&&y>=0&&y<n-1) { int bns=x*(n-1)+y+1; if(x<i)x=i; if(y<j)y=j; spfa.add_edges(ans,bns,aa[x][y]); //printf("ans=%d,bns=%d,aa=%d\n",ans,bns,aa[x][y]); } else if(x<0||y>=n-1) { if(x<i)x=i; if(y<j)y=j; spfa.add_edges(s,ans,aa[x][y]); //printf("s=%d,ans=%d,aa=%d\n",s,ans,aa[x][y]); } else { if(x<i)x=i; if(y<j)y=j; spfa.add_edges(ans,t,aa[x][y]); //printf("ans=%d,t=%d,aa=%d\n",ans,t,aa[x][y]); } } } } printf("%d\n",spfa._spfa(s,t)); } return 0;}
0 0
- HDU 3870 Catch the Theves
- HDU 3870Catch the Theves
- HDU-3870-Catch the Theves
- hdu 3870 Catch the Theves(平面最小割)
- 平面图s-t最小割 HDU 3870 Catch the Theves
- hdu 3870 Catch the Theves 平面图转对偶图
- HDU 3870 Catch the Theves 最短路求最小割
- 【最短路】 HDOJ 3870 Catch the Theves
- HDU 3870 Catch the Theves 2011 Multi-University Training Contest 4 - Host by SDU
- 【HDU】3870 Catch the Theves 对偶图全局最小割 最短路
- HDU - 3870 Catch the Theves(最小割+对偶图==最短路)
- hdu 3870 Catch the Theves(对偶图,最短路,最小割)
- HDU 3870 Catch the Theves(对偶图的最小割转最短路)
- HDU3870 Catch the Theves 题解(平面图最小割转最短路)
- hdu catch the thief
- 重做catch the cow
- Catch The Cow
- catch the cow (P3278)
- android 彻底退出应用程序在不同API下需要调用的方法
- !POJ 2488 dfs--按顺序搜索
- 代码重构
- 10000 pcs free gift of chinese top grade brown film faced plywood
- 盗梦空间
- HDU 3870Catch the Theves
- Contiki与IoT
- Linux内核源代码情景分析-强制性调度
- C语言——字符串查找字符的两个函数
- IOS UI_音频AVAudioPlayer
- TS包结构
- [hihoCoder]#1062 : 最近公共祖先·一
- 1输出x^2的值
- 关于多线程常见问题