poj 3204 Ikki's Story I - Road Reconstruction(最大流割边)

Ikki's Story I - Road Reconstruction
Time Limit: 2000MS   Memory Limit: 131072K
Total Submissions: 5586   Accepted: 1542

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 NM (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 abc, which means that there is a road from city a to city b with a transportation capacity of c (0 ≤ ab < nc ≤ 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

Source

POJ Monthly--2007.03.04, Ikki

题目:http://poj.org/problem?id=3204

题意:给你一张图,求总共有几条边,满足增加边的容量后能使最大流的值变大

分析:先求最大流,然后做两遍dfs,都是在残余网络上做的,也就是没有满流的变,一个从源点出发,标记能到达的点,一个从汇点出发,标记能到达的点,后者是反向图,然后枚举每条满流的边,看起始点是否被第一个标记了,终点是否被第二个标记了,都标记了就是一条割边了

PS:本来做这题是打算找模板的错误的,结果发现不会做。。。太久没训练网络流了= =,而且模板居然还是没找到错。。。。

大家有兴趣的话可以帮我找找,这个模板在这题上一直错,错的地方时预处理,没有预处理就A了。。。

代码:

#include<cstdio>
#include<iostream>
using namespace std;
const int mm=20000;
const int mn=555;
const int oo=2e9;
int node,src,dest,edge;
int ver[mm],flow[mm],next[mm];
int head[mn],work[mn],h[mn],q[mn],gap[mn],p[mn];
int v1[mn],v2[mn];
void prepare(int _node,int _src,int _dest)
{
    node=_node,src=_src,dest=_dest;
    for(int i=0; i<node; ++i)head[i]=-1;
    edge=0;
}
void addedge(int u,int v,int c)
{
    ver[edge]=v,flow[edge]=c,next[edge]=head[u],head[u]=edge++;
    ver[edge]=u,flow[edge]=0,next[edge]=head[v],head[v]=edge++;
}
void Isap_Pre()
{
    int i,u,v,l,r=0;
    for(i=0; i<node; ++i)h[i]=-1,gap[i]=0;
    gap[h[q[r++]=dest]=0]=1;
    for(l=0; l<r; ++l)
        for(i=head[u=q[l]]; i>=0; i=next[i])
            if(flow[i^1]&&h[v=ver[i]]<0)
                ++gap[h[q[r++]=v]=h[u]+1];
}
int Isap_flow()
{
    int i,u,ret=0,tmp,minh;
    Isap_Pre();
    for(i=0; i<node; ++i)work[i]=head[i];
    u=src;
    while(h[src]<node)
    {
        if(u==dest)
        {
            for(i=src,tmp=oo;i!=dest;i=ver[work[i]])
                tmp=min(tmp,flow[work[i]]);
            for(i=src;i!=dest;i=ver[work[i]])
                flow[work[i]]-=tmp,flow[work[i]^1]+= tmp;
            ret+=tmp,u=src;
        }
        int &e=work[u];
        for(;e>=0;e=next[e])
            if(flow[e]&&h[u]==h[ver[e]]+1)break;
        if(e>=0)p[u=ver[e]]=e^1;
        else
        {
            if(--gap[h[u]]==0)break;
            work[u]=head[u],minh=node;
            for(i=head[u];i>=0;i=next[i])
                if(flow[i])minh=min(minh,h[ver[i]]);
            ++gap[h[u]=minh+1];
            if(u!=src)u=ver[p[u]];
        }
    }
    return ret;
}
void dfspre(int u)
{
    v1[u]=1;
    for(int i=head[u],v;i>=0;i=next[i])
        if(!v1[v=ver[i]]&&flow[i])dfspre(v);
}
void dfsbak(int u)
{
    v2[u]=1;
    for(int i=head[u],v;i>=0;i=next[i])
        if(!v2[v=ver[i]]&&flow[i^1])dfsbak(v);
}
int main()
{
    int i,j,k,n,m,ans;
    while(scanf("%d%d",&n,&m)!=-1)
    {
        prepare(n,0,n-1);
        while(m--)
        {
            scanf("%d%d%d",&i,&j,&k);
            addedge(i,j,k);
        }
        Isap_flow();
        for(i=0;i<n;++i)v1[i]=v2[i]=0;
        dfspre(src);
        dfsbak(dest);
        for(ans=i=0;i<edge;i+=2)
            if(!flow[i]&&v1[ver[i^1]]&&v2[ver[i]])++ans;
        printf("%d\n",ans);
    }
    return 0;
}
/*
4 3
0 1 34
1 2 0
2 3 24
*/


你可能感兴趣的:(poj 3204 Ikki's Story I - Road Reconstruction(最大流割边))