zoukankan      html  css  js  c++  java
  • poj1637 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

    分析:
    题意:给一个混合图 (有单向边有无向边),
    你要给无向边定向使得原图中存在一条欧拉回路

    首先要明确一个定理:
    如果有向图的每个节点的出度等于入度,那这个图一定有欧拉回路
    对于无向边,我们可以随意定向,
    看看能不能通过调整无向边方向让每个点出度等于入度

    这个调整就需要网络流了

    计算每个点的入度减出度(d)
    如果存在d是奇数,则无解

    每改变一条边的方向
    1.出度+1,入度-1,入度减出度 -2
    2.出度-1,入度+1,入度减出度 +2
    总的来说,d每次会改变2
    但是网络流没法一下子增广2啊,
    那就把所有的量/2

    d为正的点作为第一部,d为负的点作为第二部
    分别和源点和汇点连边,容量为|d|
    在两个部之间,连接无向边,容量为1

    tip

    在定向时,如果定的向是u—->w
    连边的时候要w—–>u

    第一次提交竟然T了,怀疑自己的dinic
    把建图和输入结合在一起,就A了
    这给我们一个启示,就是能在一起做的操作就捆绑一下,
    减少循环的次数
    节约是一种美德。。。

    这里写代码片
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    
    using namespace std;
    
    const int INF=0x33333333;
    const int N=205;
    struct node{
        int x,y,nxt,v;
    };
    node way[20010];
    int deep[N],in[N],n,m,s,t,tot=-1,st[N];
    int lu[1010][2],tt=0,q[N],tou,wei,cur[N];
    
    void add(int u,int w,int z)
    {
        tot++;
        way[tot].x=u;way[tot].y=w;way[tot].v=z;way[tot].nxt=st[u];st[u]=tot;
        tot++;
        way[tot].x=w;way[tot].y=u;way[tot].v=0;way[tot].nxt=st[w];st[w]=tot;
    }
    
    int bfs(int s,int t)
    {
        memset(deep,-1,sizeof(deep));
        wei=tou=0;
        q[++wei]=s;
        deep[s]=1;
        for (int i=s;i<=t;i++) cur[i]=st[i];
        do
        {
            int r=q[++tou];
            for (int i=st[r];i!=-1;i=way[i].nxt)
                if (way[i].v&&deep[way[i].y]==-1)
                {
                    deep[way[i].y]=deep[r]+1;
                    q[++wei]=way[i].y;
                }
        }while (tou<wei);
        return deep[t]!=-1;
    }
    
    int dfs(int now,int t,int limit)
    {
        if (!limit||now==t) return limit;
        int f,flow=0,i;
        for (i=cur[now];i!=-1;i=way[i].nxt)
        {
            cur[now]=i;
            if (way[i].v&&deep[way[i].y]==deep[now]+1&&(f=dfs(way[i].y,t,min(limit,way[i].v))))
            {
                limit-=f;
                flow+=f;
                way[i].v-=f;
                way[i^1].v+=f;
                if (!limit) break;
            }
        }
        return flow;
    }
    
    int doit()
    {
        int ans=0;
        while (bfs(s,t))
            ans+=dfs(s,t,INF);
        return ans;
    }
    
    int main()
    {
        int T;
        scanf("%d",&T);
        while (T--)
        {
            memset(st,-1,sizeof(st)); tot=-1;
            memset(in,0,sizeof(in));
            bool ff=1;
            int o=0;
            scanf("%d%d",&n,&m);
            s=0,t=n+1;   ///
            for (int i=1;i<=m;i++)
            {
                int u,w,opt;
                scanf("%d%d%d",&u,&w,&opt);
                if (opt==1){in[w]++;in[u]--;}   //u--->w
                else  //随意定向 
                {in[w]++;in[u]--;add(w,u,1);} 
            }
            for (int i=1;i<=n;i++)
            {
                if (in[i]%2==1) 
                {
                    ff=0;
                    break;
                }
                in[i]/=2;
                if (in[i]>0) add(s,i,in[i]),o+=in[i];  //总流量 
                else if (in[i]<0) add(i,t,-in[i]);
            } 
            if (ff==0) {
                printf("impossible
    ");
                continue;
            }
            if (doit()==o) printf("possible
    ");  //满流
            else printf("impossible
    ");
        }
        return 0;
    }
  • 相关阅读:
    每日优鲜三面:在Spring Cloud实战中,如何用服务链路追踪Sleuth?
    一文就能看懂的Nginx操作详解,你还在查漏补缺吗!
    火花思维三面:说说Redis分布式锁是如何实现的!
    【秋招必备】Dubbo面试题(2021最新版)
    【秋招必备】Elasticsearch面试题(2021最新版)
    熬了一通宵!你竟然都没有弄懂陌陌面试官问的Java虚拟机内存?
    react-native-vector-icons 使用记录
    git
    在iOS项目中嵌入RN代码
    UITabBar 图标上下跳动
  • 原文地址:https://www.cnblogs.com/wutongtong3117/p/7673433.html
Copyright © 2011-2022 走看看