您的位置:首页 > 其它

poj 1637 Sightseeing tour

2013-08-29 16:38 295 查看
Time Limit: 1000MSMemory Limit: 10000K
Total Submissions: 6706Accepted: 2773
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 【题目大意】 混合图欧拉回路
#include <iostream>
#include <cstdio>
#include <algorithm>
//#include <cstring>
//#include <cmath>
#include <queue>
#include <set>
#define INF 2000000000
#define maxn 210
#define       min(a, b) a < b ?       a : b
using namespace std;

int n, m;
int dis[maxn];
int pre[maxn];
int front[maxn];
int gap[maxn];
int cur[maxn];
int du[maxn];
int index;

struct Edge{
int v;
int w;
int next;
}e[maxn*maxn];

void       init(){

memset( pre, -1, sizeof(pre)       );
memset( du, 0,       sizeof(du)       );
index = 0;
}

void Add( int u, int       v, int w       ){
e[index].v =       v;
e[index].w =       w;
e[index].next =       pre[u];
pre[u] =       index++;

e[index].v       = u;
e[index].w       = 0;
e[index].next = pre[v];
pre[v] = index++;
}

int maxflow_sap( int s, int t       ){
memset( dis, 0,       sizeof(dis)       );
memset( gap, 0,       sizeof(gap)       );
memset( front, -1, sizeof(front)       );
int       i;
for ( i       = 0; i <= t; i++       ){
cur[i] =       pre[i];
}
int flow = INF, u       = s, maxflow = 0;
gap[s] = t + 1;

while ( dis[s] <       t+1       ){
for ( i = cur[u]; i       != -1; i = e[i].next       ){
if ( dis[u] ==       dis[e[i].v] + 1       && e[i].w > 0       ){
break;
}
}
cur[u]       =       i;

if ( cur[u] !=       -1       ){
flow       = min( flow, e[i].w       );
front[e[i].v]       =       u;
u       =       e[i].v;
if ( u == t       ){
maxflow       +=       flow;
for ( i = front[t];       i != -1; i = front[i]       ){
e[cur[i]].w       -=       flow;
e[cur[i]^1].w +=       flow;
}
flow       =       INF;
u       =       s;
}
}
else{
int mindis = t+2;
for ( i = pre[u]; i       != -1; i = e[i].next       ){
if ( mindis >       dis[e[i].v] + 1       && e[i].w > 0       ){
mindis       = dis[e[i].v] +       1;
cur[u]       =       i;
}
}
gap[dis[u]]--;
if ( !gap[dis[u]]       ){
break;
}
dis[u]       =       mindis;
gap[mindis]++;
if ( u != s       ){
u       =       front[u];
}
}
}
return       maxflow;
}

int       main(){
int       T, i;
scanf("%d",       &T);
while ( T--       ){
init();
scanf("%d%d", &n, &m);
for ( i = 0; i < m; i++       ){
int a, b,       c;
scanf("%d%d%d", &a, &b, &c);
du[a]++;
du--;
[b]if ( !c && a != b       ){
Add(       a, b, 1       );
}
}
int flag = 0, x = 0;;
for ( i = 1; i <= n; i++       ){
if ( du[i] %2       ){
flag       = 1;
break;
}

if ( du[i] >       0       ){
Add(       0, i, du[i]/2       );
x       += du[i]/2;
}
else if (       du[i] < 0       ){
Add(       i, n+1, -du[i]/2       );
//x +=       -du[i]/2;
}
}
if ( !flag && x ==       maxflow_sap( 0, n+1 )       ){
printf("possible\n");
}
else{
printf("impossible\n");
}
}
return 0;
}
来自为知笔记(Wiz)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: