zoukankan      html  css  js  c++  java
  • POJ 1637: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.

    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;
    }
    
    
  • 相关阅读:
    VS2010 配置驱动开发环境
    C函数调用与入栈顺序
    Ecshop后台流量分析地区分布的地名全是乱码
    使用.net程序发送邮件代码
    齐博系统出现此文件不可写:cache/label_cache/index_0_8_0_0_1_6539c.php
    UCHOME中链接前多了link.php?url=,如何去除
    discuz7.2 修改数据调用中日期格式
    php设置和获取cookie
    删除数据库所有存储过程的SQL语句
    docker搭建skywalking 8.7简明笔记 海口
  • 原文地址:https://www.cnblogs.com/Yuzao/p/8447461.html
Copyright © 2011-2022 走看看