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. luogu1967 货车运输 最大瓶颈生成树

    题目大意 给出一张图,给出q对点,求这两个点间权值最小边最大的路径,输出这个最小边权. 题解 我们先一条一条边建图.当建立的边使得图中形成环时,因为环中的每个节点只考虑是否连通和瓶颈大小,要想互相连通 ...

  2. luogu4011 孤岛营救问题 分层图

    关键词:分层图 状态压缩 最短路径 分层图:现在要求从起点到终点的最优路线,但受到手里拿着哪些钥匙的影响,最优路线不单纯了.因此,决定一个节点.一条边的存在的数中应当增加一个手中拿有钥匙的状态.这样就 ...

  3. Node.js:NPM 使用介绍

    ylbtech-Node.js:NPM 使用介绍 1.返回顶部 1. NPM 使用介绍 NPM是随同NodeJS一起安装的包管理工具,能解决NodeJS代码部署上的很多问题,常见的使用场景有以下几种: ...

  4. ASCII编码

    ASCII(American Standard Code for Information Interchange,美国信息互换标准代码,ASCⅡ)是基于拉丁字母的一套电脑编码系统.它主要用于显示现代英 ...

  5. 在量化金融中15个最流行的Python数据分析库

    Python是当今应用最广泛的编程语言之一,以其效率和代码可读性著称.作为一个科学数据的编程语言,Python介于R和java之间,前者主要集中在数据分析和可视化,而后者主要应用于大型应用.这种灵活性 ...

  6. 如果碰到git提示“ignored tracked with git”,那么使用以下命令解决

    命令:git rm --cached -r 文件/文件夹 问题在初始化git仓库的时候没有创建.gitignore文件来过滤不必要提交的文件, 后来却发现某些文件不需要提交, 但是这些文件已经被提交了 ...

  7. Entity Framework 的懒加载、预先加载、显示加载

    1.新建两个实体,一个班级有多个学生 public class Student { public int StudentId { get; set; } public string StudentNa ...

  8. 第4章 部署模式 Deployment Plan(部署规划)

    已开发了基于组件的应用程序,该应用程序在逻辑上构造为多层结构,如 Three-Layered Services Application. 中所述.您希望将它分布到一组在物理上为多级结构的服务器上,如 ...

  9. 使用Micrisoft.net设计方案 第一章 企业解决方案中构建设计模式

    第一章企业解决方案中构建设计模式 我们知道的系统总是由简单到复杂,而不是直接去设计一个复杂系统.如果直接去设计一个复杂系统,结果最终会导致失败.在设计系统的时候,先设计一个能够正常工作的系统,然后在此 ...

  10. jQuery操作样式知识总结

    css操作 功能:设置或者修改样式,操作的是style属性. 设置单个样式 //name:需要设置的样式名称 //value:对应的样式值 css(name, value); //使用案例 $(&qu ...