POJ3204——Ikki's Story I - Road Reconstruction(网络流)
2017-02-26 19:41
357 查看
Ikki's Story I - Road Reconstruction
Description
Ikki is the king of a small country – Phoenix, Phoenix is so small that there is only one city that is responsible for the production of daily goods, and uses the road network to transport the goods to the capital. Ikki finds that the biggest problem in
the country is that transportation speed is too slow.
Since Ikki was an ACM/ICPC contestant before, he realized that this, indeed, is a maximum flow problem. He coded a maximum flow program and found the answer. Not satisfied with the current status of the transportation speed, he wants to increase the transportation
ability of the nation. The method is relatively simple, Ikki will reconstruct some roads in this transportation network, to make those roads afford higher capacity in transportation. But unfortunately, the country of Phoenix is not so rich in GDP that there
is only enough money to rebuild one road. Ikki wants to find such roads that if reconstructed, the total capacity of transportation will increase.
He thought this problem for a loooong time but cannot get it. So he gave this problem to frkstyc, who put it in this POJ Monthly contest for you to solve. Can you solve it for Ikki?
Input
The input contains exactly one test case.
The first line of the test case contains two integers N, M (N ≤ 500, M ≤ 5,000) which represents the number of cities and roads in the country, Phoenix, respectively.
M lines follow, each line contains three integers a, b, c, which means that there is a road from city a to city b with a transportation capacity of c (0 ≤ a, b < n, c ≤
100). All the roads are directed.
Cities are numbered from 0 to n − 1, the city which can product goods is numbered 0, and the capital is numbered n − 1.
Output
You should output one line consisting of only one integer K, denoting that there are K roads, reconstructing each of which will increase the network transportation capacity.
Sample Input
Sample Output
首先跑一遍最大流,找到所有的增广路。
然后对于每一条满流的边,增加其流量,然后跑一边bfs看能不能再找到一条增广路径。如果可以,说明对这条边扩容是有效的。
Time Limit: 2000MS | Memory Limit: 131072K | |
Total Submissions: 7759 | Accepted: 2242 |
Ikki is the king of a small country – Phoenix, Phoenix is so small that there is only one city that is responsible for the production of daily goods, and uses the road network to transport the goods to the capital. Ikki finds that the biggest problem in
the country is that transportation speed is too slow.
Since Ikki was an ACM/ICPC contestant before, he realized that this, indeed, is a maximum flow problem. He coded a maximum flow program and found the answer. Not satisfied with the current status of the transportation speed, he wants to increase the transportation
ability of the nation. The method is relatively simple, Ikki will reconstruct some roads in this transportation network, to make those roads afford higher capacity in transportation. But unfortunately, the country of Phoenix is not so rich in GDP that there
is only enough money to rebuild one road. Ikki wants to find such roads that if reconstructed, the total capacity of transportation will increase.
He thought this problem for a loooong time but cannot get it. So he gave this problem to frkstyc, who put it in this POJ Monthly contest for you to solve. Can you solve it for Ikki?
Input
The input contains exactly one test case.
The first line of the test case contains two integers N, M (N ≤ 500, M ≤ 5,000) which represents the number of cities and roads in the country, Phoenix, respectively.
M lines follow, each line contains three integers a, b, c, which means that there is a road from city a to city b with a transportation capacity of c (0 ≤ a, b < n, c ≤
100). All the roads are directed.
Cities are numbered from 0 to n − 1, the city which can product goods is numbered 0, and the capital is numbered n − 1.
Output
You should output one line consisting of only one integer K, denoting that there are K roads, reconstructing each of which will increase the network transportation capacity.
Sample Input
2 1 0 1 1
Sample Output
1
首先跑一遍最大流,找到所有的增广路。
然后对于每一条满流的边,增加其流量,然后跑一边bfs看能不能再找到一条增广路径。如果可以,说明对这条边扩容是有效的。
#include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include <cstring> #include <queue> #include <vector> using namespace std; const int MAXV=1010; const int INF=1000000007; struct edge{int to,cap,rev,is_reverse;}; vector <edge> G[MAXV]; int level[MAXV],iter[MAXV]; void add_edge(int from,int to,int cap){ G[from].push_back((edge){to,cap,(int)G[to].size(),0}); G[to].push_back((edge){from,0,(int)G[from].size()-1,1}); } void bfs(int s){ memset(level,-1,sizeof(level)); queue<int> que; level[s]=0; que.push(s); while(!que.empty()){ int v=que.front();que.pop(); for(int i=0;i<G[v].size();i++){ edge &e=G[v][i]; if(e.cap>0&&level[e.to]<0){ level[e.to]=level[v]+1; 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>0&&level[v]<level[e.to]){ int d=dfs(e.to,t,min(f,e.cap)); if(d>0){ e.cap-=d; G[e.to][e.rev].cap+=d; return d; } } } return 0; } int max_flow(int s,int t){ int flow=0; for(;;){ bfs(s); if(level[t]<0) return flow; memset(iter,0,sizeof(iter)); int f; while((f=dfs(s,t,INF))>0){ flow+=f; } } } int main(int argc, char *argv[]) { int n,m; scanf("%d%d",&n,&m); for(int i=0;i<m;i++){ int a,b,c; scanf("%d%d%d",&a,&b,&c); add_edge(a, b, c); } max_flow(0, n-1); int res=0; for(int i=0;i<n;i++){ for(int j=0;j<G[i].size();j++){ if(G[i][j].is_reverse==1){ continue; } if(G[i][j].cap==0){ G[i][j].cap++; bfs(0); if(level[n-1]>=0){ res++; } G[i][j].cap--; } } } printf("%d\n",res); }
相关文章推荐
- POJ 3204 Ikki's Story I-Road Reconstruction (网络流关键边)
- POJ 3204 Ikki's Story I-Road Reconstruction (网络流关键边)
- poj 3204 Ikki's Story I - Road Reconstruction && zoj 2532 Internship 网络流关键边
- POJ 3204 Ikki's Story I - Road Reconstruction 网络流初步 Dinic
- Poj 3204 Ikki's Story I - Road Reconstruction【最大流Dinic+可行方案边数统计】
- POJ 3204 Ikki's Story I - Road Reconstruction(最大流-Dinic)
- poj 3204 Ikki's Story I - Road Reconstruction(最大流割边)
- POJ 3204 Ikki's Story I - Road Reconstruction 残余网络
- poj 3204 Ikki's Story I - Road Reconstruction && zoj 2532 Internship 网络流关键边
- POJ 3204 - Ikki's Story I - Road Reconstruction 找最小割的割边数量
- poj 3204 Ikki's Story I - Road Reconstruction(最大流求割边)
- poj 3204 Ikki's Story I - Road Reconstruction
- poj 3204 Ikki's Story I - Road Reconstruction(最小割,求关键边)
- POJ 3204 Ikki's Story I - Road Reconstruction(最大流)
- 【POJ 3204】Ikki's Story I - Road Reconstruction
- poj 3204 Ikki's Story I - Road Reconstruction 找可使流量增加的割边个数 最小割+dfs
- POJ-3204-Ikki's Story I - Road Reconstruction
- poj 3204 Ikki's Story I - Road Reconstruction 最大流
- poj 3204 Ikki's Story I - Road Reconstruction (最大流的应用)
- poj 3204 Ikki's Story I - Road Reconstruction(最大流,增广路)