HDU_3376_Matrix Again(最小费用流)
2015-09-22 18:59
525 查看
Matrix Again
[b]Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)Total Submission(s): 3509 Accepted Submission(s): 1032
[/b]
Problem Description
Starvae very like play a number game in the n*n Matrix. A positive integer number is put in each area of the Matrix.
Every time starvae should to do is that choose a detour which from the top left point to the bottom right point and than back to the top left point with the maximal values of sum integers that area of Matrix starvae choose. But from the top to the bottom can
only choose right and down, from the bottom to the top can only choose left and up. And starvae can not pass the same area of the Matrix except the start and end..
Do you know why call this problem as “Matrix Again”? AS it is like the problem 2686 of HDU.
Input
The input contains multiple test cases.
Each case first line given the integer n (2<=n<=600)
Then n lines, each line include n positive integers. (<100)
Output
For each test case output the maximal values starvae can get.
Sample Input
2 10 3 5 10 3 10 3 3 2 5 3 6 7 10 5 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 4 5 6 7 8 5 6 7 8 9
Sample Output
28 46 80
题意:从矩阵起点(1,1) -> (n,n) -> (1,1) 经过点的权值和最大是多少。每个点只能走一次,从(1,1) -> (n,n)只能往右或往下走,从(n,n) -> (1,1)只能往左或往上走。
分析:跟HDU_2686题意一模一样,只不过点数增加了很多,所有用vector会超时,转为结构体数组就行了,然而C++ TLE了,G++ AC。不知为何?
建图:首先要想到拆点。加入超级源点s(0),s连向1,容量为2,费用为0;加入超级汇点t(N*N*2+1),点N*N*2连向t,容量为2,费用为0;对于一个点u(I,j),如果u为(1,1)或(N,N),那么连边 u -> u+N*N,容量为2,费用为-m[i][j],否则连边 u -> u+N*N,容量为1,费用为-m[I][j];然后对于其右边点以及下边点v,连边 u+N*N -> v,容量为1,费用为0。然后跑一遍最小费用 最大流即可。注意得到的mincost里包含了两个点(1,1),(N,N),所以结果为
-mincost - m[1][1] - m
。
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3376
代码清单:
#include<map> #include<set> #include<cmath> #include<queue> #include<stack> #include<ctime> #include<cctype> #include<string> #include<cstdio> #include<cstring> #include<cstdlib> #include<iostream> #include<algorithm> using namespace std; #define end() return 0 typedef long long ll; typedef unsigned int uint; typedef unsigned long long ull; const int maxn = 1000000 + 5; const int INF = 0x7f7f7f7f; struct Edge{ int from,to,cap,flow,cost,next; Edge(){} Edge(int u,int v,int c,int f,int w,int n):from(u),to(v),cap(c),flow(f),cost(w),next(n){} }; struct MCMF{ int n,m,cnt; Edge edge[4*maxn]; int head[maxn]; int inq[maxn]; //是否在队列 int d[maxn]; //Bellman-Ford int p[maxn]; //上一条弧 int a[maxn]; //可改进量 void init(int n){ this -> n = n; memset(head,-1,sizeof(head)); cnt=0; } void addEdge(int from,int to,int cap,int cost){ edge[cnt]=Edge(from,to,cap,0,cost,head[from]); head[from]=cnt++; edge[cnt]=Edge(to,from,0,0,-cost,head[to]); head[to]=cnt++; } bool BellmanFord(int s,int t,int& flow,int& cost){ memset(d,INF,sizeof(d)); memset(inq,0,sizeof(inq)); //memset(p,-1,sizeof(p)); d[s]=0; inq[s]=1; p[s]=0; a[s]=INF; queue<int>q; q.push(s); while(!q.empty()){ int u=q.front();q.pop(); inq[u]=0; for(int i=head[u];i!=-1;i=edge[i].next){ Edge& e=edge[i]; if(e.cap>e.flow&&d[e.to]>d[u]+e.cost){ d[e.to]=d[u]+e.cost; p[e.to]=i; a[e.to]=min(a[u],e.cap-e.flow); if(!inq[e.to]){ q.push(e.to); inq[e.to]=1; } } } } if(d[t]==INF) return false; flow+=a[t]; cost+=d[t]*a[t]; //if(flow==2) return false; for(int u=t;u!=s;u=edge[p[u]].from){ edge[p[u]].flow+=a[t]; edge[p[u]^1].flow-=a[t]; } return true; } //需要保证初始网络中没有负权圈 int MincostMaxflow(int s,int t){ int flow=0,cost=0; while(BellmanFord(s,t,flow,cost)); return cost; } }; int N,tail; int m[605][605]; MCMF mcmf; void input(){ for(int i=1;i<=N;i++){ for(int j=1;j<=N;j++){ scanf("%d",&m[i][j]); } } } void createGraph(){ tail=N*N*2+1; mcmf.init(tail+1); mcmf.addEdge(0,1,2,0); mcmf.addEdge(N*N*2,tail,2,0); for(int i=1;i<=N;i++){ for(int j=1;j<=N;j++){ int u=(i-1)*N+j; int v1=(i-1)*N+j+1; //(i,j+1) int v2=i*N+j; //(i+1,j) if(i==1&&j==1){ mcmf.addEdge(u,u+N*N,2,-m[i][j]); } else if(i==N&&j==N){ mcmf.addEdge(u,u+N*N,2,-m[i][j]); } else{ mcmf.addEdge(u,u+N*N,1,-m[i][j]); } if(j+1<=N) mcmf.addEdge(u+N*N,v1,1,0); if(i+1<=N) mcmf.addEdge(u+N*N,v2,1,0); } } } void solve(){ createGraph(); printf("%d\n",-m[1][1]-m -mcmf.MincostMaxflow(0,tail)); } int main(){ while(scanf("%d",&N)!=EOF){ input(); solve(); }end(); }
相关文章推荐
- 03-树3 Tree Traversals Again
- NSSearchPathForDirectoriesInDomains用法
- _exit和exit的区别 http://www.cnblogs.com/hnrainll/archive/2011/08/17/2142001.html
- wait和waitpid函数
- 微博情感分析的表情符号平滑语言模型(A11, AAAI2012)
- Failed to load resource: net::ERR_INCOMPLETE_CHUNKED_ENCODING
- rhel6安装aircrack-ng
- Unity 3D 使用TerrainCompose 调用RTP 报错:
- weglogic Error 503--Service Unavailable
- [置顶] linux中fork()函数详解 ) http://blog.csdn.net/jason314/article/details/5640969
- 解决svn Authorization failed错误
- Container With Most Water
- ORA-00054: 资源正忙, 但指定以 NOWAIT 方式获取资源, 或者超时失效
- 杭电1023Train Problem II
- exit和return的区别 http://blog.csdn.net/firefly_2002/article/details/7960595
- STM32用JLINK 烧写程序时出现NO Cortex-m device found in JTAG chain现象和解决方案
- 没有main函数生成可执行程序的几种方法 http://www.linuxidc.com/Linux/2013-09/90061.htm
- 升级Xcode7&iOS9后,出现NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -980)
- 磁盘RAID总结
- 文件显示命令:cat、more、less、tail、touch详解