POJ-3204-Ikki's Story I - Road Reconstruction(最大流)
题意:
给一个有向图
求给那些边增加容量能增加总的流量,求边的条数
分析:
一开始求的是割边,结果wa了,那是因为有些割边增加了容量,但总的容量也不会增加
只有满流的边并且从源点汇点都有一条可扩展的路时,才满足条件
因此,求完最大流后,在从源汇两点dfs,找扩展路。然后枚举边即可
// File Name: 3204.cpp
// Author: Zlbing
// Created Time: 2013年08月15日 星期四 14时59分13秒 #include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<cstring>
#include<stack>
#include<cmath>
#include<queue>
using namespace std;
#define CL(x,v); memset(x,v,sizeof(x));
#define INF 0x3f3f3f3f
#define LL long long
#define REP(i,r,n) for(int i=r;i<=n;i++)
#define RREP(i,n,r) for(int i=n;i>=r;i--)
const int MAXN=;
struct Edge{
int from,to,cap,flow;
};
bool cmp(const Edge& a,const Edge& b){
return a.from < b.from || (a.from == b.from && a.to < b.to);
}
struct Dinic{
int n,m,s,t;
vector<Edge> edges;
vector<int> G[MAXN];
bool vis[MAXN];
int d[MAXN];
int cur[MAXN];
void init(int n){
this->n=n;
for(int i=;i<=n;i++)G[i].clear();
edges.clear();
}
void AddEdge(int from,int to,int cap){
edges.push_back((Edge){from,to,cap,});
edges.push_back((Edge){to,from,,});//当是无向图时,反向边容量也是cap,有向边时,反向边容量是0
m=edges.size();
G[from].push_back(m-);
G[to].push_back(m-);
}
bool BFS(){
CL(vis,);
queue<int> Q;
Q.push(s);
d[s]=;
vis[s]=;
while(!Q.empty()){
int x=Q.front();
Q.pop();
for(int i=;i<(int)G[x].size();i++){
Edge& e=edges[G[x][i]];
if(!vis[e.to]&&e.cap>e.flow){
vis[e.to]=;
d[e.to]=d[x]+;
Q.push(e.to);
}
}
}
return vis[t];
}
int DFS(int x,int a){
if(x==t||a==)return a;
int flow=,f;
for(int& i=cur[x];i<(int)G[x].size();i++){
Edge& e=edges[G[x][i]];
if(d[x]+==d[e.to]&&(f=DFS(e.to,min(a,e.cap-e.flow)))>){
e.flow+=f;
edges[G[x][i]^].flow-=f;
flow+=f;
a-=f;
if(a==)break;
}
}
return flow;
}
//当所求流量大于need时就退出,降低时间
int Maxflow(int s,int t,int need){
this->s=s;this->t=t;
int flow=;
while(BFS()){
CL(cur,);
flow+=DFS(s,INF);
if(flow>need)return flow;
}
return flow;
}
//最小割割边
vector<int> Mincut(){
BFS();
vector<int> ans;
for(int i=;i<(int)edges.size();i++){
Edge& e=edges[i];
if(vis[e.from]&&!vis[e.to]&&e.cap>)ans.push_back(i);
}
return ans;
}
void Reduce(){
for(int i = ; i <(int) edges.size(); i++) edges[i].cap -= edges[i].flow;
}
void ClearFlow(){
for(int i = ; i <(int) edges.size(); i++) edges[i].flow = ;
}
void dfs1(int u)
{
vis_src[u]=;
for(int i=;i<(int)G[u].size();i++)
{
Edge e=edges[G[u][i]];
if(e.cap&&e.flow<e.cap&&!vis_src[e.to])
{
dfs1(e.to);
}
}
}
void dfs2(int u)
{
vis_dest[u]=;
for(int i=;i<(int)G[u].size();i++)
{
Edge e=edges[G[u][i]^];
if(e.cap&&e.flow<e.cap&&!vis_dest[e.from])
{
dfs2(e.from);
}
}
}
int solve()
{
CL(vis_dest,);
CL(vis_src,);
dfs1(s);
dfs2(t);
int ans=;
for(int i=;i<(int)edges.size();i++)
{
Edge e=edges[i];
if(e.cap&&vis_src[e.from]&&vis_dest[e.to])
ans++;
}
return ans;
}
int vis_dest[MAXN];
int vis_src[MAXN];
};
Dinic solver;
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
solver.init(n);
int s=,t=n-;
int a,b,c;
REP(i,,m)
{
scanf("%d%d%d",&a,&b,&c);
solver.AddEdge(a,b,c);
}
solver.Maxflow(s,t,INF);
int ans=solver.solve();
printf("%d\n",ans);
}
return ;
}
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 ...
- POJ 3204 Ikki's Story I-Road Reconstruction (网络流关键边)
[题意]给定一个N个节点M条边的网络流,求有多少条边,使得当增其中加任何一个边的容量后,整个网络的流将增加. 挺好的一道题,考察对网络流和增广路的理解. [思路] 首先关键边一定是满流边.那么对于一个 ...
- 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=""& ...
- poj 2195 二分图带权匹配+最小费用最大流
题意:有一个矩阵,某些格有人,某些格有房子,每个人可以上下左右移动,问给每个人进一个房子,所有人需要走的距离之和最小是多少. 貌似以前见过很多这样类似的题,都不会,现在知道是用KM算法做了 KM算法目 ...
- POJ 3436 ACM Computer Factory (网络流,最大流)
POJ 3436 ACM Computer Factory (网络流,最大流) Description As you know, all the computers used for ACM cont ...
随机推荐
- android开发之Notification学习笔记
今天总结了一下Notification的使用,与大家分享一下. MainActivity.java: 本文参考:http://www.jb51.net/article/36567.htm,http:/ ...
- html 新元素
html5新元素 html5语义元素 <header> 定义了文档或者文档的一部分区域的页眉 <nav> 定义了导航链接的部分 <section> 定义了文档的某个 ...
- webUploader上传组件 实际运用小结
WebUploader组件实际介绍: 官网:http://fex.baidu.com/webuploader/doc/index.html 组件优势及优化总结:http://itindex.net/d ...
- asp.net 自动遍历实体类
最近做项目需要读取修改前数据库中被修改的数据所有的信息,一开始想要在model层的每个类都写一个函数return一串字符串, 但是由于表太多,实体类数量太大,写出来太浪费时间,所以决定写一个通用的方法 ...
- Uri、UriMatcher、ContentUris详解
http://blog.csdn.net/feng88724/article/details/6331396 1.Uri 通用资源标志符(Universal Resource Identifier, ...
- Member var and Static var.
/* member variable and static variable: 1,invoke ways: member variable,also called 'instance' variab ...
- CI 笔记2,(命令规范等)
调试模式开启,$this->output->enable_profiler(TRUE); 保留字,不能和控制器重名,有3个,CI_Controller ,Default, index.这三 ...
- Spring中@Autowired注解与自动装配
1 使用配置文件的方法来完成自动装配我们编写spring 框架的代码时候.一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量.并且要配套写上 get 和 set方法. ...
- [LeetCode OJ] Gas Station
问题描述: There are N gas stations along a circular route, where the amount of gas at station i is gas[i ...
- 【HDU4366】【DFS序+分块】Successor
Problem Description Sean owns a company and he is the BOSS.The other Staff has one Superior.every st ...