HDU 4862 Jump (Multi-school joint training 1) (minimum cost maximum stream), hdu4862
Address: HDU4862
The minimum cost flow is still too small. You cannot figure it out...
Directly reference the official question...
The minimum K path coverage model is solved by the cost flow or KM algorithm. A two-part graph is constructed. X has N * M nodes, and The Source Vertex connects one edge to each node in X, with 1 traffic, the cost is 0. Department Y has N * M nodes, and each node connects one edge to the sink. The traffic is 1 and the cost is 0, if node X in Part x can reach node Y in part y within one step, even side x-> y, the cost is the amount of energy consumed from the x-grid to the y-grid minus the amount of energy obtained. The traffic is 1, and a new node is added to the X-grid, which means that K requests can be initiated from any node, the Source Vertex is connected to its edge. The cost is 0, and the traffic is K. This vertex is connected to each edge in the Y part. The cost is 0, and the traffic is 1. This graph is used to run the maximum flow with the minimum cost, if a full stream is used, the solution exists; otherwise, the reverse Number of the minimum fee is the maximum energy that can be obtained.
The Code is as follows:
#include <iostream>#include <stdio.h>#include <string.h>#include <stdlib.h>#include <math.h>#include <ctype.h>#include <queue>#include <map>#include<algorithm>using namespace std;const int INF=0x3f3f3f3f;int head[500], source, sink, cnt, flow, cost;int cur[500], d[500], vis[500], mp[20][20];struct node{ int u, v, cap, cost, next;} edge[1000000];void add(int u, int v, int cap, int cost){ edge[cnt].v=v; edge[cnt].cap=cap; edge[cnt].cost=cost; edge[cnt].next=head[u]; head[u]=cnt++; edge[cnt].v=u; edge[cnt].cap=0; edge[cnt].cost=-cost; edge[cnt].next=head[v]; head[v]=cnt++;}int spfa(){ memset(d,INF,sizeof(d)); memset(vis,0,sizeof(vis)); queue<int>q; q.push(source); d[source]=0; cur[source]=-1; int minflow=INF, i; while(!q.empty()) { int u=q.front(); q.pop(); vis[u]=0; for(i=head[u]; i!=-1; i=edge[i].next) { int v=edge[i].v; if(d[v]>d[u]+edge[i].cost&&edge[i].cap) { d[v]=d[u]+edge[i].cost; minflow=min(minflow,edge[i].cap); cur[v]=i; if(!vis[v]) { vis[v]=1; q.push(v); } } } } if(d[sink]==INF) return 0; flow+=minflow; cost-=minflow*d[sink]; for(i=cur[sink]; i!=-1; i=cur[edge[i^1].v]) { edge[i].cap-=minflow; edge[i^1].cap+=minflow; } return 1;}void mcmf(int sum){ while(spfa()); if(flow==sum) printf("%d\n",cost); else printf("-1\n");}int main(){ int t, n, m, i, j, k, h, num=0; char s[30]; scanf("%d",&t); while(t--) { num++; scanf("%d%d%d",&n,&m,&k); for(i=0; i<n; i++) { scanf("%s",s); for(j=0; j<m; j++) { mp[i][j]=s[j]-'0'; } } source=0; sink=2*n*m+2; cnt=0; memset(head,-1,sizeof(head)); flow=0; cost=0; for(i=1; i<=n*m; i++) { add(source,i,1,0); add(i+n*m+1,sink,1,0); add(n*m+1,i+n*m+1,1,0); } add(source,n*m+1,k,0); int z; for(i=0; i<n; i++) { for(j=0; j<m; j++) { for(h=j+1; h<m; h++) { z=0; if(mp[i][j]==mp[i][h]) z=mp[i][j]; add(i*m+j+1,n*m+1+i*m+h+1,1,h-j-1-z); } for(h=i+1; h<n; h++) { int z=0; if(mp[i][j]==mp[h][j]) z=mp[i][j]; add(i*m+j+1,n*m+1+h*m+j+1,1,h-i-1-z); } } } printf("Case %d : ",num); mcmf(n*m); } return 0;}