Ikki ' s Story i-road reconstruction
Time Limit: 2000MS |
|
Memory Limit: 131072K |
Total Submissions: 7971 |
|
Accepted: 2294 |
Description
Ikki is the king of a small Country–phoenix, Phoenix are so small that there are only one city that's 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 was that transportation speed is too slow.
Since Ikki was an ACM/ICPC contestant before, he realized the 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'll reconstruct some roads in this transportation network, to make those roads AFFO RD higher capacity in transportation. But unfortunately, the country of Phoenix isn't so rich in GDP that there was only enough money to rebuild one road. Ikki wants to find such roads so if reconstructed, the total capacity of transportation would increase.
He thought this problem for a loooong time but cannot get it. So he gave the problem to Frkstyc and who puts it in the This POJ Monthly contest for your to solve. Can you solve it for Ikki?
Input
The input contains exactly one test case.
The first line of the test case contains 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 the there is a road from city A to City B with a T Ransportation capacity of C (0≤a, b < n, c≤100). All the roads is directed.
Cities was numbered from 0 to n−1, the city which can product goods was numbered 0, and the capital is numbered n−1.
Output should output one line consisting of only one integer k, denoting that there is K roads, reconstructing each of which would increase the network transportation capacity.
Sample Input
2 1
0) 1 1
Sample Output
1
————————————————————————————————
The main idea is that, to find such a number of edges, is to increase the capacity of the edge, can make the maximum rheological
Idea: On the determination of the critical edge of the maximum flow, first we run the maximum flow. Then you cannot enumerate each edge and then run the maximum flow, so it is too slow. We can know that the key must be full flow, and from the source point to it and it to the meeting point will be able to find the augmented path, so we first DFS find out where the source and sink can flow to which point, and then enumerate each edge to determine whether the full stream and the source point sinks can be reached
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include < algorithm> #include <cmath> #include <map> #include <set> #include <stack> #include <queue
> #include <vector> #include <bitset> using namespace std;
#define LL Long Long const int INF = 0X3F3F3F3F;
#define MAXN. struct node {int u, V, next, Cap;} EDGE[MAXN*MAXN];
int NT[MAXN], S[MAXN], D[MAXN], VISIT[MAXN],VIS[MAXN];
int A[MAXN],B[MAXN];
int cnt;
void init () {cnt = 0;
Memset (S,-1, sizeof (s));
} void Add (int u, int v, int c) {edge[cnt].u = u;
EDGE[CNT].V = v;
Edge[cnt].cap = C;
Edge[cnt].next = S[u];
S[u] = cnt++;
edge[cnt].u = v;
EDGE[CNT].V = u;
Edge[cnt].cap = 0;
Edge[cnt].next = S[v];
S[V] = cnt++;
} bool BFS (int ss, int ee) {memset (d, 0, sizeof D);
D[SS] = 1;
queue<int>q;
Q.push (ss); while (!q.empty ()) {int pre = Q.front ();
Q.pop ();
for (int i = s[pre]; ~i; i = edge[i].next) {int v = EDGE[I].V;
if (Edge[i].cap > 0 &&!d[v]) {D[v] = D[pre] + 1;
Q.push (v);
}}} return D[ee]; } int DFS (int x, int. exp, int ee) {if (x = = ee| |!
EXP) return exp;
int temp,flow=0;
for (int i = nt[x]; ~i; i = Edge[i].next, nt[x] = i) {int v = EDGE[I].V; if (d[v] = = D[x] + 1&& (temp = (DFS (V, MIN (exp, edge[i].cap), ee))) > 0) {edge[i].cap-= Te
mp
edge[i ^ 1].cap + = temp;
Flow + = temp;
Exp-= temp;
if (!exp) break;
}} if (!flow) d[x] = 0;
return flow;
} int dinic_flow (int ss,int ee) {int ans = 0;
while (BFS (SS, ee)) {for (int i = 0; I <=ee; i++) nt[i] = s[i];
ans+= DFS (ss, INF, EE);
} return ans; } void Dfs1 (int n) {for (int i=s[n]; ~i; i=edge[i].next) {if (edge[i].cap>0&&!a[edge[i].v]) {a[edge[
I].v]=1;
Vis[edge[i].v]=1;
DFS1 (EDGE[I].V);
vis[edge[i].v]=0; }}}} void dfs2 (int n) {for (int i=s[n]; ~i; i=edge[i].next) {if (edge[i^1].cap>0&&!b[edg
E[I].V]) {b[edge[i].v]=1;
Vis[edge[i].v]=1;
DFS2 (EDGE[I].V);
vis[edge[i].v]=0;
}}} int main () {int n,m,u,v,c;
while (~SCANF ("%d%d", &n,&m)) {init ();
for (int i=0; i<m; i++) {scanf ("%d%d%d", &u,&v,&c);
Add (U,V,C);
} int Ans=dinic_flow (0,n-1);
memset (a,0,sizeof a);
memset (b,0,sizeof b);
A[0]=1;
B[n-1]=1;
DFS1 (0);
DFS2 (n-1);
int k=0; for (int i=0;i<cnt;i+=2) {if (edge[i].cap==0&&a[edge[I].u]==1&&b[edge[i].v]==1) k++;
} printf ("%d\n", K);
} return 0; }