原题链接: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的更多相关文章

  1. Aizu 2304 Reverse Roads 费用流

    Reverse Roads Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view ...

  2. Aizu 2304 Reverse Roads(无向流)

    把有向图修改成无向图,并保证每条边的流量守恒并满足有向容量(即abs(flow(u,v) - flow(v,u)) <= 1)满足限制. 得到最大流,根据残流输出答案. 因为最后少了'\n'而W ...

  3. CF 1100E Andrew and Taxi(二分答案)

    E. Andrew and Taxi time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  4. POJ - 2421 Constructing Roads 【最小生成树Kruscal】

    Constructing Roads Description There are N villages, which are numbered from 1 to N, and you should ...

  5. HNU 13411 Reverse a Road II(最大流+BFS)经典

    Reverse a Road II Time Limit: 10000ms, Special Time Limit:25000ms, Memory Limit:65536KB Total submit ...

  6. LeetCode 7. Reverse Integer

    Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have you ...

  7. js sort() reverse()

    数组中存在的两个方法:sort()和reverse() 直接用sort(),如下: ,,,,,,,,,,,]; console.log(array.sort());ps:[0, 1, 2, 2, 29 ...

  8. [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 ...

  9. [LeetCode] Reverse String 翻转字符串

    Write a function that takes a string as input and returns the string reversed. Example: Given s = &q ...

随机推荐

  1. HTML练习题

    1.查询一下对div和span标签的理解 div标签:是用来为HTML文档内大块的内容提供结构和背景的元素.DIV的起始标签和结束标签之间的所有内容都是用来构成这个块的,中文我们把它称作“层”. sp ...

  2. leetcode 【 Best Time to Buy and Sell Stock III 】python 实现

    题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...

  3. CSS视觉格式化模型

    CSS视觉格式化模型(visual formatting model)是用来处理文档并将它显示在视觉媒体上的机制.这是CSS 2.1的一个基础概念.视觉格式化模型根据CSS盒模型为文档的每个元素生成0 ...

  4. 连接Oracle 10g时ORA-12514:TNS:监听进程不能解析在连接描述符中给出的SERVICE_NAME错误的解决

    近日服务器断电,导致客户端连接ORACLE服务器时出现ORA-12514错误,在网上查得解决方法如下 解决方法: 1. 打开/network/admin/listener.ora文件,找到: SID_ ...

  5. Map-Reduce基础

    1.设置文件读入分隔符 默认按行读入; 按句子读入 : conf1.set("textinputformat.record.delimiter", "."); ...

  6. MCMC 浅谈

    # MCMC 浅谈 1. 采样(sampling)是什么 MCMC在采样算法中有着举足轻重的地位,那么什么是采样?采样就是根据某种分布生成样本.举个例子,线性同余发生器就是根据均匀分布生成样本,这就很 ...

  7. Halcon11 Windows版 下载

    Halcon11 下载地址:http://www.211xun.com/download_page_2.html HALCON 11 是一套机器视觉图像处理库,由一千多个算子以及底层的数据管理核心构成 ...

  8. Spring整合hibernate -SessionFactory

    本文目录 1  本文采用 hibernate4 整合 Spring3.1 2 把Spring获取datasource通过class="org.springframework.orm.hibe ...

  9. icheck 动态设置选中,判断是否选择

    $(this).iCheck('check'); //启用禁用上级编号             $('#OnPar').on('ifUnchecked', function (event) {     ...

  10. 利用js生成二维码

    $('#barcode').qrcode({ width: 300, height: 300, render: !!document.createElement('canvas').getContex ...