POJ 3204 Ikki's Story I-Road Reconstruction (网络流关键边)
【题意】给定一个N个节点M条边的网络流,求有多少条边,使得当增其中加任何一个边的容量后,整个网络的流将增加.
挺好的一道题,考察对网络流和增广路的理解。
【思路】
首先关键边一定是满流边。那么对于一个满流边<x,y>来说,如果残余网络中从起点到x和从y到终点都有路径可达的话,那么这条边的容量增加时,在残量网络上将会产生一条增广路,最大流的值一定会发生改变。
则算法如下:
求最大流,得到残余网络
枚举每条满流边,DFS判断是否分别从源点和到汇点可达,如果可达则加1。
#include
#include
#include
#include
#include
#include
#define MID(x,y) ((x+y)/2)
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
const int MAXV = 505;
const int MAXE = 20005;
const int oo = 0x3fffffff;
struct node{
int u, v, flow;
int opp;
int next;
};
struct Dinic{
node arc[MAXE];
int vn, en, head[MAXV]; //vn点个数(包括源点汇点),en边个数
int cur[MAXV]; //当前弧
int q[MAXV]; //bfs建层次图时的队列
int path[MAXE], top; //存dfs当前最短路径的栈
int dep[MAXV]; //各节点层次
void init(int n){
vn = n;
en = 0;
mem(head, -1);
}
void insert_flow(int u, int v, int flow){
arc[en].u = u;
arc[en].v = v;
arc[en].flow = flow;
arc[en].opp = en + 1;
arc[en].next = head[u];
head[u] = en ++; arc[en].u = v;
arc[en].v = u;
arc[en].flow = 0; //反向弧
arc[en].opp = en - 1;
arc[en].next = head[v];
head[v] = en ++;
}
bool bfs(int s, int t){
mem(dep, -1);
int lq = 0, rq = 1;
dep[s] = 0;
q[lq] = s;
while(lq < rq){
int u = q[lq ++];
if (u == t){
return true;
}
for (int i = head[u]; i != -1; i = arc[i].next){
int v = arc[i].v;
if (dep[v] == -1 && arc[i].flow > 0){
dep[v] = dep[u] + 1;
q[rq ++] = v;
}
}
}
return false;
}
int solve(int s, int t){
int maxflow = 0;
while(bfs(s, t)){
int i, j;
for (i = 1; i <= vn; i ++) cur[i] = head[i];
for (i = s, top = 0;;){
if (i == t){
int mink;
int minflow = 0x3fffffff;
for (int k = 0; k < top; k ++)
if (minflow > arc[path[k]].flow){
minflow = arc[path[k]].flow;
mink = k;
}
for (int k = 0; k < top; k ++)
arc[path[k]].flow -= minflow, arc[arc[path[k]].opp].flow += minflow;
maxflow += minflow;
top = mink; //arc[mink]这条边流量变为0, 则直接回溯到该边的起点即可(这条边将不再包含在增广路内).
i = arc[path[top]].u;
}
for (j = cur[i]; j != -1; cur[i] = j = arc[j].next){
int v = arc[j].v;
if (arc[j].flow && dep[v] == dep[i] + 1)
break;
}
if (j != -1){
path[top ++] = j;
i = arc[j].v;
}
else{
if (top == 0) break;
dep[i] = -1;
i = arc[path[-- top]].u;
}
}
}
return maxflow;
}
}dinic;
bool vis[MAXV];
bool reach(int u, int p){
vis[u] = 1;
if (u == p)
return true;
for (int i = dinic.head[u]; i != -1; i = dinic.arc[i].next){
int v = dinic.arc[i].v;
if (vis[v] || dinic.arc[i].flow <= 0) continue;
if (reach(v, p))
return true;
}
return false;
}
int work(int n){
int res = 0;
for (int i = 0; i < dinic.en; i += 2){
if (dinic.arc[i].flow == 0){
mem(vis, 0);
int u = dinic.arc[i].u;
int v = dinic.arc[i].v;
if (reach(1, u) && reach(v, n)){
res ++;
}
}
}
return res;
}
int main(){
//freopen("test.in", "r", stdin);
//freopen("test.out", "w", stdout);
int n, m;
scanf("%d %d", &n, &m);
dinic.init(n);
for (int i = 0; i < m; i ++){
int u,v,w;
scanf("%d %d %d", &u, &v, &w);
dinic.insert_flow(u+1, v+1, w);
}
dinic.solve(1, n);
printf("%d\n", work(n));
return 0;
}
POJ 3204 Ikki's Story I-Road Reconstruction (网络流关键边)的更多相关文章
- POJ 3204 Ikki's Story I - Road Reconstruction
Ikki's Story I - Road Reconstruction Time Limit: 2000MS Memory Limit: 131072K Total Submissions: 7 ...
- POJ3204 Ikki's Story I - Road Reconstruction
Ikki's Story I - Road Reconstruction Time Limit: 2000MS Memory Limit: 131072K Total Submissions: 7 ...
- POJ3184 Ikki's Story I - Road Reconstruction(最大流)
求一次最大流后,分别对所有满流的边的容量+1,然后看是否存在增广路. #include<cstdio> #include<cstring> #include<queue& ...
- poj 3204(最小割--关键割边)
Ikki's Story I - Road Reconstruction Time Limit: 2000MS Memory Limit: 131072K Total Submissions: 7 ...
- POJ 3207 Ikki's Story IV - Panda's Trick(2-sat问题)
POJ 3207 Ikki's Story IV - Panda's Trick(2-sat问题) Description liympanda, one of Ikki's friend, likes ...
- POJ 3207 Ikki's Story IV - Panda's Trick(2-sat)
POJ 3207 Ikki's Story IV - Panda's Trick id=3207" target="_blank" style=""& ...
- POJ3204 Ikki's Story - Road Reconstruction 网络流图的关键割边
题目大意:一个有源有汇的城市,问最少增加城市中的多少道路可以增加源到汇上各个路径上可容纳的总车流量增加. 网络流关键割边集合指如果该边的容量增加,整个网络流图中的任意从原点到汇点的路径的流量便可增加. ...
- POJ 3207 Ikki's Story IV - Panda's Trick
Ikki's Story IV - Panda's Trick Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 7296 ...
- poj 3207 Ikki's Story IV - Panda's Trick (2-SAT)
http://poj.org/problem?id=3207 Ikki's Story IV - Panda's Trick Time Limit: 1000MS Memory Limit: 13 ...
随机推荐
- ASP.NET文件上传
<asp:FileUpload ID="FileUpload" runat="server" /> private string upLoad() ...
- 模式对话框里的CheckedChanged事件
问题: 模式对话框里的CheckedChanged事件不被触发: 解决方法: 一.先不直接showModalDialog出要的页面,而是要放一个中单页面 window.showModalDialo ...
- SQL 跨服务器数据库增、删、改、查(二)
--创建链接服务器 exec sp_addlinkedserver 'jx3xxiednr3ucidf', ' ', 'SQLOLEDB', 'jx3xxiednr3ucidf' exec sp_ad ...
- 人生规划和GTD——“知”、“得”与“合”
写在前面:非常不错的一篇GTD入门以及人生目标与规划的文章,值得收藏. 本文来自 零折 作为<小强升职记>的读书感悟,给我自己.作为分享,也送给或许需要的给你. 我不知道你是否真的需要.但 ...
- ASP.NET遍历textbox文本框
Asp.Net如何遍历所有TextBox控件并清空 asp.net 不能像window那样直接遍历this.Controls就可以了,因为: this.Controls只是包含了Page根一级的con ...
- Why are very few schools involved in deep learning research? Why are they still hooked on to Bayesian methods?
Why are very few schools involved in deep learning research? Why are they still hooked on to Bayesia ...
- The 5th Zhejiang Provincial Collegiate Programming Contest---ProblemE:Easy Task
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2969 全场第一水题.我不知道怎么解释,看代码就好了... #include ...
- Avro RPC 之 Protocol 定义和代码生成
摘自http://avro.apache.org/docs/current/spec.html#Protocol+Declaration,1.7.6版 Protocol Declaration Avr ...
- 单例模式Java“完美”实现
我们通过单例模式可以保证系统中一个类只有一个实例而且该实例易于外界访问,从而方便对实例个数的控制并节约系统资源.如果希望在系统中某个类的对象只能存在一个,单例模式是最好的解决方案. public cl ...
- python datetime笔记
python datetime笔记 http://mint-green.diandian.com/post/2011-09-09/4892024 获取当前时间,并通过字符串输出. 格式为:%Y-%m- ...