zoukankan      html  css  js  c++  java
  • POJ1637:Sightseeing tour(混合图的欧拉回路)

    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;
    }
  • 相关阅读:
    Android的消息机制
    AndroidVolley框架的使用
    Android事件分发机制浅谈(三)源码分析(View篇)
    Java 编程下简介 Class 与类加载
    [ZJOI2007]最大半连通子图
    [HNOI2012]永无乡
    [HEOI2016/TJOI2016]排序
    CSS 条件缩放图片
    CSS图片垂直居中
    最近写 ASP.NET 中出现的错误 & 实现DataTable和DataSet类型的客户端/服务器端自动转换
  • 原文地址:https://www.cnblogs.com/heyuhhh/p/10230367.html
Copyright © 2011-2022 走看看