裸的最小费用最大流....
Bob lives in an ancient village, where transactions are done by one item exchange with another. Bob is very clever and he knows what items will become more valuable later on. So, Bob has decided to do some business with villagers.
At first, Bob has N kinds of items indexed from 1 to N, and each item has Ai. There are M ways to exchanges items. For the ith way (Xi, Yi), Bob can exchange oneXith item to one Yith item, vice versa. Now Bob wants that his ith item has exactly Bi, and he wonders what the minimal times of transactions is.
There are multiple test cases.
For each test case: the first line contains two integers: N and M (1 <= N, M <= 100).
The next N lines contains two integers: Ai and Bi (1 <= Ai, Bi <= 10,000).
Following M lines contains two integers: Xi and Yi (1 <= Xi, Yi <= N).
There is one empty line between test cases.
For each test case output the minimal times of transactions. If Bob could not reach his goal, output -1 instead.
2 1 1 2 2 1 1 2 4 2 1 3 2 1 3 2 2 3 1 2 3 4
1 -1Author: FENG, Jingyi
/* *********************************************** Author :CKboss Created Time :2015年08月24日 星期一 13时12分25秒 File Name :E.cpp ************************************************ */ #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <string> #include <cmath> #include <cstdlib> #include <vector> #include <queue> #include <set> #include <map> using namespace std; const int maxn=220; const int INF=0x3f3f3f3f; struct Edge { int to,next,cap,flow,cost; }edge[maxn*maxn]; 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) { 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,m; int A[maxn],B[maxn]; int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); while(scanf("%d%d",&n,&m)!=EOF) { int suma=0,sumb=0; for(int i=1;i<=n;i++) { scanf("%d%d",A+i,B+i); suma+=A[i]; sumb+=B[i]; } init(); for(int i=0;i<m;i++) { int u,v; scanf("%d%d",&u,&v); /// u--->v Add_Edge(u,v,INF,1); Add_Edge(v,u,INF,1); } if(suma!=sumb) { puts("-1"); continue; } int S=0,T=n+1; for(int i=1;i<=n;i++) { Add_Edge(S,i,A[i],0); Add_Edge(i,T,B[i],0); } N=n+2; int flow,cost; flow=MinCostMaxFlow(S,T,cost); //cout<<"flow "<<flow<<"cost "<<cost<<endl; if(flow==suma) { printf("%d\n",cost); } else { puts("-1"); continue; } } return 0; }