Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=2686
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 does 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 this area of the 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. Test Instructions Description: N*n matrix, each number is a positive integer, requires from the leftmost position to go to the bottom right position, each time can only go to the right or down a grid, and then from the bottom right of the position back to the leftmost position, each time can only go up or to the left a grid, and then accumulate two times the number of paths. Note that each lattice can only go one time (except for the leftmost and most right), i.e. each number in the matrix can only be added once. Algorithm Analysis: This problem can use DP to do, but my DP thinking is limited, is not familiar with, just recently brush the topic, introduce the minimum cost maximum flow algorithm solution. Think of the top left as the source point, and the bottom right as the meeting point. Walk from the top down once, and then from the bottom to go up once, in fact, it is equivalent to go down from the top two times, that is, from the source to the meeting point to find two of the most valuable and disjoint path. First, the diagram: (i-1) *n+j-> (i-1) *n+j+n*n (W is 1 (note: Source and sink point is 2, because two paths are found), cost is the value in the matrix);( i-1) *n+j+n*n (i-1) *n+j+1 (W for 1,cost is 0)(i-1) *n+j+n*n-I*n+j (W for 1,cost 0)then it can be solved by cost flow. (It is not clear in the title that each number is positive, I dis[] function initialized 0WA, this is why? )
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cstdlib>5#include <cmath>6#include <algorithm>7#include <vector>8#include <queue>9 #defineINF 0x7fffffffTen using namespacestd; One Const intmaxn=20000+Ten; A Const intM =9999999; - - intN from, to; the structnode - { - intV,flow,cost; - intNext; +}edge[m*3]; - intHead[maxn],edgenum; + intDIS[MAXN],PRE[MAXN],PID[MAXN],VIS[MAXN]; A intan[ -][ -]; at - voidAddintUintVintFlowintCost ) - { -Edge[edgenum].v=v; edge[edgenum].flow=flow; -Edge[edgenum].cost=cost; edge[edgenum].next=Head[u]; -head[u]=edgenum++; in -Edge[edgenum].v=u; edge[edgenum].flow=0; toEdge[edgenum].cost=-cost; edge[edgenum].next=Head[v]; +head[v]=edgenum++; - } the * intSPFA () $ {Panax Notoginsengmemset (Vis,0,sizeof(Vis)); -memset (dis,-1,sizeof(DIS)); thequeue<int>Q; +Q.push ( from); Adis[ from]=0; thevis[ from]=1; + while(!q.empty ()) - { $ intu=Q.front (); Q.pop (); $vis[u]=0; - for(intI=head[u]; i!=-1; i=edge[i].next) - { the intv=edge[i].v; - if(edge[i].flow>0&& dis[v]<dis[u]+edge[i].cost)Wuyi { thedis[v]=dis[u]+Edge[i].cost; -pre[v]=u; Wupid[v]=i; - if(!Vis[v]) About { $vis[v]=1; - Q.push (v); - } - } A } + } the returnDis[to]; - } $ the intmincost () the { the intaug=0, maxflow=0; the intans=0; - intNcase=0; in while(1) the { theaug=inf; About inttmp=SPFA (); the if(tmp==0) Break; the for(intI=to; i!= from; i=Pre[i]) the { + if(Edge[pid[i]].flow<( ) -aug=Edge[pid[i]].flow; the }Bayi for(intI=to; i!= from; i=Pre[i]) the { theEdge[pid[i]].flow-=; -edge[pid[i]^1].flow + =; - } theAns + =tmp; thencase++; the if(ncase==2) Break; the } - returnans-an[1][1]-An[n][n]; the } the the intMain ()94 { the while(SCANF ("%d", &n)! =EOF) the { thememset (head,-1,sizeof(head));98edgenum=0; About for(intI=1; i<=n; i++.) - {101 for(intj=1; j<=n; j + +)102scanf"%d",&an[i][j]);103 }104 from=1; theto=2*n*N;106 for(intI=1; i<=n; i++.)107 {108 for(intj=1; j<=n; j + +)109 { the intu= (I-1) *n+J;111 intv= (I-1) *n+j+n*N; theAdd (U,v,1, An[i][j]);113 if(j+1<=n) Add (v,u+1,1,0); the if(i+1<=n) Add (V,i*n+j,1,0); the } the }117Add from, from+n*n,1, an[1][1]);118Add (N*n,to,1, An[n][n]);119 intsum=mincost (); -printf"%d\n", sum);121 }122 return 0;123}
HDU 2686 Matrix minimum cost max flow