The Exchange of Items

Time Limit: 2000ms
Memory Limit: 65536KB

This problem will be judged on ZJU. Original ID: 3885
64-bit integer IO format: %lld      Java class name: Main

 

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 (XiYi), Bob can exchange one Xith item to oneYith 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 <= NM <= 100).
The next N lines contains two integers: Ai and Bi (1 <= AiBi <= 10,000).
Following M lines contains two integers: Xi and Yi (1 <= XiYi <= 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
 

Source

Author

FENG, Jingyi
 
解题:费用流
 
两种建图方式
第一种,源点向i连流为Ai费用为0的边,i向汇点连流为Bi费用为0的边 可以交换的物品之间连费用为1流量无穷的双向边
 
第二种 对于ai < bi的 i向汇点连流量为bi-ai 费用为0的边,ai > bi的 源点向i连流量为ai-bi 费用为0的边 可以交换的物品间 建立流量无穷费用1的双向边
 
看是否满流
 
 #include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = ;
struct arc{
int to,flow,cost,next;
arc(int x = ,int y = ,int z = ,int nxt = -){
to = x;
flow = y;
cost = z;
next = nxt;
}
}e[maxn*maxn];
int head[maxn],p[maxn],tot;
void add(int u,int v,int flow,int cost){
e[tot] = arc(v,flow,cost,head[u]);
head[u] = tot++;
e[tot] = arc(u,,-cost,head[v]);
head[v] = tot++;
}
bool in[maxn];
int d[maxn],S,T;
bool spfa(){
queue<int>q;
memset(d,0x3f,sizeof d);
memset(in,false,sizeof in);
memset(p,-,sizeof p);
d[S] = ;
q.push(S);
while(!q.empty()){
int u = q.front();
q.pop();
in[u] = false;
for(int i = head[u]; ~i; i = e[i].next){
if(e[i].flow && d[e[i].to] > d[u] + e[i].cost){
d[e[i].to] = d[u] + e[i].cost;
p[e[i].to] = i;
if(!in[e[i].to]){
in[e[i].to] = true;
q.push(e[i].to);
}
}
}
}
return p[T] > -;
}
void solve(int sum){
int cost = ,flow = ;
while(spfa()){
int minF = INF;
for(int i = p[T]; ~i; i = p[e[i^].to])
minF = min(minF,e[i].flow);
for(int i = p[T]; ~i; i = p[e[i^].to]){
e[i].flow -= minF;
e[i^].flow += minF;
}
flow += minF;
cost += d[T]*minF;
}
if(sum == flow) printf("%d\n",cost);
else puts("-1");
}
int main(){
int n,m,u,v,sum;
bool flag = false;
while(~scanf("%d%d",&n,&m)){
//if(flag) puts("");
memset(head,-,sizeof head);
sum = S = tot = ;
T = n + ;
for(int i = ; i <= n; ++i){
scanf("%d%d",&u,&v);
add(S,i,u,);
add(i,T,v,);
sum += v;
}
for(int i = ; i < m; ++i){
scanf("%d%d",&u,&v);
add(u,v,INF,);
add(v,u,INF,);
}
solve(sum);
}
return ;
}

ZOJ 3885 The Exchange of Items的更多相关文章

  1. POJ 1860 Currency Exchange / ZOJ 1544 Currency Exchange (最短路径相关,spfa求环)

    POJ 1860 Currency Exchange / ZOJ 1544 Currency Exchange (最短路径相关,spfa求环) Description Several currency ...

  2. 【最短路】 ZOJ 1544 Currency Exchange 推断负圈

    给出 N 种货币 M 条兑换关系 開始时全部的货币S 和有X 块钱 接下来M条关系 A B W1 W2 W3 W4 表示 A->B 所需的手续费为W2块钱  汇率为W1 B->A 所需的手 ...

  3. 一位学长的ACM总结(感触颇深)

    发信人: fennec (fennec), 信区: Algorithm 标 题: acm 总结 by fennec 发信站: 吉林大学牡丹园站 (Wed Dec 8 16:27:55 2004) AC ...

  4. AMQP 0-9-1 Model Explained Why does the queue memory grow and shrink when publishing/consuming? AMQP和AMQP Protocol的是整体和部分的关系 RabbitMQ speaks multiple protocols.

    AMQP 0-9-1 Model Explained — RabbitMQ http://next.rabbitmq.com/tutorials/amqp-concepts.html AMQP 0-9 ...

  5. zoj 2734 Exchange Cards【dfs+剪枝】

    Exchange Cards Time Limit: 2 Seconds      Memory Limit: 65536 KB As a basketball fan, Mike is also f ...

  6. 使用.NET读取exchange邮件

    公司有个第3方的系统,不能操作源码修改错误捕获,但是错误会发一个邮件出来,里面包含了主要的信息.于是想从邮件下手,提取需要的数据 开始考虑使用的是exchange service,主要参考http:/ ...

  7. Exchange WebSerivce Usage

    //ExchangeService版本为2007SP1 ExchangeService service = new ExchangeService(ExchangeVersion.Exchange20 ...

  8. [Exchange]使用EWS托管API2.0同步邮箱

    你可以通过Exchange Web Serivice(EWS)托管API去检索从一个给定的时间点,文件夹中有变化的列表中的项. 客户端可以使用SyncFoldersItems方法,同步服务端的项目,你 ...

  9. [EWS]在exchange中的标识符

    摘要 最近在用ews的方式开发邮箱服务,包括写邮件,查看某封邮件的详情,回复,全部回复及转发功能.在获取收件箱的时候,关于唯一标识符的问题.也有点困惑,在每个邮件item中,存在一个changeKey ...

随机推荐

  1. APP漏洞自动化扫描专业评测报告(下篇)

    上篇.中篇回顾:通过收费情况.样本测试后的扫描时间.漏洞项对比以及扫描能力这几个方面对阿里聚安全[1].360App漏洞扫描[2].腾讯金刚审计系统[3].百度移动云测试中心[4]以及AppRisk ...

  2. 【剑指offer】Q19:二叉树的镜像

    def MirroRecursively(root): # root is None or just one node, return root if None == root or None == ...

  3. Cookies操作类

    实现代码: //声名一个数据集合 var listString = new List<string>() { "a", "b", "c&q ...

  4. Java-杂项:Float 加减精度问题

    ylbtech-Java-杂项:Float 加减精度问题 1.返回顶部 1. java float 加减精度问题在取这个字段的时候转换成BigDecimal就可以了同时,BigDecimal是可以设置 ...

  5. [ASP.Net] 转 > ASP.NET MVC 大牛之路

    URL: http://www.cnblogs.com/willick/ [ASP.NET MVC 大牛之路]01 - 开篇 [ASP.NET MVC 大牛之路]02 - C#高级知识点概要(1) - ...

  6. BZOJ 1061费用流

    思路: 我们可以列出几个不等式 用y0带进去变成等式 下-上 可以消好多东西 我们发现 等式左边的加起来=0 可以把每个方程看成一个点 正->负 连边 跑费用流即可 //By SiriusRen ...

  7. Ajax+Struts做登录判断

    Action类里: /* * 登录 */ public ActionForward doLogin(ActionMapping mapping,ActionForm form,HttpServletR ...

  8. WRAR下载及注册

    下载过程: 1.打开winrar官网:https://www.win-rar.com 2.点击下载winrar按钮,如上图所示 3.进入下一页面,点击下载按钮即可完成下载过程 注册过程:https:/ ...

  9. 关于HTTPS通信机制的笔记

    一次安全可靠的通信--HTTPS原理 转自:腾讯开放社区raphealguo文章

  10. BZOJ3573: [Hnoi2014]米特运输(树上乱搞)

    Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 1669  Solved: 1031[Submit][Status][Discuss] Descript ...