POJ 1637 Sightseeing tour

Sightseeing tour
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 3133   Accepted: 1266

Description

The city executive board in Lund wants to construct a sightseeing tour by bus in Lund, so that tourists can see every corner of the beautiful city. They want to construct the tour so that every street in the city is visited exactly once. The bus should also start and end at the same junction. As in any city, the streets are either one-way or two-way, traffic rules that must be obeyed by the tour bus. Help the executive board and determine if it's possible to construct a sightseeing tour under these constraints.

Input

On the first line of the input is a single positive integer n, telling the number of test scenarios to follow. Each scenario begins with a line containing two positive integers m and s, 1 <= m <= 200,1 <= s <= 1000 being the number of junctions and streets, respectively. The following s lines contain the streets. Each street is described with three integers, xi, yi, and di, 1 <= xi,yi <= m, 0 <= di <= 1, where xi and yi are the junctions connected by a street. If di=1, then the street is a one-way street (going from xi to yi), otherwise it's a two-way street. You may assume that there exists a junction from where all other junctions can be reached.

Output

For each scenario, output one line containing the text "possible" or "impossible", whether or not it's possible to construct a sightseeing tour.

Sample Input

4
5 8
2 1 0
1 3 0
4 1 1
1 5 0
5 4 1
3 4 0
4 2 1
2 2 0
4 4
1 2 1
2 3 0
3 4 0
1 4 1
3 3
1 2 0
2 3 0
3 2 0
3 4
1 2 0
2 3 1
1 2 0
3 2 0

Sample Output

possible
impossible
impossible
possible

Source

Northwestern Europe 2003

 

#include<stdio.h>

#include<string.h>

#include<algorithm>

using namespace std;

int n,m,flow[250][250],dist[250],gap[250];//注意范围

int deg[250],total;

int find_path(int p, int limit = 0x3f3f3f3f)

{

    if (p == n - 1) return limit;

    for (int i = 0; i < n; ++i)

    {

        if (dist[p] == dist[i] + 1 && flow[p][i] > 0)

        {

            int t = find_path(i, min(limit, flow[p][i]));

            if (t < 0) return t;

            if (t > 0)

            {

                flow[p][i] -= t;

                flow[i][p] += t;

                return t;

            }

        }

    }

    int label = n;

    for (int i = 0; i < n; ++i) if (flow[p][i] > 0)  label = min(label, dist[i] + 1);

    if (--gap[dist[p]] == 0 || dist[0] >= n) return -1;

    ++gap[dist[p] = label];

    return 0;

}

int iSAP()

{

    gap[0] = n;

    int maxflow = 0,t = 0;

    while ((t = find_path(0)) >= 0)  maxflow += t;

    return maxflow;

}

int main()

{

    int T;

    scanf("%d",&T);

    while(T--)

    {

        memset(flow,0,sizeof(flow));

        memset(gap,0,sizeof(gap));

        memset(dist,0,sizeof(dist));//注意初始化

        memset(deg,0,sizeof(deg));

        scanf("%d%d", &n,&m);

        for(int i=0;i<m;++i)

        {

            int x, y, c;

            scanf("%d%d%d", &x, &y, &c);

            deg[x]++;

            deg[y]--;

            if(c==0) flow[x][y]++;

        }

        bool flag=true;

        for(int i=1;i<=n;i++)

        {

            if(abs(deg[i])%2==1)

            {

                flag=false;

                break;

            }

            else deg[i]=deg[i]/2;

        }

        if(flag)

        {

            total=0;

            for(int i=1;i<=n;i++)

            {

                if(deg[i]<0)  flow[i][n+1]=-deg[i];

                else  if(deg[i]>0) flow[0][i]=deg[i],total+=deg[i];

            }

            n=n+2;

            if(iSAP()==total) printf("possible/n");

            else  printf("impossible/n");

        }

        else printf("impossible/n");

    }

    return 0;

}

你可能感兴趣的:(c,Integer,input,Path,each,output)