【poj3204】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

2 1
0 1 1

Sample Output

1

Source

POJ Monthly–2007.03.04, Ikki


找【如果增加这个边的容量就会导致最大流增大的边】的数量。

残量网络里dfs一下…

注意从终点dfs时,相当于残量网络里反向dfs只能走有流量的边,也就是l[i ^ 1].d不为0。

代码里注释内和外的代码都对

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
using namespace std;

const int INF = 1000000010;
const int SZ = 1000010;

int head[SZ],nxt[SZ],tot = 1,s,e;

struct edge{
    int f,t,d;
}l[SZ];

void build(int f,int t,int d)
{
    l[++ tot].t = t;
    l[tot].d = d;
    l[tot].f = f;
    nxt[tot] = head[f];
    head[f] = tot;
}

void insert(int f,int t,int d)
{
    build(f,t,d); build(t,f,0);
}

queue<int> q;
int deep[SZ];

bool bfs()
{
    memset(deep,0,sizeof(deep));
    deep[s] = 1;
    while(q.size()) q.pop();
    q.push(s);
    while(q.size())
    {
        int f = q.front(); q.pop();
        for(int i = head[f];i;i = nxt[i])
        {
            int v = l[i].t;
            if(!deep[v] && l[i].d)
            {
                q.push(v);
                deep[v] = deep[f] + 1;
                if(v == e) return true;
            }
        }
    }
    return false;
}

int dfs(int u,int flow)
{
    if(u == e || flow == 0) return flow;
    int rest = flow;
    for(int i = head[u];i;i = nxt[i])
    {
        int v = l[i].t;
        if(deep[v] == deep[u] + 1 && l[i].d)
        {
            int f = dfs(v,min(rest,l[i].d));
            if(f > 0)
            {
                l[i].d -= f;
                l[i ^ 1].d += f;
                rest -= f;
                if(rest == 0) break;
            }
            else deep[v] = 0;
        }
    }
    if(flow - rest == 0) deep[u] = 0;
    return flow - rest;
}

int dinic()
{
    int ans = 0;
    while(bfs())
    {
        int tmp = dfs(s,INF);
        if(!tmp) break;
        ans += tmp;
    }
    return ans;
}

int vis[SZ];

void dfs1(int u)
{
    vis[u] = 1;
    for(int i = head[u];i;i = nxt[i])
    {
        int v = l[i].t;
        if(!vis[v] && l[i].d)
            dfs1(v);
    }
}

void dfs2(int u)
{
    vis[u] = 2;
    for(int i = head[u];i;i = nxt[i])
    {
        int v = l[i].t;
        if(!vis[v] && l[i ^ 1].d)
            dfs2(v);
    }
}


int ff[SZ],tt[SZ],dd[SZ];

int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i = 1;i <= m;i ++)
    {
        scanf("%d%d%d",&ff[i],&tt[i],&dd[i]);
        ff[i] ++; tt[i] ++;
        insert(ff[i],tt[i],dd[i]);
    }
    s = 1,e = n;
    dinic();

    dfs1(s);    dfs2(e);


    int ans = 0;
/*  for(int u = 1;u <= n;u ++)
    {
        if(vis[u] == 1)
        {
            for(int i = head[u];i;i = nxt[i])
            {
                int v = l[i].t;
                if(vis[v] == 2 && !l[i].d && i % 2 == 0)
                    ans ++;
            }
        }
    }*/

    for(int i = 2;i <= tot;i += 2)
    {
        if(!l[i].d && vis[l[i].f] * vis[l[i].t] == 2)
            ans ++;
    }

    printf("%d",ans);

    return 0;
}

/*

4 5
0 1 40
0 3 20
1 3 20
1 2 30
2 3 10

*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值