ZOJ 2567 Trade
Trade
This problem will be judged on ZJU. Original ID: 2567
64-bit integer IO format: %lld      Java class name: Main
In the Middle Ages m European cities imported many goods from n Arabian cities. Due to continous feudal wars, European cities did not trade with each other, so is some European city needed some Arabian goods, the special trade route was established for this particular trade.
Studying the manuscripts historians have found out that each European city imported goods from at least two Arabian cities, and each Arabian city exported goods to at least two European cities. They have also investigated different factors and identified all potential trade routes (trade routes between some pairs of cities were impossible due to various reasons).
Now historians wonder, what is the minimal possible number of trade routes, that could have existed. Help them to find that out.
Input
The first line of the input file contains m, n, and p - the number of European and Arabian cities respectively, and the number of potential trade routes (1 <= m, n <= 300, 1 <= p <= nm). The following p lines describe potential trade routes, each description consists of two numbers - the European and the Arabian city connected by the route.
Output
On the first line of the output file print k - the minimal possible number of trade routes that could have existed. After that output k numbers - some minimal set of routes that might have existed to satisfy all conditions. Routes are numbered starting from 1 as they are given in the input file.
If historians must have made a mistake and it is impossible to satisfy the specified conditions, print -1 on the first and the only line of the output file.
Sample Input
5 5 14
1 2
1 3
1 4
1 5
2 1
2 5
3 1
3 5
4 1
4 5
5 1
5 2
5 3
5 4
Sample Output
12
1 2 3 5 6 7 8 9 10 12 13 14
Source
Author
- 先按无源汇的上下界可行流建图
- 对S到T跑最大流$f_1$,然后连接$<T,S,INF>$,再跑次最大流$f_2$
- 如果$f_1+f_2=\sum_{du[i]>0}{du[i]}$则存在可行流,此时边$<T,S>$的反向弧的流量即是最小流
- 然后输出不在残量网络上的边
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = ;
struct arc{
int to,flow,next;
arc(int x = ,int y = ,int z = -){
to = x;
flow = y;
next = z;
}
}e[];
int head[maxn],cur[maxn],d[maxn],du[maxn],tot;
void add(int u,int v,int flow){
e[tot] = arc(v,flow,head[u]);
head[u] = tot++;
e[tot] = arc(u,,head[v]);
head[v] = tot++;
}
bool bfs(int S,int T){
queue<int>q;
memset(d,-,sizeof d);
d[S] = ;
q.push(S);
while(!q.empty()){
int u = q.front();
q.pop();
for(int i = head[u]; ~i; i = e[i].next){
if(e[i].flow && d[e[i].to] == -){
d[e[i].to] = d[u] + ;
q.push(e[i].to);
}
}
}
return d[T] > -;
}
int dfs(int u,int T,int low){
if(u == T) return low;
int a,tmp = ;
for(int &i = cur[u]; ~i; i = e[i].next){
if(e[i].flow && d[e[i].to] == d[u] +&&(a=dfs(e[i].to,T,min(e[i].flow,low)))){
e[i].flow -= a;
e[i^].flow += a;
low -= a;
tmp += a;
if(!low) break;
}
}
if(!tmp) d[u] = -;
return tmp;
}
int dinic(int S,int T,int ret = ){
while(bfs(S,T)){
memcpy(cur,head,sizeof head);
ret += dfs(S,T,INF);
}
return ret;
}
int main(){
int n,m,p,u,v;
while(~scanf("%d%d%d",&n,&m,&p)){
memset(head,-,sizeof head);
memset(du,,sizeof du);
int S = tot = ,T = n + m + ,SS = T + ,TT = SS + ;
for(int i = ; i < p; ++i){
scanf("%d%d",&u,&v);
add(u,v + n,);
}
for(int i = ; i <= n; ++i){
add(S,i,INF);
du[S] -= ;
du[i] += ;
}
for(int i = ; i <= m; ++i){
add(i + n,T,INF);
du[i + n] -= ;
du[T] += ;
}
int sum = ;
for(int i = S; i <= T; ++i){
if(du[i] > ){
add(SS,i,du[i]);
sum += du[i];
}else add(i,TT,-du[i]);
}
u = dinic(SS,TT);
add(T,S,INF);
if(u + dinic(SS,TT) == sum){
bool flag = false;
printf("%d\n",e[tot-].flow);
for(int i = ; i < p; ++i)
if(!e[i*].flow){
if(flag) putchar(' ');
flag = true;
printf("%d",i + );
}
puts("");
}else puts("-1");
}
return ;
}
ZOJ 2567 Trade的更多相关文章
- ZOJ FatMouse' Trade 贪心
		得之我幸,不得,我命.仅此而已. 学姐说呀,希望下次看到你的时候依然潇洒如故.(笑~) 我就是这么潇洒~哈哈. 感觉大家比我还紧张~ 我很好的.真的 ------------------------- ... 
