HDU_3376_Matrix Again(最小费用流)
来源:互联网 发布:java round half up 编辑:程序博客网 时间:2024/05/01 08:27
Matrix Again
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)
Total Submission(s): 3509 Accepted Submission(s): 1032
Total Submission(s): 3509 Accepted Submission(s): 1032
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.
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)
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
210 35 10310 3 32 5 36 7 1051 2 3 4 52 3 4 5 63 4 5 6 74 5 6 7 85 6 7 8 9
Sample Output
284680
题意:从矩阵起点(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[N][N]。
题目链接: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 0typedef 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[N][N]-mcmf.MincostMaxflow(0,tail));}int main(){ while(scanf("%d",&N)!=EOF){ input(); solve(); }end();}
0 0
- HDU_3376_Matrix Again(最小费用流)
- hdoj 3376,2686 Matrix Again 【最小费用最大流】
- HDU 3376 Matrix Again (最小费用最大流)
- hdu 3376 Matrix Again(最小费用流)
- HDU 3376 Matrix Again 费用流(不MLE的最小费用最大流模板)
- 费用流 hdu3376 Matrix Again
- Matrix Again hdu3376 费用流
- HDOJ 2686 - Matrix & HDOJ 3376 - Matrix Again 构图最小费用最大流 or dp
- 最小费用最小流uva10746
- 最小费用最大流
- 最小费用最大流
- 最小费用最大流
- 最小费用最大流
- 最小费用流模版
- 【最小费用最大流】
- 最小费用流
- 最小费用最大流
- 最小费用最大流
- Texlive 更新命令
- unity3d 学习笔记(七) 类的基本函数学习
- 百度云下载限速如何提速
- Ubuntu-Python2.7安装 scipy,numpy,matplotlib
- hdu4418 Time travel(高斯消元法+概率)
- HDU_3376_Matrix Again(最小费用流)
- jqMobi指南系列教程是《 jqMobi Javascript Frameworks Cheat Sheet》 的中文版现在改名为app framework
- segmentedControl与swipe联合控制页面
- QT中QTimer的使用方法介绍
- UVA 10870 Recurrences
- atexit http://www.cnblogs.com/wanghetao/archive/2012/04/03/2431256.html
- Linux下的常用shell命令
- hadoop 自学指南八之Hive(2)
- 并发和并行