ZOJ 3885 The Exchange of Items
The Exchange of Items
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 (Xi, Yi), 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 <= 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
Source
Author
#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的更多相关文章
- POJ 1860 Currency Exchange / ZOJ 1544 Currency Exchange (最短路径相关,spfa求环)
POJ 1860 Currency Exchange / ZOJ 1544 Currency Exchange (最短路径相关,spfa求环) Description Several currency ...
- 【最短路】 ZOJ 1544 Currency Exchange 推断负圈
给出 N 种货币 M 条兑换关系 開始时全部的货币S 和有X 块钱 接下来M条关系 A B W1 W2 W3 W4 表示 A->B 所需的手续费为W2块钱 汇率为W1 B->A 所需的手 ...
- 一位学长的ACM总结(感触颇深)
发信人: fennec (fennec), 信区: Algorithm 标 题: acm 总结 by fennec 发信站: 吉林大学牡丹园站 (Wed Dec 8 16:27:55 2004) AC ...
- 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 ...
- zoj 2734 Exchange Cards【dfs+剪枝】
Exchange Cards Time Limit: 2 Seconds Memory Limit: 65536 KB As a basketball fan, Mike is also f ...
- 使用.NET读取exchange邮件
公司有个第3方的系统,不能操作源码修改错误捕获,但是错误会发一个邮件出来,里面包含了主要的信息.于是想从邮件下手,提取需要的数据 开始考虑使用的是exchange service,主要参考http:/ ...
- Exchange WebSerivce Usage
//ExchangeService版本为2007SP1 ExchangeService service = new ExchangeService(ExchangeVersion.Exchange20 ...
- [Exchange]使用EWS托管API2.0同步邮箱
你可以通过Exchange Web Serivice(EWS)托管API去检索从一个给定的时间点,文件夹中有变化的列表中的项. 客户端可以使用SyncFoldersItems方法,同步服务端的项目,你 ...
- [EWS]在exchange中的标识符
摘要 最近在用ews的方式开发邮箱服务,包括写邮件,查看某封邮件的详情,回复,全部回复及转发功能.在获取收件箱的时候,关于唯一标识符的问题.也有点困惑,在每个邮件item中,存在一个changeKey ...
随机推荐
- hdu1018--斯特灵公式
斯特灵公式 Wiki http://zh.wikipedia.org/wiki/斯特林公式 /** \brief hdu 1018 * * \param date 2014/7/24 * \param ...
- B1260 [CQOI2007]涂色paint 区间dp
这个题和我一开始想的区别不是很大,但是要我独自做出来还是有一些难度. 每一次涂色 只有这两种可能: 1) 把一段未被 覆盖过的区间 涂成 * 色 2) 把一段被一种颜色覆盖的区间涂成 * 色 (并且 ...
- shp系列(六)——利用C++进行Dbf文件的写(创建)
上一篇介绍了shp文件的创建,接下来介绍dbf的创建. 推荐结合读取dbf的博客一起看! 推荐结合读取dbf的博客一起看! 推荐结合读取dbf的博客一起看! 1.Dbf头文件的创建 Dbf头文件的结构 ...
- BPM不同表单之间子表的赋值
上次写的是同一个表单的子表之间赋值,这次是不同表单之间子表的赋值 首先,我们给需要赋值的表单添加一个复制按钮 $.MvcSheet.AddAction({ Action: &qu ...
- First step in troubleshooting complex issues: Define and scope your issue properly
最近在查调试相关资料的时候,无意看到Tess的一篇关于如何快速分析复合场景问题的博文,感觉很实用,Mark备忘. My 9 questions for a pretty thorough proble ...
- Codeforces Round #448
Pizza Serparation #include<stdio.h> #include<string.h> #include<stdlib.h> #include ...
- Redis之Ubuntu下Redis集群搭建
安装redis 首先下载redis $ wget http://download.redis.io/releases/redis-4.0.10.tar.gz $ .tar.gz $ cd redis- ...
- 前端-JQ思维导图
看不清的朋友右键保存或者新窗口打开哦!喜欢我可以关注我,还有更多前端思维导图笔记
- (转)webpack用法
前言 webpack前端工程中扮演的角色越来越重要,它也是前端工程化很重要的一环.本文将和大家一起按照项目流程学习使用wbepack,妈妈再也不用担心我不会使用webpack,哪里不会看哪里.这是一个 ...
- 利用Axis2默认口令安全漏洞入侵WebService网站
近期,在黑吧安全网上关注了几则利用Axis2默认口令进行渗透测试的案例,大家的渗透思路基本一致,利用的技术工具也大致相同,我在总结这几则案例的基础之上进行了技术思路的拓展.黑吧安全网Axis2默认口令 ...