- ZOJ 2753 Min Cut (Destroy Trade Net)(无向图全局最小割)
		题目大意 给一个无向图,包含 N 个点和 M 条边,问最少删掉多少条边使得图分为不连通的两个部分,图中有重边 数据范围:2<=N<=500, 0<=M<=N*(N-1)/2 做 ... 
- ZOJ 2109 FatMouse' Trade (背包 dp + 贪婪)
		链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1109 FatMouse prepared M pounds of cat ... 
- zoj 2109 FatMouse' Trade
		FatMouse' Trade Time Limit: 2 Seconds Memory Limit: 65536 KB FatMouse prepared M pounds of cat ... 
- HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序
		FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ... 
- ZOJ  People Counting
		第十三届浙江省大学生程序设计竞赛 I 题, 一道模拟题. ZOJ 3944http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=394 ... 
- ZOJ 3686 A Simple Tree Problem
		A Simple Tree Problem Time Limit: 3 Seconds Memory Limit: 65536 KB Given a rooted tree, each no ... 
- ZOJ Problem Set - 1394 Polar Explorer
		这道题目还是简单的,但是自己WA了好几次,总结下: 1.对输入的总结,加上上次ZOJ Problem Set - 1334 Basically Speaking ac代码及总结这道题目的总结 题目要求 ... 
- ZOJ Problem Set - 1392 The Hardest Problem Ever
		放了一个长长的暑假,可能是这辈子最后一个这么长的暑假了吧,呵呵...今天来实验室了,先找了zoj上面简单的题目练练手直接贴代码了,不解释,就是一道简单的密文转换问题: #include <std ... 
随机推荐
- [POJ1721]Cards
			Description 剀剀和凡凡有N张牌(依次标号为1,2,--,N)和一台洗牌机.假设N是奇数.洗牌机的功能是进行如下的操作:对所有位置I(1≤I≤N),如果位置I上的牌是J,而且位置J上的牌是K ... 
- linux的SHELL编程
			管道 | 特殊的重定向 前一个命令的输出作为后一个命令的输入; 管道连接的命令数没有限制; who|wc−l统计用户数ps |sort|more 按序显示当前进程名 字符:具有特定作用的特殊字符 ,& ... 
- how-to-fix-fs-re-evaluating-native-module-sources-is-not-supported-graceful
			http://stackoverflow.com/questions/37346512/how-to-fix-fs-re-evaluating-native-module-sources-is-not ... 
- python general
			everything in python is object assignment is binding a name to an object one object can have several ... 
- MVP架构模式
			概念解释 MVP是Model(数据) View(界面) Presenter(表现层)的缩写,它是MVC架构的变种,强调Model和View的最大化解耦和单一职责原则 Model:负责数据的来源和封装, ... 
- 转】用Hadoop构建电影推荐系统
			原博文出自于: http://blog.fens.me/hadoop-mapreduce-recommend/ 感谢! 用Hadoop构建电影推荐系统 Hadoop家族系列文章,主要介绍Hadoop家 ... 
- actuator服务实战
			1. actuator服务实战 1.1. 前言 actuator默认集成了很多端点查看,这里我会挑选也用到可能性大些的 1.2. Endpoints 1.2.1. 使用方式 开启服务后,直接访问:lo ... 
- HTML标签,简单归纳
			列表标签 有序列表: <ol><li></li></ol> 无序列表: <ul><li></li></ul&g ... 
- nginx教程从入门到精通
			[转]nginx教程从入门到精通 nginx教程写了一段时间,无意中发现,nginx相关文章已经达到了近100篇了.觉得很有必要汇总到一起,它是我们运维生存时间的一片心血,他是学习nginx的同学必看 ... 
- css选择器的对比样式代码精简
			通常就分为这三大类:* 选定所有对象.通配选择符(Universal Selector)通常不建议使用通配选择符,因为它会遍历并命中文档中所有的元素,出于性能考虑,需酌情使用一.标签选择器,以开始标签 ... 
