首页 > 专题系列 > Java解POJ > POJ 1637 Sightseeing tour [解题报告] Java
2013
11-10

POJ 1637 Sightseeing tour [解题报告] Java

Sightseeing tour

问题描述 :

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.

输入:

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.

输出:

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

样例输入:

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

样例输出:

possible
impossible
impossible
possible

解题代码:

//* @author 
import java.util.*; 
import java.util.concurrent.ArrayBlockingQueue; 


public class Main { 
    static int N = 205; 
    static int [] degin, degout, deg, pre; 
    static int [][] cap; 

    private static int maxflow(int s, int t, int size) { 
        Queue que = new ArrayBlockingQueue(N); 
        int flow = 0, i, ext; 

        while(true) { 
            for(i = 0;i < size;++ i) 
                pre[i] = -1; 
            pre[s] = s; 
            que.clear(); 
            que.add(s); 
            while(!que.isEmpty()) { 
                int k = que.poll(); 
                for(i = 0;i < size;++ i) { 
                    if(pre[i] == -1 && cap[k][i] > 0) { 
                        pre[i] = k; 
                        if(i == t) break; 
                        que.add(i); 
                    } 
                } 
            } 

            if(pre[t] == -1) break; 

            ext = cap[pre[t]][t]; 
            for(i = t;i != s;i = pre[i]) { 
                if(ext > cap[pre[i]][i]) 
                    ext = cap[pre[i]][i]; 
            } 
            flow += ext; 
            for(i = t;i != s;i = pre[i]) { 
                cap[pre[i]][i] -= ext; 
                cap[i][pre[i]] += ext; 
            } 
        } 
        return flow; 
    } 

    public static void main(String [] args) { 
        Scanner cin = new Scanner(System.in); 
        int n, m, s, a, b, d, i, j, p; 
        degin = new int [N]; 
        degout = new int [N]; 
        deg = new int [N]; 
        pre = new int [N]; 
        cap = new int [N][N]; 

        n = cin.nextInt(); 
        while(n -- > 0) { 
            m = cin.nextInt(); 
            s = cin.nextInt(); 
            for(i = 0;i < m + 2;++ i) { 
                degin[i] = degout[i] = deg[i] = 0; 
                for(j = 0;j < m + 2;++ j) 
                    cap[i][j] = 0; 
            } 
            p = 0; 

            for(i = 0;i < s;++ i) { 
                a = cin.nextInt(); 
                b = cin.nextInt(); 
                d = cin.nextInt(); 
                if(d == 0) { 
                    deg[a]++; 
                    deg[b]++; 
                    cap[0][a]++; 
                    cap[a][b]++; 
                    ++p; 
                } 
                else { 
                    degout[a]++; 
                    degin[b]++; 
                } 
            } 

            Boolean flag = true; 
            for(i = 1;i <= m;++ i) { 
                int k = degin[i] + degout[i] + deg[i]; 
                if(k % 2 == 1 || k / 2 < degin[i] || k / 2 < degout[i]) { 
                    flag = false; 
                    break; 
                } 
                cap[i][m + 1] = k / 2 - degin[i]; 
            } 

            if(flag && maxflow(0, m + 1, m + 2) == p) 
                System.out.println("possible"); 
            else 
                System.out.println("impossible"); 
        } 
    } 
}

  1. 有两个重复的话结果是正确的,但解法不够严谨,后面重复的覆盖掉前面的,由于题目数据限制也比较严,所以能提交通过。已更新算法