HDU 3376 Matrix Again 费用流(不MLE的最小费用最大流模板)
来源:互联网 发布:七台河淘宝客服 编辑:程序博客网 时间:2024/05/10 02:14
点击打开链接
Matrix Again
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)
Total Submission(s): 2413 Accepted Submission(s): 734
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
Author
Starvae
Source
HDOJ Monthly Contest – 2010.04.04
Recommend
lcy
题意:从n*n矩阵的最左上角的点走到最右下角的点,只能往右或者往下走,然后从最右下角的点走到最左上角的点,只能往上或者往左走。求走过的路最大的数。
此题属于费用流,而难在建图上,建好图之后直接套一个好的模板,就会AC。那么如何建图呢?将矩阵中的每个点都拆成 i 和 n*n+i 。费用是该点的值,容量是1,就能控制走一次。起点和终点的容量是2.源点和起点相连容量是2,费用是0.汇点和终点相连,容量是2,费用是0.
#include<iostream>#include<algorithm>#include<cstring>#include<queue>#include<cstdio>using namespace std;const int MAXN=610*610*2+2;const int inf=1<<29;int pre[MAXN]; // pre[v] = k:在增广路上,到达点v的边的编号为kint dis[MAXN]; // dis[u] = d:从起点s到点u的路径长为dint vis[MAXN]; // inq[u]:点u是否在队列中int path[MAXN];int head[MAXN];int NE,tot,ans,max_flow,map[666][666];struct node{ int u,v,cap,cost,next;} Edge[MAXN<<2];void addEdge(int u,int v,int cap,int cost){ Edge[NE].u=u; Edge[NE].v=v; Edge[NE].cap=cap; Edge[NE].cost=cost; Edge[NE].next=head[u]; head[u]=NE++; Edge[NE].v=u; Edge[NE].u=v; Edge[NE].cap=0; Edge[NE].cost=-cost; Edge[NE].next=head[v]; head[v]=NE++;}int SPFA(int s,int t) // 源点为0,汇点为sink。{ int i; for(i=s;i<=t;i++) dis[i]=inf; memset(vis,0,sizeof(vis)); memset(pre,-1,sizeof(pre)); dis[s] = 0; queue<int>q; q.push(s); vis[s] =1; while(!q.empty()) // 这里最好用队列,有广搜的意思,堆栈像深搜。 { int u =q.front(); q.pop(); for(i=head[u]; i!=-1;i=Edge[i].next) { int v=Edge[i].v; if(Edge[i].cap >0&& dis[v]>dis[u]+Edge[i].cost) { dis[v] = dis[u] + Edge[i].cost; pre[v] = u; path[v]=i; if(!vis[v]) { vis[v] =1; q.push(v); } } } vis[u] =0; } if(pre[t]==-1) return 0; return 1;}void end(int s,int t){ int u, sum = inf; for(u=t; u!=s; u=pre[u]) { sum = min(sum,Edge[path[u]].cap); } max_flow+=sum; //记录最大流 for(u = t; u != s; u=pre[u]) { Edge[path[u]].cap -= sum; Edge[path[u]^1].cap += sum; ans += sum*Edge[path[u]].cost; // cost记录的为单位流量费用,必须得乘以流量。 }}int main(){ int i,j,n,s,t; while(scanf("%d",&n)!=EOF) { memset(head,-1,sizeof(head)); NE=ans=max_flow=s=0; for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { scanf("%d",&map[i][j]); } } int k=n*n; t=2*k+1; for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { addEdge(j+(i-1)*n,k+j+(i-1)*n,1,-map[i][j]); if(j!=n) addEdge(k+j+(i-1)*n,j+1+(i-1)*n,inf,0); if(i!=n) addEdge(k+j+(i-1)*n,i*n+j,inf,0); } } addEdge(s,1,2,0); addEdge(1,k+1,1,0); addEdge(2*k,t,2,0); addEdge(k,2*k,1,0); while(SPFA(s,t)) { end(s,t); } printf("%d\n",-ans); } return 0;}
- HDU 3376 Matrix Again 费用流(不MLE的最小费用最大流模板)
- HDU 3376 Matrix Again (最小费用最大流)
- hdu 3376 Matrix Again【最大费用流】
- hdu 3376 Matrix Again 最大费用流
- hdu 3376 Matrix Again(最小费用流)
- hdu 2686 Matrix / 3376 Matrix Again最大费用流
- HDU 3376 Matrix Again(费用流)
- hdoj 3376,2686 Matrix Again 【最小费用最大流】
- Matrix Again HDU 3376 最大费用最大流
- hdu 2686/hdu 3376 Matrix Again 费用流模板
- HDU 2686 Matrix && HDU 3376 Matrix Again(最大费用)
- hdu 2686 Matrix 最小费用最大流
- HDU 2686 Matrix 最小费用最大流
- HDU 2686 Matrix 3376 Matrix Again(费用流)
- HDU 3376 Matrix Again 费用流
- HDU 3376 Matrix Again(费用流)
- HDU 3376 Matrix Again(最大费用最大流)HDU2686加强题
- HDU 3488 最小费用最大流模板
- (4)散列函数设计:除留余数法
- 区分eclipse中两种jre
- 如何查看出错产生的core
- PHP和MySQL存储过程的实例演示
- C# WinForm使用log4net日志
- HDU 3376 Matrix Again 费用流(不MLE的最小费用最大流模板)
- ARM的协处理器操作--mrc和mcr
- 异或符号'^'在C/C++编程中的应用
- C++新手入门导视
- 学生管理系统问题集锦四
- 大整数相乘的问题
- DevExpress ChartControl SeriesLabel背景透明
- 在sturts2中ActionContext和ServletActionContext的区别
- java中的内部类