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;
    }
    
    
  • 相关阅读:
    Mybatis多表查询
    (转)Java安全通信:HTTPS与SSL
    (转)RSA加密解密及数字签名Java实现
    (转)大型企业电话会议视频会议备份解决方案
    (转)虚拟IP原理
    虚拟IP---Linux下一个网卡配置多个IP
    C++ 点
    算法(8)Maximum Product Subarray
    算法(7)Majority Element II
    算法(6)3Sum Closest
  • 原文地址:https://www.cnblogs.com/Yuzao/p/8447461.html
Copyright © 2011-2022 走看看