ZOJ 3885 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[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
",cost);
}
else
{
puts("-1"); continue;
}
}
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.