POJ 2195 minimum charge flow
Q: to give a messy square matrix, H Represents the home and m represents the people. Now everyone is going home and asking everyone the steps to go home and
Idea: I think of the cost flow very well. The cost is the minimum number of steps for people to go home, and the traffic is the number of people. If you connect to the edge, everyone will reach home with a capacity of 1, the cost is the edge of the number of steps. The super source point is connected to people. The capacity is 1, the cost is 0, and the home is connected to the super sink point. The same capacity is 1, and the fattening Hall is 0, the result is the minimum cost stream. PS: the entry question is quite simple .........
#include
#include
#include
#include
#include
#include
#includeusing namespace std;typedef long long ll;const int inf=0x3f3f3f3f;const int maxn=10010;typedef pair
P;struct edge{ int to,cap,cost,rev; edge(); edge(int a,int b,int c,int d){to=a,cap=b,cost=c,rev=d;};};vector
G[maxn];int h[maxn],dis[maxn],prevv[maxn],preve[maxn];void addedge(int st,int en,int cap,int cost){ G[st].push_back(edge(en,cap,cost,G[en].size())); G[en].push_back(edge(st,0,-cost,G[st].size()-1));}int min_cost_flow(int st,int en,int f){ int ans=0; memset(h,0,sizeof(h)); while(f>0){ priority_queue
,greater
>que; memset(dis,inf,sizeof(dis)); dis[st]=0;que.push(P(0,st)); while(!que.empty()){ P p=que.top();que.pop(); int v=p.second; if(dis[v]
0&&dis[e.to]>dis[v]+e.cost+h[v]-h[e.to]){ dis[e.to]=dis[v]+e.cost+h[v]-h[e.to]; prevv[e.to]=v; preve[e.to]=i; que.push(P(dis[e.to],e.to)); } } } if(dis[en]==inf) return -1; for(int i=0;i