ZOJ 3885The Exchange of Items 最小费用最大流
裸的最小费用最大流....
The Exchange of Items
Time Limit: 2 Seconds Memory Limit: 65536 KB
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.
Input
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.
Output
For each test case output the minimal times of transactions. If Bob could not reach his goal, output -1 instead.
Sample Input
2 1
1 2
2 1
1 2
4 2
1 3
2 1
3 2
2 3
1 2
3 4
Sample Output
1
-1
Author: FENG, Jingyi
Source: ZOJ Monthly, July 2017
/* ***********************************************
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;
int Adj,Size,N;
void init()
{
memset(Adj,-1,sizeof(Adj)); Size=0;
}
void addedge(int u,int v,int cap,int cost)
{
edge.to=v;
edge.next=Adj;
edge.cost=cost;
edge.cap=cap;
edge.flow=0;
Adj=Size++;
}
void Add_Edge(int u,int v,int cap,int cost)
{
addedge(u,v,cap,cost);
addedge(v,u,0,-cost);
}
int dist,vis,pre;
bool spfa(int s,int t)
{
queue<int> q;
for(int i=0;i<N;i++)
{
dist=INF; vis=false; pre=-1;
}
dist=0; vis=true; q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
vis=false;
for(int i=Adj;~i;i=edge.next)
{
int v=edge.to;
if(edge.cap>edge.flow&&
dist>dist+edge.cost)
{
dist=dist+edge.cost;
pre=i;
if(!vis)
{
vis=true; q.push(v);
}
}
}
}
if(pre==-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;~i;i=pre.to])
{
if(Min>edge.cap-edge.flow)
{
Min=edge.cap-edge.flow;
}
}
for(int i=pre;~i;i=pre.to])
{
edge.flow+=Min;
edge.flow-=Min;
cost+=edge.cost*Min;
}
flow+=Min;
}
return flow;
}
int n,m;
int A,B;
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; sumb+=B;
}
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,0);
Add_Edge(i,T,B,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;
}
页:
[1]