Sightseeing tour 【混合图欧拉回路】
作者:互联网
题目链接:http://poj.org/problem?id=1637
Sightseeing tourTime Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions:10837 | Accepted: 4560 |
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. 题目大意:给出一张混合图(有双向边以及单向边),求是否存在欧拉路径 解题思路: 1.首先推荐两篇博客:https://blog.csdn.net/pi9nc/article/details/12223693 以及 https://blog.csdn.net/commonc/article/details/52442882 2.说说自己对混合图欧拉回路的理解。因为有向边的存在,所以一定是需要满足每个点的入度等于出度,但是无向边,我们可以去进行两种选择,来达到寻找欧拉回路的目的。所以首先我们要对无向边进行定向处理,即随意的给无向边一个方向。这样转化成了有向图。 3.转化成有向图之后我们要计算每个点的出入以及出度,若其奇偶性不同,很明显无法通过自调整来实现入度与出度的相等,这幅图就一定不存在欧拉回路。 4.自调整的过程就是跑最大流的过程。对于建图:无向边定向并且容量为1,有向边不可以加入建图中,因为有向边是不可以进行自调整的。对于每个出度大于入度的点,我们从源点向该点连边,边权为出度与入度之差的一半,对于每个入度大于出入的点,从该点向汇点连边,边权为入度与出度之差的一半。从源点到汇点跑一遍最大流。 5.若最大流等于从源点出发的边权之和,则存在欧拉回路。 代码如下:1 #include<stdio.h> 2 #include<string.h> 3 #include<queue> 4 #define mem(a, b) memset(a, b, sizeof(a)) 5 const int MAXN = 210; 6 const int MAXM = 1100; 7 const int inf = 0x3f3f3f3f; 8 using namespace std; 9 10 int n, m, st, ed, tot; //n个点 m条边(有向 + 无向) 11 int out[MAXN], in[MAXN], head[MAXN], cnt; 12 queue<int> Q; 13 int dep[MAXN]; 14 15 struct Edge 16 { 17 int to, next, flow; 18 }edge[6 * MAXM]; 19 20 void add(int a, int b, int c) 21 { 22 cnt ++; 23 edge[cnt].to = b; 24 edge[cnt].next = head[a]; 25 edge[cnt].flow = c; 26 head[a] = cnt; 27 } 28 29 int build() 30 { 31 for(int i = 1; i <= n; i ++) 32 { 33 if((out[i] + in[i]) % 2) //如果存在有点的出入跟入度的奇偶性不同 那么无论如何调节都无法做到入度和出度相等 34 return 0; 35 if(out[i] > in[i]) 36 { 37 int x = (out[i] - in[i]) / 2; 38 tot += x; 39 add(st, i, x); 40 add(i, st, 0); 41 } 42 else if(in[i] > out[i])//入度与出度相等的情况 加不加边无影响 43 { 44 int x = (in[i] - out[i]) / 2; 45 add(i, ed, x); 46 add(ed, i, 0); 47 } 48 } 49 return 1; 50 } 51 52 int bfs() 53 { 54 if(st == ed) 55 return 0; 56 mem(dep, -1); 57 dep[st] = 1; 58 Q.push(st); 59 while(!Q.empty()) 60 { 61 int index = Q.front(); 62 Q.pop(); 63 for(int i = head[index]; i != -1; i = edge[i].next) 64 { 65 int to = edge[i].to; 66 if(edge[i].flow > 0 && dep[to] == -1) 67 { 68 dep[to] = dep[index] + 1; 69 Q.push(to); 70 } 71 } 72 } 73 return dep[ed] != -1; 74 } 75 76 int dfs(int now, int zx) 77 { 78 if(now == ed) 79 return zx; 80 for(int i = head[now]; i != -1; i = edge[i].next) 81 { 82 int to = edge[i].to; 83 if(dep[to] == dep[now] + 1 && edge[i].flow > 0) 84 { 85 int flow = dfs(to, min(zx, edge[i].flow)); 86 if(flow > 0) 87 { 88 edge[i].flow -= flow; 89 edge[i ^ 1].flow += flow; 90 return flow; 91 } 92 } 93 } 94 return -1; 95 } 96 97 int dinic() 98 { 99 int ans = 0; 100 while(bfs()) 101 { 102 while(1) 103 { 104 int inc = dfs(st, inf); 105 if(inc == -1) 106 break; 107 ans += inc; 108 } 109 } 110 return ans; 111 } 112 113 int main() 114 { 115 int T; 116 scanf("%d", &T); 117 while(T --) 118 { 119 mem(out, 0), mem(in, 0), mem(head, -1); 120 cnt = -1, tot = 0; 121 scanf("%d%d", &n, &m); 122 st = 0, ed = n + 1; 123 for(int i = 1; i <= m; i ++) 124 { 125 int a, b, op; 126 scanf("%d%d%d", &a, &b, &op); 127 out[a] ++ ,in[b] ++; 128 if(op == 0) //只有无向边才能自调节 129 { 130 add(a, b, 1); 131 add(b, a, 0); 132 } 133 } 134 if(build()) 135 { 136 int maxflow = dinic(); 137 if(maxflow == tot) //最大流等于源点出去的边满流 138 printf("possible\n"); 139 else 140 printf("impossible\n"); 141 } 142 else 143 printf("impossible\n"); 144 } 145 return 0; 146 }View Code
标签:int,入度,flow,st,dep,tour,edge,欧拉,Sightseeing 来源: https://www.cnblogs.com/yuanweidao/p/11523359.html