Aizu 2304 Reverse Roads
原题链接:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2304
题意:
给你一个网络,其中每条边的容量是1,你可以通过调整边的方向来获得更大的流量,现在问你能获得的最大流量是多少。并且输出更改方向的边的编号。
题解:
就每条边弄成无向的,并且标记一下是否是原始边,然后跑一发Dinic即可。然后在残余网络上寻找解即可。
代码:
#include<iostream>
#include<stack>
#include<vector>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<queue>
#define MAX_S (1<<10)+10
#define MAX_V 500
#define MAX_N MAX_V
#define INF 1000009
using namespace std; struct edge {
int to, cap, rev;
bool isRev;
bool isOri;
int id; edge(int t, int c, int r, bool ir, bool io,int iid)
: to(t), cap(c), rev(r), isRev(ir), isOri(io),id(iid) { } edge() { }
}; template <class T>
inline bool scan_d(T &ret)
{
char c;
int sgn;
if(c=getchar(),c==EOF) return ; //EOF
while(c!=' -' &&(c<'' ||c>'' )) c=getchar();
sgn=(c==' -' )?-:;
ret=(c==' -' )?:(c-'' );
while(c=getchar(),c>='' &&c<='' ) ret=ret*+(c-'' );
ret*=sgn;
return ;
} vector<edge> G[MAX_N];
int level[MAX_V];
int iter[MAX_V]; void init(int totNode) {
for (int i = ; i <= totNode; i++)
G[i].clear();
memset(level, , sizeof(level));
memset(iter, , sizeof(iter));
} void add_edge(int from,int to,int cap,bool io,int id) {
G[from].push_back(edge (to, cap, G[to].size(),,io,id));
G[to].push_back(edge (from, , G[from].size() - ,,io,id));
} void bfs(int s) {
queue<int> que;
memset(level, -, sizeof(level));
level[s] = ;
que.push(s);
while (!que.empty()) {
int v = que.front();
que.pop();
for (int i = ; i < G[v].size(); i++) {
edge &e = G[v][i];
if (e.cap > && level[e.to] < ) {
level[e.to] = level[v] + ;
que.push(e.to);
}
}
}
} int dfs(int v,int t,int f) {
if (v == t)return f;
for (int &i = iter[v]; i < G[v].size(); i++) {
edge &e = G[v][i];
if (e.cap > && level[v] < level[e.to]) {
int d = dfs(e.to, t, min(f, e.cap));
if (d > ) {
e.cap -= d;
G[e.to][e.rev].cap += d;
return d;
}
}
}
return ;
} int max_flow(int s,int t) {
int flow = ;
for (; ;) {
bfs(s);
if (level[t] < )return flow;
memset(iter, , sizeof(iter));
int f;
while ((f = dfs(s, t, INF)) > ) {
flow += f;
}
}
} int S,T; int N,M; vector<int> ans; int main() {
scanf("%d%d", &N, &M);
for (int i = ; i < M; i++) {
int u, v;
scanf("%d%d", &u, &v);
add_edge(u, v, , , i + );
add_edge(v, u, , , i + );
}
scanf("%d%d", &S, &T);
int f = max_flow(S, T);
printf("%d\n", f);
for (int i = ; i <= N; i++)
for (int j = ; j < G[i].size(); j++)
if (G[i][j].isRev == && G[i][j].isOri == && G[i][j].cap == )
ans.push_back(G[i][j].id);
printf("%d\n", ans.size());
for (int i = ; i < ans.size(); i++)
printf("%d\n", ans[i]);
return ;
}
Aizu 2304 Reverse Roads的更多相关文章
- Aizu 2304 Reverse Roads 费用流
Reverse Roads Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view ...
- Aizu 2304 Reverse Roads(无向流)
把有向图修改成无向图,并保证每条边的流量守恒并满足有向容量(即abs(flow(u,v) - flow(v,u)) <= 1)满足限制. 得到最大流,根据残流输出答案. 因为最后少了'\n'而W ...
- CF 1100E Andrew and Taxi(二分答案)
E. Andrew and Taxi time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- POJ - 2421 Constructing Roads 【最小生成树Kruscal】
Constructing Roads Description There are N villages, which are numbered from 1 to N, and you should ...
- HNU 13411 Reverse a Road II(最大流+BFS)经典
Reverse a Road II Time Limit: 10000ms, Special Time Limit:25000ms, Memory Limit:65536KB Total submit ...
- LeetCode 7. Reverse Integer
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have you ...
- js sort() reverse()
数组中存在的两个方法:sort()和reverse() 直接用sort(),如下: ,,,,,,,,,,,]; console.log(array.sort());ps:[0, 1, 2, 2, 29 ...
- [LeetCode] Reverse Vowels of a String 翻转字符串中的元音字母
Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Giv ...
- [LeetCode] Reverse String 翻转字符串
Write a function that takes a string as input and returns the string reversed. Example: Given s = &q ...
随机推荐
- 自己用C语言写RH850 F1KM serial bootloader
了解更多关于bootloader 的C语言实现,请加我QQ: 1273623966 (验证信息请填 bootloader),欢迎咨询或定制bootloader(在线升级程序). 手上有块Renesas ...
- 大数据面试(HR电话了解)
1什么是HA集群? 所谓HA,即高可用(7*24小时不中断服务) HA集群是hadoop高可用集群,即有两个namenode,一个active,一个stanby,active的name挂掉之后,sta ...
- IOS开发学习笔记012-核心语法
1.点语法 2.成员变量的作用域 3. @property和@synthesize 4.id类型 5.构造方法 6.自定义构造方法 7.模板修改 8.Category - 分类 9.类扩展 一.点语法 ...
- win7装python3.6提示api-ms-win-runtime-1-1-0.dll丢失
win7为MSDN下的旗舰版,没有servicepack1那个,刚开始安装python3.6提示必须得安装servicepack1,于是乎到微软官网下了个900mb大小的安装包. https://ww ...
- Halcon18 Linux For Armv7a 下载
Halcon18 Linux For Armv7a 下载地址:http://www.211xun.com/download_page_16.html HALCON 18 是一套机器视觉图像处理库,由一 ...
- Windows命令行中pip install jieba,但没有安装到anaconda3中
系统混淆了python3环境下的pip和anaconda3环境下的pip. 找到Anaconda3的Scripts目录,我这里是C:\Users\Diane\Anaconda3\Scripts 将该目 ...
- 关于mybitis的异常总结
由于原项目中系统登录用户表中新添加了字段来关联其他表,但原来的mapper和bean就得重新再逆向出来,逆向后,就参照着原来你mapper来添加一些自定义在mapper的方法,那么接下来就爆出异常了 ...
- Vue2.0 - 生命周期
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8 ...
- 环境说明与HelloWorld
本人采用的是ExtJs4.2版本,采用WebStorm作为IDE开发工具 目录说明 builds:压缩后的ExtJs代码 docs:文档 examples:官方示例 locale:多国语言的资源文件 ...
- cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'mongo:mongo-client'.
cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element ...