【HDU 3376 Matrix Again】网络流 & 拆点 & 最大费用最大流
来源:互联网 发布:sql注入测试工具有哪些 编辑:程序博客网 时间:2024/05/22 16:45
Matrix Again
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)
Total Submission(s): 4444 Accepted Submission(s): 1292
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
Author
Starvae
Source
HDOJ Monthly Contest – 2010.04.04
题意 : 从 (1,1) 到 (n,n)只能往右或往下走,然后再从(n,n)走到(1,1),不能重复走,走过的格子的数字最大和是多少
思路 : 从 (1,1) 到 (n,n)再从(n,n)到(1,1),相当于从(1,1)到(n,n)找到两条不重复的路,把每个格子拆分成左右两个点,一个入点,一个出点
源点s -> (1,1)入点 流为 2 -> (1,1)出点 流为 2 -> (1,2)入点流为 1 -> (1,2)出点 流为 1 -> …….-> (n,n)入点 -> (n,n)出点 流为 2 -> 汇点 e 流为 2
AC代码:
#include<cstdio>#include<cmath>#include<deque>#include<queue>#include<vector>#include<cstring>#include<algorithm>using namespace std;const int MAX = 8e5 + 10;const int INF = (1e9 + 7) / 2;typedef long long LL;int head[MAX],nl;struct node{ int from,to,cost,flow,next;}st[MAX * 5];void add(int x,int y,int z,int a){ st[nl] = node{x,y,z,a,head[x]},head[x] = nl++; st[nl] = node{y,x,-z,0,head[y]},head[y] = nl++;}void init(){ nl = 0; memset(head,-1,sizeof head);}int vis[MAX],p[MAX],w[MAX];bool DFS(int s,int t){ memset(vis,0,sizeof vis); memset(w,-1,sizeof w); fill(p,p + MAX,-INF); p[s] = 0,vis[s] = 1; queue<int> q; q.push(s); while(!q.empty()){ int o = q.front(); q.pop(); vis[o] = 0; for(int i = head[o]; i != -1; i = st[i].next){ int a = st[i].to; if(p[a] < p[o] + st[i].cost && st[i].flow){ p[a] = p[o] + st[i].cost; w[a] = i; if(!vis[a]){ vis[a] = 1; q.push(a); } } } } return w[t] != -1;}int Mincost(int s,int t){ int Maxflow = 0,cost = 0; while(DFS(s,t)){ int flow = INF; for(int i = w[t]; i != -1; i = w[st[i].from]){ if(flow > st[i].flow) flow = st[i].flow; } for(int i = w[t]; i != -1; i = w[st[i].from]){ st[i].flow -= flow; st[i ^ 1].flow += flow; cost += flow * st[i].cost; } Maxflow += flow; } return cost;}int z[666][666],n;int main(){ while(~scanf("%d",&n)){ init(); int t = n * n,s = 0,e = n * n * 2 + 1; add(s,1,0,2); add(t * 2,e,0,2); for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++){ scanf("%d",&z[i][j]); int a = (i - 1) * n + j; if(i == j && (i == 1 || i == n)) add(a,a + t,z[i][j],2); else add(a,a + t,z[i][j],1); if(i + 1 <= n) add(a + t,a + n,0,1); if(j + 1 <= n) add(a + t,a + 1,0,1); } printf("%d\n",Mincost(s,e) - z[n][n] - z[1][1]); } return 0;}
- 【HDU 3376 Matrix Again】网络流 & 拆点 & 最大费用最大流
- hdu 3376 Matrix Again【最大费用流】
- hdu 3376 Matrix Again 最大费用流
- Matrix Again HDU 3376 最大费用最大流
- hdu 2686 Matrix / 3376 Matrix Again最大费用流
- HDU 3376 Matrix Again (最小费用最大流)
- HDU 2686 Matrix(最大费用最大流+拆点)
- HDU 3376 Matrix Again(最大费用最大流)HDU2686加强题
- HDU 3376--Matrix Again【最大费用最大流 && 经典建图】
- HDU 3376 Matrix Again 费用流(不MLE的最小费用最大流模板)
- HDU 2686 Matrix && HDU 3376 Matrix Again(最大费用)
- hdoj 3376,2686 Matrix Again 【最小费用最大流】
- hdu 2686 Matrix 最大费用最大流
- Matrix (hdu 2686 最大费用最大流)
- HDU3376:Matrix Again(最大费用最大流)
- HDU 2686 Matrix||HDU 3376 Matrix Again (拆点费用流)
- hdoj 3376 Matrix Again and hdoj 2686 Matrix 【最大费用最大流】
- hdu 2686 Matrix 最小费用最大流
- Lintcode139 Subarray Sum Closest solution 题解
- 华为机考笔试题-删数
- 鸡兔同笼问题
- 收集游戏制作的图片
- React Native之PanResponder
- 【HDU 3376 Matrix Again】网络流 & 拆点 & 最大费用最大流
- Linux中vi命令与vim命令的区别
- Chm 文件资源编辑软件
- 华为机考笔试题-字符集合
- Oracle sqlplus: command not found
- 【Unity3D】GIF与序列帧动画的使用
- tomcat集群
- Linux 文件内容查询 —— cat less more touch
- 信息熵——Information Entropy