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 ...
随机推荐
- 不重启IIS修改dotnet framework版本
因为公司现在存在.net站点和asp站点共同运行的情况,所以需要对IIS进行一些修改,运行环境Win2003+IIS6 一.起因 原来的老站是asp开发的,用的是.net 2.0运行环境; 新站是.n ...
- php中除法取整的方法(round,ceil,floor)
PHP中遇到需要将除法所得结果取整的情况时,就需要用到以下方法: 1. round:四舍五入 round() 函数对浮点数进行四舍五入. 语法:round(x, prec) 参数 描述 x 可选.规定 ...
- js promise 介绍和使用
1.什么是promise js是单线程执行的. ajax是典型的异步操作,我们通常会在ajax的成功或者失败之后写上回掉函数.这中写法是一种嵌套的方式,如果回掉多了会造成代码复杂并且难以复用. pro ...
- yii项目开发配置
Clone项目 git clone https://gitee.com/s***/dianshang.git 安装yii php ini 选择 [0] Development 安装扩展 copy co ...
- div根据鼠标的移入移除显示隐藏
onmouseout 是把div当成一个对象,div里面包含的元素当成别的对象,所以移动的时候,会隐藏,达不到我们预期的效果. onmouseleave 就是把整个div当成一个对象. 大家可以去试 ...
- swift @objc dynamic
@objc vs @objc dynamic @objc: Objective-C entry points One can explicitly write @objc on any Swift ...
- spring-shiro 配置
配置 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www ...
- H5 canvas 之乱画
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- js 复制文字、 复制链接到粘贴板
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Zend Studio 离线汉化包下载方法
进入eclipse官网 语言包位置 http://www.eclipse.org/babel/downloads.php