Codeforces 107A. Dorm Water Supply 搜图

A. Dorm Water Supply
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

The German University in Cairo (GUC) dorm houses are numbered from 1 to n. Underground water pipes connect these houses together. Each pipe has certain direction (water can flow only in this direction and not vice versa), and diameter (which characterizes the maximal amount of water it can handle).

For each house, there is at most one pipe going into it and at most one pipe going out of it. With the new semester starting, GUC student and dorm resident, Lulu, wants to install tanks and taps at the dorms. For every house with an outgoing water pipe and without an incoming water pipe, Lulu should install a water tank at that house. For every house with an incoming water pipe and without an outgoing water pipe, Lulu should install a water tap at that house. Each tank house will convey water to all houses that have a sequence of pipes from the tank to it. Accordingly, each tap house will receive water originating from some tank house.

In order to avoid pipes from bursting one week later (like what happened last semester), Lulu also has to consider the diameter of the pipes. The amount of water each tank conveys should not exceed the diameter of the pipes connecting a tank to its corresponding tap. Lulu wants to find the maximal amount of water that can be safely conveyed from each tank to its corresponding tap.

Input

The first line contains two space-separated integers n and p (1 ≤ n ≤ 1000, 0 ≤ p ≤ n) — the number of houses and the number of pipes correspondingly.

Then p lines follow — the description of p pipes. The i-th line contains three integers ai bi di, indicating a pipe of diameter di going from house ai to house bi (1 ≤ ai, bi ≤ n, ai ≠ bi, 1 ≤ di ≤ 106).

It is guaranteed that for each house there is at most one pipe going into it and at most one pipe going out of it.

Output

Print integer t in the first line — the number of tank-tap pairs of houses.

For the next t lines, print 3 integers per line, separated by spaces: tankitapi, and diameteri, where tanki ≠ tapi (1 ≤ i ≤ t). Here tankiand tapi are indexes of tank and tap houses respectively, and diameteri is the maximum amount of water that can be conveyed. All thet lines should be ordered (increasingly) by tanki.

Sample test(s)
input
3 2
1 2 10
2 3 20
output
1
1 3 10
input
3 3
1 2 20
2 3 10
3 1 5
output
0
input
4 2
1 2 60
3 4 50
output
2
1 2 60
3 4 50


有m条边,n个点,每个点都至多有一个入水口和一个出水口,要求找到所有的水塔以及路径中最短的边。

#include <iostream>
#include <cstring>

using namespace std;

struct ANS{
    int u;
    int v;
    int d;
}ans[1111];

int cnt;
int n,m;
int next[1111]={0};
int cost[1111]={0};
int pre[1111]={0};
bool v[1111]={0};

void dfs(int i);

void dfs(int i,int flow)
{
    v[i]=true;
    if (next[i]==0)
    {
        ans[cnt].d=flow;
        ans[cnt].v=i;
        return;
    }
    int j=next[i];
    if (cost[j]<flow&&cost[j]!=0)
    {
        dfs(j,cost[j]);
    }
    else
    {
        dfs(j,flow);
    }
}

int main()
{
    cnt=0;
    cin>>n>>m;
    for (int i=1;i<=m;i++)
    {
        int a,b,k;
        cin>>a>>b>>k;
        next[a]=b;
        pre[b]=a;
        cost[a]=k;
    }
    for (int i=1;i<=n;i++)
    {
        if (!v[i]&&pre[i]==0&&next[i]!=0)
        {
            ans[cnt].u=i;
            dfs(i,cost[i]);
            cnt++;
        }
    }
    cout<<cnt<<endl;
    for (int i=0;i<cnt;i++)
    {
        cout<<ans[i].u<<" "<<ans[i].v<<" "<<ans[i].d<<endl;
    }
    return 0;
}




你可能感兴趣的:(题解,图论,DFS,codeforces)