Question address: POJ2195
I am the first to get my career expenses !! After the invitational competition, I decided to learn more. At least I had to drop A simple network stream. In the future, the maximum flow cost will be reduced to a single click.
In the past, the cost flow had always been a tall and unattainable image in my mind. But in the past two days, I found that the original cost flow was a spfa and I had to add something to it... I always thought that the billing flow would be much more troublesome than the isap of the largest stream. After all, there is an extra cost element .... I am really wrong .. After studying it carefully, we can solve it with only one spfa...
This is an entry-level question (although more ..), The idea of creating a graph is very simple, that is, connecting people to the source site. The traffic is 1, the cost is 0, the house is connected to the sink, the traffic is 1, and the cost is 0. at the beginning, I didn't know how to use the traffic 1. Later I thought it was supposed to limit the traffic, because every person and House can only go once. Connect people to the House. The traffic is 1 and the cost is the number of steps to be taken. Finally, find the minimum fee flow. 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[300], s, t, cnt, cost, flow;int d[300], q[10000], cur[300], vis[300];struct node{ int u, v, cap, cost, next;} edge[100000];struct a{ int x, y;} men[300];struct b{ int x, y;} house[300];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(){ int f1=0, f2=0, i, minflow=INF; memset(d,INF,sizeof(d)); memset(vis,0,sizeof(vis)); d[s]=0; cur[s]=-1; q[f1++]=s; while(f1>=f2) { int u=q[f2++]; 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; if(minflow>edge[i].cap) minflow=edge[i].cap; cur[v]=i; if(!vis[v]) { q[f1++]=v; vis[v]=1; } } } } if(d[t]==INF) return 0; flow+=minflow; cost+=minflow*d[t]; for(i=cur[t]; i!=-1; i=cur[edge[i^1].v]) { edge[i].cap-=minflow; edge[i^1].cap+=minflow; } return 1;}int main(){ int n, m, i, n1, n2, j; char mp[200][200]; while(scanf("%d%d",&n,&m)!=EOF&&n&&m) { n1=0; n2=0; memset(head,-1,sizeof(head)); cnt=0; for(i=0; i<n; i++) { scanf("%s",mp[i]); for(j=0; j<m; j++) { if(mp[i][j]=='m') { men[++n1].x=i; men[n1].y=j; } else if(mp[i][j]=='H') { house[++n2].x=i; house[n2].y=j; } } } s=0; t=n1+n2+1; flow=0; cost=0; for(i=1; i<=n1; i++) add(s,i,1,0); for(i=1; i<=n2; i++) add(i+n1,t,1,0); for(i=1; i<=n1; i++) { for(j=1; j<=n2; j++) { add(i,j+n1,1,abs(men[i].x-house[j].x)+abs(men[i].y-house[j].y)); } } while(spfa()); printf("%d\n",cost); } return 0;}