Trade

Time Limit: 5000ms
Memory Limit: 32768KB

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

Special Judge

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

Andrew Stankevich
 
解题:有源汇的上下界最小流
  1. 先按无源汇的上下界可行流建图
  2. 对S到T跑最大流$f_1$,然后连接$<T,S,INF>$,再跑次最大流$f_2$
  3. 如果$f_1+f_2=\sum_{du[i]>0}{du[i]}$则存在可行流,此时边$<T,S>$的反向弧的流量即是最小流
  4. 然后输出不在残量网络上的边
 #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的更多相关文章

  1. ZOJ FatMouse' Trade 贪心

    得之我幸,不得,我命.仅此而已. 学姐说呀,希望下次看到你的时候依然潇洒如故.(笑~) 我就是这么潇洒~哈哈. 感觉大家比我还紧张~ 我很好的.真的 ------------------------- ...

  2. ZOJ 2753 Min Cut (Destroy Trade Net)(无向图全局最小割)

    题目大意 给一个无向图,包含 N 个点和 M 条边,问最少删掉多少条边使得图分为不连通的两个部分,图中有重边 数据范围:2<=N<=500, 0<=M<=N*(N-1)/2 做 ...

  3. ZOJ 2109 FatMouse&#39; Trade (背包 dp + 贪婪)

    链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1109 FatMouse prepared M pounds of cat ...

  4. zoj 2109 FatMouse' Trade

    FatMouse' Trade Time Limit: 2 Seconds      Memory Limit: 65536 KB FatMouse prepared M pounds of cat ...

  5. HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  6. ZOJ People Counting

    第十三届浙江省大学生程序设计竞赛 I 题, 一道模拟题. ZOJ  3944http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=394 ...

  7. ZOJ 3686 A Simple Tree Problem

    A Simple Tree Problem Time Limit: 3 Seconds      Memory Limit: 65536 KB Given a rooted tree, each no ...

  8. ZOJ Problem Set - 1394 Polar Explorer

    这道题目还是简单的,但是自己WA了好几次,总结下: 1.对输入的总结,加上上次ZOJ Problem Set - 1334 Basically Speaking ac代码及总结这道题目的总结 题目要求 ...

  9. ZOJ Problem Set - 1392 The Hardest Problem Ever

    放了一个长长的暑假,可能是这辈子最后一个这么长的暑假了吧,呵呵...今天来实验室了,先找了zoj上面简单的题目练练手直接贴代码了,不解释,就是一道简单的密文转换问题: #include <std ...

随机推荐

  1. 暴力 BestCoder Round #46 1001 YJC tricks time

    题目传送门 /* 暴力:模拟枚举每一个时间的度数 详细解释:http://blog.csdn.net/enjoying_science/article/details/46759085 期末考结束第一 ...

  2. 转 ORA-00054 的解决方法

    统有一个不用的索引,想删除这个索引, SQL> drop index GPSTIME_GLOBAL_INDEX  2  /drop index GPSTIME_GLOBAL_INDEX      ...

  3. jquery readio checked

    今天太鬼火了为这个难问题搜了一下午了到最后还是csdn的朋友给了我正确的答案http://bbs.csdn.net/topics/300162450谢谢这位朋友 // $("#ISOK1&q ...

  4. Android 计算view 的高度

    上午在做一个QuickAction里嵌套一个ListView,在Demo运行没事,结果引入到我的项目里,发现我先让它在Button上面,结果是无视那个Button的高度,这很明显,就是那个Button ...

  5. 把WebForm移植到.Net MVC中

    最近写项目,想把以前项目中的几个功能页面移植过来(想偷懒一下),在网上查了很多的资料,多数资料都是直接在MVC中添加WebForm,和我的需求不同.在此非常感谢网友“Jason”给予的帮助,终于搞定了 ...

  6. vba,excel,身份证,照片

    Sub 插入图片() '调整单元格大小,以适应图片大小 功能 插入身份证照片打印 - 正面在单元格d6       反面单元格d10 ActiveSheet.Pictures.Delete '清理过期 ...

  7. UEFI启动 安装win8 win10 及windows server 2012 最简单的方法

    纯UEFI模式只认U盘 纯UEFI模式下U盘安装的具体步骤其实很简单:                                   1.BIOS设置中启动项关闭兼容模式            ...

  8. [eclipse]的快捷键的设置

    今天新解压了一个eclipse,发现alt+/提示的快捷键不好用了,开始比较犯懒,但是发现开发效率低下,总结几个eclipse下快捷方式的解决办法. 第一个解决没有一点提示的情况. 1.首先通过菜单打 ...

  9. 新手写的一个DBCP工具类

    package com.xx.questionnaire.util.dao; import java.io.IOException; import java.sql.Connection; impor ...

  10. day1 python 基础

    # 一行注释"""多行注释"""print("hello world\n" * 3)name = "sure& ...