码迷,mamicode.com
首页 > 其他好文 > 详细

POJ1637:Sightseeing tour(混合图的欧拉回路)

时间:2019-01-06 21:53:40      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:不能   i++   mit   body   main   end   sub   设置   NPU   

Sightseeing tour

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 10581   Accepted: 4466

题目链接:http://poj.org/problem?id=1637

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

题意:

输入包含多组数据,然后给出一个混合图(既有有向边也有无向边),现在问是否能从一个起点出发,经过每一条边一次又重新回到这一个点。

PS:双向边也只能经过一次。

 

题解:

如果这个题就为单纯的有向图或者无向图就好办了,我们只需要判断每个点的入度和出度就ok了,但这是混合图....反正我开始并没想到用最大流= =

对于欧拉路径,最不可少的就是每个点入度和出度的度数了,我们还是可以统计每个点的入度、出度度数。

假设对于一个点来说,如果将一条边反向,入度和出度的变化之和为0。我们就可以利用这一性质来判断可行性。

假定现在存在了欧拉路径,说明我们至少有一种将一些边反向的方案能够满足条件。

对于双向边而言,我们就先假定任意一个方向然后再来统计度数并且加边,有向边边权为0,意即流不能从这条边通过。

我们假定最大流经过的边就是能够反向的边,那么我们将每条“有向边”(其实是无向边)的容量设置为1即可。

对于源点和汇点,假如一个点当前的入度大于出度,那么源点与之相连,边权为(入度-出度)/2(意即需要将多少边反向);对于汇点也同理。

最后我们跑最大流看看能否满流就行了。

思路还是很巧妙的,主要就在于定无向为有向,再利用最大流来考虑将边反向。

 

代码如下:

 

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#define s 0
#define t n+1
#define INF 1e9
using namespace std;
typedef long long ll;
const int N = 205,M = 1005;
int n,m,T,tot;
int head[N],in[N],out[N],d[N];
struct Edge{
    int v,next,c;
}e[M<<2];
void adde(int u,int v,int c){
    e[tot].v=v;e[tot].next=head[u];e[tot].c=c;head[u]=tot++;
    e[tot].v=u;e[tot].next=head[v];e[tot].c=0;head[v]=tot++;
}
bool bfs(int S,int T){
    memset(d,0,sizeof(d));d[S]=1;
    queue <int > q;q.push(S);
    while(!q.empty()){
        int u=q.front();q.pop();
        for(int i=head[u];i!=-1;i=e[i].next){
            int v=e[i].v;
            if(!d[v] && e[i].c>0){
                d[v]=d[u]+1;
                q.push(v);
            }
        }
    }
    return d[T]!=0;
}
int dfs(int S,int a){
    int flow=0,f;
    if(S==t || a==0) return a;
    for(int i=head[S];i!=-1;i=e[i].next){
        int v=e[i].v;
        if(d[v]!=d[S]+1) continue ;
        f=dfs(v,min(a,e[i].c));
        if(f){
            e[i].c-=f;
            e[i^1].c+=f;
            flow+=f;
            a-=f;
            if(a==0) break;
        }
    }
    if(!flow) d[S]=-1;
    return flow;
}
int Dinic(){
    int max_flow=0;
    while(bfs(0,t)){
        max_flow+=dfs(0,INF);
    }
    return max_flow;
}
int main(){
    scanf("%d",&T);
    while(T--){
        tot=0;memset(head,-1,sizeof(head));
        memset(in,0,sizeof(in));memset(out,0,sizeof(out));
        scanf("%d%d",&n,&m);
        for(int i=1;i<=m;i++){
            int u,v,op;
            scanf("%d%d%d",&u,&v,&op);
            if(op==0) adde(u,v,1);
            else adde(u,v,0);
            in[v]++;out[u]++;
        }
        int flag=1,sum=0;
        for(int i=1;i<=n;i++){
            int now = in[i]-out[i];
            if(now&1) flag=0;
            if(now>0) adde(i,t,now/2);
            if(now<=0) adde(s,i,-now/2),sum+=now/2;
        }
        sum=-sum;
        if(!flag){
            puts("impossible");
            continue ;
        }
        int flow = Dinic();
        if(flow==sum) puts("possible");
        else puts("impossible");
    }
    return 0;
}

 

POJ1637:Sightseeing tour(混合图的欧拉回路)

标签:不能   i++   mit   body   main   end   sub   设置   NPU   

原文地址:https://www.cnblogs.com/heyuhhh/p/10230367.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!