POJ 1637 Sightseeing tour

Sightseeing tour

Time Limit: 1000ms
Memory Limit: 10000KB
This problem will be judged on PKU. Original ID: 1637
64-bit integer IO format: %lld      Java class name: Main
 
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

 
解题:传说中的混合欧拉回路。
 
混合欧拉回路建图???????无视有向边,但是有向边的度还是要计入的。把无向边视为有向边。统计个点的入度与出度的差。出的话就减,入的话就加。
 
先判存在欧拉回路的条件。各顶点没有度差为奇的点,再继续最大流。否则直接判死刑。最大流?现在各点的度差都为偶数了。只要将各点合适的边反向,也许存在一种可能,使得各点的入度等于出度,啊哈,入度等于出度那么一定有这样的欧拉回路了。如何判断这种可能性是否存在?最大流是否满流,就能判断这种可能性。还没建网络流图呢。边容量都是度差的一半。负的取绝对值。
然后度差负的与源点连接,度差为正数的与汇点连接。
 
  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <cmath>
  5 #include <algorithm>
  6 #include <climits>
  7 #include <vector>
  8 #include <queue>
  9 #include <cstdlib>
 10 #include <string>
 11 #include <set>
 12 #include <stack>
 13 #define LL long long
 14 #define pii pair<int,int>
 15 #define INF 0x3f3f3f3f
 16 using namespace std;
 17 const int maxn = 250;
 18 struct arc {
 19     int to,flow,next;
 20     arc(int x = 0,int y = 0,int z = -1) {
 21         to = x;
 22         flow = y;
 23         next = z;
 24     }
 25 };
 26 int n,m,tot,S,T;
 27 arc e[20000];
 28 int head[maxn],d[maxn],ind[maxn],cur[maxn];
 29 void add(int u,int v,int flow) {
 30     e[tot] = arc(v,flow,head[u]);
 31     head[u] = tot++;
 32     e[tot] = arc(u,0,head[v]);
 33     head[v] = tot++;
 34 }
 35 queue<int>q;
 36 bool bfs(){
 37     memset(d,-1,sizeof(d));
 38     while(!q.empty()) q.pop();
 39     d[S] = 1;
 40     q.push(S);
 41     while(!q.empty()){
 42         int u = q.front();
 43         q.pop();
 44         for(int i = head[u]; ~i; i = e[i].next){
 45             if(e[i].flow && d[e[i].to] < 0){
 46                 d[e[i].to] = d[u] + 1;
 47                 q.push(e[i].to);
 48             }
 49         }
 50     }
 51     return d[T] > -1;
 52 }
 53 int dfs(int u,int low){
 54     if(u == T) return low;
 55     int tmp = 0,a;
 56     for(int &i = cur[u]; ~i; i = e[i].next){
 57         if(e[i].flow && d[e[i].to] == d[u]+1 && (a = dfs(e[i].to,min(low,e[i].flow)))){
 58             tmp += a;
 59             e[i].flow -= a;
 60             e[i^1].flow += a;
 61             low -= a;
 62             if(!low) break;
 63         }
 64     }
 65     if(tmp == 0) d[u] = -1;
 66     return tmp;
 67 }
 68 int dinic(){
 69     int tmp = 0;
 70     while(bfs()){
 71         for(int i = S; i <= T; i++)
 72             cur[i] = head[i];
 73         tmp += dfs(S,INF);
 74     }
 75     return tmp;
 76 }
 77 int main() {
 78     int t,u,v,w,i,flow,sum;
 79     scanf("%d",&t);
 80     while(t--) {
 81         scanf("%d %d",&n,&m);
 82         memset(head,-1,sizeof(head));
 83         memset(ind,0,sizeof(ind));
 84         sum = S = tot = 0;
 85         flow = INF;
 86         T = n+1;
 87         for(i = 1; i <= m; i++) {
 88             scanf("%d %d %d",&u,&v,&w);
 89             ind[u]--;
 90             ind[v]++;
 91             if(!w) add(u,v,1);
 92         }
 93         for(i = 1; i <= n; i++)
 94             if(ind[i]&1) break;
 95         if(i > n) {
 96             for(i = 1; i <= n; i++) {
 97                 if(ind[i] < 0) add(S,i,(-ind[i])>>1);
 98                 if(ind[i] > 0) {
 99                     add(i,T,ind[i]>>1);
100                     sum += ind[i]>>1;
101                 }
102             }
103             flow = dinic();
104         }
105         if(flow == sum) puts("possible");
106         else puts("impossible");
107     }
108     return 0;
109 }
View Code
原文地址:https://www.cnblogs.com/crackpotisback/p/3977149.html