zoukankan      html  css  js  c++  java
  • POJ 1637 Sightseeing tour (混合图欧拉回路)

    Sightseeing tour

     

    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<cstdio>
    #include<vector>
    #include<queue>
    #include<cstring>
    using namespace std;
    const int INF=0x3f3f3f3f,MAXN=205;
    int in[MAXN],out[MAXN],m,s;
    struct Edge
    {
        int from,to,cap,flow;
        Edge(){}
        Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){};
    };
    struct dinic
    {
        int s,t;
        vector<Edge>edges;
        vector<int>G[MAXN];
        bool vis[MAXN];
        int d[MAXN];
        int cur[MAXN];
        void init()
        {
            for(int i=0;i<=m+1;i++)G[i].clear();
            edges.clear();
            memset(in,0,sizeof(in));
            memset(out,0,sizeof(out));
        }
        void addedge(int from,int to,int cap)
        {
            edges.push_back((Edge){from,to,cap,0});
            edges.push_back((Edge){to,from,0,0});
            int m=edges.size();
            G[from].push_back(m-2);
            G[to].push_back(m-1);
        }
        bool bfs()
        {
            memset(vis,0,sizeof(vis));
            queue<int>q;
            q.push(s);
            d[s]=0;
            vis[s]=1;
            while(!q.empty())
            {
                int x=q.front();q.pop();
                for(int i=0;i<G[x].size();i++)
                {
                    Edge& e=edges[G[x][i]];
                    if(!vis[e.to]&&e.cap>e.flow)
                    {
                        vis[e.to]=1;
                        d[e.to]=d[x]+1;
                        q.push(e.to);
                    }
                }
            }
            return vis[t];
        }
        int dfs(int x,int a)
        {
            if(x==t||a==0)return a;
            int flow=0,f;
            for(int& i=cur[x];i<G[x].size();i++)
            {
                Edge& e=edges[G[x][i]];
                if(d[x]+1==d[e.to]&&(f=dfs(e.to,min(a,e.cap-e.flow)))>0)
                {
                    e.flow+=f;
                    edges[G[x][i]^1].flow-=f;
                    flow+=f;
                    a-=f;
                    if(a==0)break;
                }
            }
            return flow;
        }
        int maxflow(int s,int t)
        {
            this->s=s,this->t=t;
            int flow=0;
            while(bfs())
            {
                memset(cur,0,sizeof(cur));
                flow+=dfs(s,INF);
            }
            return flow;
        }
    }dc;
    int main()
    {
        int T;
        scanf("%d",&T);
        while(T--)
        {
            dc.init();
            scanf("%d%d",&m,&s);
            for(int i=0;i<s;i++)
            {
                int u,v,d;
                scanf("%d%d%d",&u,&v,&d);
                out[u]++,in[v]++;
                if(d==0)dc.addedge(u,v,1);
            }
            bool ok=1;
            int sum=0;
            for(int i=1;i<=m;i++)
                if((in[i]-out[i])%2==1)
                {
                    ok=0;
                    break;
                }
            if(!ok){puts("impossible");continue;}
            for(int i=1;i<=m;i++)
            {
                if(in[i]<out[i])
                    dc.addedge(0,i,(out[i]-in[i])/2);
                else if(in[i]>out[i])
                {
                    dc.addedge(i,m+1,(in[i]-out[i])/2);
                    sum+=(in[i]-out[i])/2;
                }
            }
            puts(sum==dc.maxflow(0,m+1)?"possible":"impossible");
        }
        return 0;
    }
  • 相关阅读:
    参考vue.js实现双向绑定的方法理解双向绑定原理(:Object.defineProperty和发布-订阅模式)
    不错的站点 博文
    使用C#动态生成Word文档/Excel文档的程序测试通过后,部署到IIS服务器上,不能正常使用的问题解决方案
    详解HTML<head> 头标签元素的意义以及使用场景
    css奇特用法之 IMG添加背景图片配合显示--效果惊艳
    .net面试题-15k+左右
    微信小程序IOS真机调试发生了SSL 错误,无法建立与该服务器的安全连接
    微信小程序自定义组件-下拉框
    微信小程序语音(A)发给别人(B),也能播放,是需要先把语音上传到自己的服务器上才可以
    微信小程序循环中点击一个元素,其他的元素不发生变化,类似点击一个循环中的语音,其他的不发生点击事件
  • 原文地址:https://www.cnblogs.com/homura/p/6075619.html
Copyright © 2011-2022 走看看