首页 > 其他 > 详细

POJ 1637:Sightseeing tour

时间:2018-02-13 22:09:57      阅读:196      评论:0      收藏:0      [点我收藏+]

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.

Translation

判断混合图是否存在欧拉回路,混合图即 既存在单向边也存在双向边的图

Solution

其实决策就是把双向边定向
假设我们随便定好项,会得到一个入度和出度,设其差值为 \(in[i]\)
如果不是偶数显然不存在
否则就要调整双向边的方向,存在欧拉回路当且仅当所有点都满足 \(in[i]==0\)

我们把 \(in[i]\) 为负数的连到 \(S\),容量为 \(-\frac{in[i]}{2}\),正数的连到 \(T\),容量为 \(\frac{in[i]}{2}\)
一条\(S->T\)的增广路相当于把左边的点 \(in[i]+=2\),右边的 \(in[i]-=2\)
如果全部流满则代表存在

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
const int N=405,M=100005,inf=2e8;
int n,m,head[N],nxt[M],to[M],dis[M],num=1,in[N];
inline void link(int x,int y,int z){
    nxt[++num]=head[x];to[num]=y;head[x]=num;dis[num]=z;
    nxt[++num]=head[y];to[num]=x;head[y]=num;dis[num]=0;
}
int S,T=N-1,dep[N];
inline bool bfs(){
    queue<int>q;
    memset(dep,0,sizeof(dep));
    q.push(S);dep[S]=1;
    while(!q.empty()){
        int x=q.front();q.pop();
        for(int i=head[x];i;i=nxt[i]){
            int u=to[i];
            if(dis[i]<=0 || dep[u])continue;
            dep[u]=dep[x]+1;q.push(u);
        }
    }
    return dep[T];
}
inline int dfs(int x,int flow){
    if(x==T || !flow)return flow;
    int tot=0,u,t;
    for(int i=head[x];i;i=nxt[i]){
        u=to[i];
        if(dep[u]!=dep[x]+1 || dis[i]<=0)continue;
        t=dfs(u,min(flow,dis[i]));
        dis[i]-=t;dis[i^1]+=t;
        flow-=t;tot+=t;
        if(!flow)break;
    }
    if(!tot)dep[x]=-1;
    return tot;
}
inline int Dinic(){
    int tmp,tot=0;
    while(bfs()){
        tmp=dfs(S,inf);
        while(tmp)tot+=tmp,tmp=dfs(S,inf);
    }
    return tot;
}
inline void Clear(){
    num=1;
    for(register int i=0;i<N;i++)head[i]=in[i]=0;
}
void work(){
    Clear();
    int x,y,z,tot=0;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++){
        scanf("%d%d%d",&x,&y,&z);
        in[y]++,in[x]--;
        if(!z)link(x,y,1);
    }
    for(int i=1;i<=n;i++)if(in[i]&1){puts("impossible");return ;}
    for(int i=1;i<=n;i++){
        if(in[i]<0)link(S,i,-(in[i]>>1));
        else if(in[i]>0)link(i,T,in[i]>>1),tot+=in[i]>>1;
    }
    if(Dinic()==tot)puts("possible");
    else puts("impossible");
}
int main(){
  freopen("pp.in","r",stdin);
  freopen("pp.out","w",stdout);
  int T;cin>>T;
  while(T--)work();
  return 0;
}

POJ 1637:Sightseeing tour

原文:https://www.cnblogs.com/Yuzao/p/8447461.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!