标签:des style blog http color io os ar strong
K路最大费用最大流,
每个点的值只能取一次: 拆点,一个点的两个部分之间连 1 条费用mp容量一的边,连一条费用0容量很大的边
K次: 源点和汇点拆点,两个部分之间连K条边
Kaka‘s Matrix Travels
Description On an N × N chessboard with a non-negative number in each grid, Kaka starts his matrix travels with SUM = 0. For each travel, Kaka moves one rook from the left-upper grid to the right-bottom one, taking care that the rook moves only to the right or down. Kaka adds the number to SUM in each grid the rook visited, and replaces it with zero. It is not difficult to know the maximum SUMKaka can obtain for his first travel. Now Kaka is wondering what is the maximum SUM he can obtain after his Kth travel. Note the SUM is accumulative during the K travels. Input The first line contains two integers N and K (1 ≤ N ≤ 50, 0 ≤ K ≤ 10) described above. The following N lines represents the matrix. You can assume the numbers in the matrix are no more than 1000. Output The maximum SUM Kaka can obtain after his Kth travel. Sample Input 3 2 1 2 3 0 2 1 1 4 2 Sample Output 15 Source
POJ Monthly--2007.10.06, Huang, Jinsong
|
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <queue> using namespace std; const int maxn=100100; const int INF=1<<30; struct Edge { int to,next,cap,flow,cost; }edge[maxn*20]; int Adj[maxn],Size,n; void init() { memset(Adj,-1,sizeof(Adj)); Size=0; } void addedge(int u,int v,int cap,int cost) { edge[Size].to=v; edge[Size].next=Adj[u]; edge[Size].cost=cost; edge[Size].cap=cap; edge[Size].flow=0; Adj[u]=Size++; } void Add_Edge(int u,int v,int cap,int cost) { //cout<<"add :"<<u<<" , "<<v<<" , "<<cap<<" , "<<cost<<endl; addedge(u,v,cap,cost); addedge(v,u,0,-cost); } int dist[maxn],vis[maxn],pre[maxn]; bool spfa(int s,int t) { queue<int> q; for(int i=0;i<n;i++) { dist[i]=-INF; vis[i]=false; pre[i]=-1; } dist[s]=0; vis[s]=true; q.push(s); while(!q.empty()) { int u=q.front(); q.pop(); vis[u]=false; for(int i=Adj[u];~i;i=edge[i].next) { int v=edge[i].to; if(edge[i].cap>edge[i].flow&& dist[v]<dist[u]+edge[i].cost) { dist[v]=dist[u]+edge[i].cost; pre[v]=i; if(!vis[v]) { vis[v]=true; q.push(v); } } } } if(pre[t]==-1) return false; return true; } int MinCostMaxFlow(int s,int t,int& cost) { int flow=0; cost=0; while(spfa(s,t)) { int Min=INF; for(int i=pre[t];~i;i=pre[edge[i^1].to]) { if(Min>edge[i].cap-edge[i].flow) Min=edge[i].cap-edge[i].flow; } for(int i=pre[t];~i;i=pre[edge[i^1].to]) { edge[i].flow+=Min; edge[i^1].flow-=Min; cost+=edge[i].cost*Min; } flow+=Min; } return flow; } int N,K; int mp[70][70]; int main() { while(scanf("%d%d",&N,&K)!=EOF) { init(); for(int i=0;i<N;i++) { for(int j=0;j<N;j++) { scanf("%d",&mp[i][j]); int a=i*N+j; int b=a+N*N; Add_Edge(a,b,1,mp[i][j]); Add_Edge(a,b,K-1,0); if(j+1<N) Add_Edge(b,a+1,K,0); if(i+1<N) Add_Edge(b,a+N,K,0); } } int s=0,t=2*N*N-1; n=2*N*N; int FLOW,COST; FLOW=MinCostMaxFlow(s,t,COST); //printf("FLOW=%d COST=%d\n",FLOW,COST); printf("%d\n",COST); } return 0; }
POJ 3422 Kaka's Matrix Travels
标签:des style blog http color io os ar strong
原文地址:http://blog.csdn.net/ck_boss/article/details/39256623