poj 3204 Ikki's Story I - Road Reconstruction(最小割,求关键边)

来源:互联网 发布:网络毒鸡汤是什么意思 编辑:程序博客网 时间:2024/04/29 21:26
Ikki's Story I - Road Reconstruction
Time Limit: 2000MS Memory Limit: 131072KTotal Submissions: 6461 Accepted: 1844

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 ofc (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 numberedn − 1.

Output

You should output one line consisting of only one integerK, denoting that there are K roads, reconstructing each of which will increase the network transportation capacity.

Sample Input

2 10 1 1

Sample Output

1
参考http://www.cppblog.com/y346491470/articles/152490.html
AC代码:
#include <iostream>#include <cmath>#include <cstdlib>#include <cstring>#include <cstdio>#include <queue>#include <stack>#include <ctime>#include <vector>#include <algorithm>#define ll __int64using namespace std;const int INF = 1000000000;const int maxn = 10000;struct Edge{    int u, v, cap, flow, next;} et[10000000];vector<int>G[maxn], RG[maxn];int low[maxn], pre[maxn], cnt[maxn], cur[maxn], dis[maxn], eh[maxn];int col[maxn];int s, t, num, n, m;void init(){    memset(eh, -1, sizeof(eh));    num = 0;}void add(int u, int v, int cap, int flow){    Edge e = {u, v, cap, flow, eh[u]};    et[num] = e;    eh[u] = num++;}void addedge(int u, int v, int cap){    add(u, v, cap, 0);    add(v, u, 0, 0);}void isap(int s, int t, int nv){    int u, v, now, flow = 0;    memset(low, 0, sizeof(low));    memset(cnt, 0, sizeof(cnt));    memset(dis, 0, sizeof(dis));    for(u = 0; u <= nv; u++) cur[u] = eh[u];    low[s] = INF, cnt[0] = nv, u = s;    while(dis[s] < nv)    {        for(now = cur[u]; now != -1; now = et[now].next)            if(et[now].cap - et[now].flow && dis[u] == dis[v = et[now].v] + 1) break;        if(now != -1)        {            cur[u] = pre[v] = now;            low[v] = min(low[u], et[now].cap - et[now].flow);            u = v;            if(u == t)            {                for(; u != s; u = et[pre[u]].u)                {                    et[pre[u]].flow += low[t];                    et[pre[u]^1].flow -= low[t];                }                flow += low[t];                low[s] = INF;            }        }        else        {            if(--cnt[dis[u]] == 0) break;            dis[u] = nv, cur[u] = eh[u];            for(now = eh[u]; now != -1; now = et[now].next)                if(et[now].cap - et[now].flow && dis[u] > dis[et[now].v] + 1)                    dis[u] = dis[et[now].v] + 1;            cnt[dis[u]]++;            if(u != s) u = et[pre[u]].u;        }    }}void dfs1(int u){    col[u] = 1;    for(int i = 0; i < (int)G[u].size(); i++)    {        int v = G[u][i];        if(!col[v]) dfs1(v);    }}void dfs2(int u){    col[u] = 2;    for(int i = 0; i < (int)RG[u].size(); i++)    {        int v = RG[u][i];        if(!col[v]) dfs2(v);    }}int main(){    int a, b, c;    while(~scanf("%d%d", &n, &m))    {        init();        s = 0, t = n - 1;        while(m--)        {            scanf("%d%d%d", &a, &b, &c);            addedge(a, b, c);        }        isap(s, t, t + 1);        for(int i = 0; i <= n; i++)        {            G[i].clear();            RG[i].clear();        }        queue<int>Q;        for(int i = 0; i < num; i += 2)        {            if(et[i].cap - et[i].flow)            {                int u = et[i].u, v = et[i].v;                G[u].push_back(v);                RG[v].push_back(u);            }            else Q.push(i);        }        memset(col, 0, sizeof(col));        dfs1(s);        dfs2(t);        int ans = 0;        while(!Q.empty())        {            int i = Q.front();            Q.pop();            int u = et[i].u, v = et[i].v;            if(col[u] ==1 && col[v] == 2) ans++;        }        printf("%d\n", ans);    }    return 0;}


0 0