hdu 2686 Matrix(费用流,拆点)
来源:互联网 发布:手机怎么在淘宝上购物 编辑:程序博客网 时间:2024/05/21 18:30
Matrix
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2355 Accepted Submission(s): 1243
Problem Description
Yifenfei 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 yifenfei should to do is that choose a detour which frome 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 yifenfei 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 yifenfei can not pass the same area of the Matrix except the start and end.
Every time yifenfei should to do is that choose a detour which frome 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 yifenfei 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 yifenfei can not pass the same area of the Matrix except the start and end.
Input
The input contains multiple test cases.
Each case first line given the integer n (2<n<30)
Than n lines,each line include n positive integers.(<100)
Each case first line given the integer n (2<n<30)
Than n lines,each line include n positive integers.(<100)
Output
For each test case output the maximal values yifenfei 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
yifenfei
思路:很明显的费用流模型,其实就相当于(1,1)出发走到(n,n)走两次求最大权值。不过因为这个每个点只能走一次,所以我们把每个点都拆成两个点,中间连一条容量为1的边即可,注意数组开大点,30*30*2
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <queue>using namespace std;#define N 2010#define INF 0x3f3f3f3fstruct Edge{ int from,to,next,cap,cost;///起点,终点,同起点下一条边,残余流量,费用} edge[N*N];int cnt,head[N];int vis[N],d[N],pp[N];int dir[2][2]={1,0,0,1};int ma[55][55];int sumflow;///最大流量总和void init(){ cnt=0; memset(head,-1,sizeof(head));}void addedge(int from,int to,int cap,int cost){ edge[cnt].from=from; edge[cnt].to=to; edge[cnt].cost=cost; edge[cnt].cap=cap; edge[cnt].next=head[from]; head[from]=cnt++; edge[cnt].from=to; edge[cnt].to=from; edge[cnt].cost=-cost; edge[cnt].cap=0; edge[cnt].next=head[to]; head[to]=cnt++;///存反向边}int spfa(int s,int t,int n){ queue<int>q; memset(vis,0,sizeof(vis)); memset(pp,-1,sizeof(pp));///pp[i]表示最短路径上以i为终点的边的编号 for(int i=0; i<=n; i++) d[i]=-INF; d[s]=0; vis[s]=1; q.push(s); while(!q.empty()) { int u=q.front(); q.pop(); vis[u]=0; for(int i=head[u]; i!=-1; i=edge[i].next) { int v=edge[i].to; if(edge[i].cap>0&&d[v]<d[u]+edge[i].cost) { d[v]=d[u]+edge[i].cost; pp[v]=i; if(!vis[v]) { vis[v]=1; q.push(v); } } } } if(d[t]==-INF) return 0;///找不到一条到终点的路 return 1;}int MCMF(int s,int t,int n){ int mincost=0,minflow,flow=0;///最小费用,路径中最小流量,总流量 while(spfa(s,t,n))///找当前的最短路 { minflow=INF+1; for(int i=pp[t]; i!=-1; i=pp[edge[i].from]) minflow=min(minflow,edge[i].cap);///从路径中找最小的流量 flow+=minflow;///总流量加上最小流量 for(int i=pp[t]; i!=-1; i=pp[edge[i].from]) { edge[i].cap-=minflow;///当前边减去最小流量 edge[i^1].cap+=minflow;///反向边加上最小流量 } mincost+=d[t]*minflow;///最小费用等于路径和*每条路径的流量(经过多少次) } sumflow=flow; return mincost;}int main(){ int n; while(~scanf("%d",&n)) { init(); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) scanf("%d",&ma[i][j]); int s=1,t=2*n*n; for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { if((i==1&&j==1)||(i==n&&j==n)) addedge((i-1)*n+j,(i-1)*n+j+n*n,2,ma[i][j]); else addedge((i-1)*n+j,(i-1)*n+j+n*n,1,ma[i][j]); for(int k=0;k<2;k++) { int x=i+dir[k][0],y=j+dir[k][1]; if(x<1||x>n||y<1||y>n) continue; addedge((i-1)*n+j+n*n,(x-1)*n+y,1,0); } } } printf("%d\n",MCMF(s,t,t)-ma[1][1]-ma[n][n]); } return 0;}
0 0
- hdu 2686 Matrix(费用流,拆点)
- HDU 2686 Matrix||HDU 3376 Matrix Again (拆点费用流)
- HDU 2686 Matrix(最大费用最大流+拆点)
- hdu 2686 Matrix(最大费用流)
- HDU 2686 Matrix | 费用流
- HDU 2686 Matrix(费用流)
- HDU 2686 Matrix 费用流
- hdu 2686 Matrix【费用流】
- HDU 2686 Matrix(费用流)
- HDU 2686 Matrix 3376 Matrix Again(费用流)
- hdu 3376 Matrix Again(费用流,拆点,手动扩栈)
- HDU 2686 费用流 拆点
- hdu 2686 Matrix hdu 3376 Matrix Again 费用流
- HDU 2686 Matrix HDU 3376 Matrix Again 费用流
- HDU 2686 Matrix && HDU 3376 Matrix Again(最大费用)
- hdu 2686 Matrix(最大费用流||多线程DP)@
- hdu 2686 Matrix 最小费用最大流
- hdu 2686 Matrix 最大费用最大流
- 《C++Primer》读书笔记——第1章 开始
- webpack 插件: html-webpack-plugin
- 基于STM32 HAL库的flash emulation eeprom
- matlab常用命令整理
- 挨踢生涯文档集
- hdu 2686 Matrix(费用流,拆点)
- 窗体上传文件方式
- 贪心 POJ 2393
- 滑动返回结束当前Activity
- hdu 1251 统计难题 (字典树)
- Label 行间距--swift版
- objective-c简单例子
- c语言函数的可变参数列表
- matlab基础笔记(一)matlab中求和函数sum