HDU1956 Sightseeing tour 混合图欧拉回路 最大流Dinic

47 篇文章 0 订阅
10 篇文章 0 订阅
Sightseeing tour
Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 424    Accepted Submission(s): 192


Problem 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



Source
NWERC2003 

脑洞大开..原来最大流还能这样用

不难发现 之前求解欧拉回路的方法 在混合图中貌似没什么luan用
首先 对图中的 无向边 进行任意定向 转化为 有向图
令点i的度degree[i]=出度-入度
当degree[i]为奇数时 明显 无论无向边的方向怎么定向 degree[i]都是奇数
而在无向图中 只有所有边的 入度=出度 即degree[i]=0 才可能存在欧拉回路
所以degree[i]为奇数 直接impossible

当degree[i]全为偶数时
建一个源点S,连接到degree[i]>0的边 权值为degree[i]/2
建一个汇点T,degree[i]<0的点 连接到T 权值为 -degree[i]/2;
将所有定向了的无向边添加到图中 权值为1

如果找到了一条增广路径S->i->j->T
那残存网络中 i->S的流量增加 可以看作是 将原来的无向边的方向调换了一下 重新定向
degree[i]递减 degree[j]递增
当最大流网络中 满流后的残存网络 degree[i]全部=0 即存在欧拉回路


#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<time.h>
#include<math.h>
#include<list>
#include<cstring>
#include<fstream>
//#include<memory.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define INF 1000000007
#define pll pair<ll,ll>
#define pid pair<int,double>

const int N = 200 + 5;
const int M = 2e3+N;

int level[N];//标号 level[i]:s到i的最短距离

struct Edge{
    int to,c,next;
}edge[M];
int head[N];

inline void add_edge(int k,int u,int v,int c){
    edge[k].to = v;
    edge[k].c = c;
    edge[k].next = head[u];
    head[u] = k;
}

bool bfs(int s,int t,int n){//标号 计算level
    deque<int>que;
    fill(level,level+n+1,-1);
    que.push_back(s);
    level[s] = 0;
    while(!que.empty()){
        int u = que.front();
        if(u == t){
            return true;
        }
        que.pop_front();
        for(int i = head[u];i!=-1;i = edge[i].next){
            if(edge[i].c > 0 && level[edge[i].to] == -1){
                level[edge[i].to] = level[u] + 1;
                que.push_back(edge[i].to);
            }
        }
    }
    return false;
}

int dfs(int u,int t,int maxf){//u:所在的点 t:汇点 maxf:能流到u的流量
    if(u == t){
        return maxf;
    }
    int sum = 0;
    for(int i = head[u];i!=-1;i = edge[i].next){
        Edge&e = edge[i];
        if(e.c>0 && level[e.to]>level[u]){
            int f = dfs(e.to,t,min(maxf - sum,e.c));
            sum += f;
            edge[i].c -= f;
            edge[i^1].c += f;
            if(sum == maxf){//流量用完了
                break;
            }
        }
    }
    level[u]=-1;
    return sum;
}

int dinic(int s,int t,int n){//s:源点 t:汇点 n:点数
    int ans = 0;
    while(bfs(s,t,n)){
        ans += dfs(s,t,2*INF);
    }
    return ans;
}

int degree[N];//degreed = out_degree - in_degree

bool slove(int n,int m){
    fill(head,head+n+2+1,-1);
    fill(degree,degree+n+1,0);
    int u,v,direct;
    int tmp=0;
    for(int i=0;i<m;++i){
        scanf("%d%d%d",&u,&v,&direct);
        ++degree[u];
        --degree[v];
        if(direct!=1){//定向为u->v
            add_edge(tmp++,u,v,1);
            add_edge(tmp++,v,u,0);
        }
    }
    const int s = n+1;
    const int t = n+2;
    int tar_flow=0;
    for(int i=1;i<=n;++i){
        if((degree[i]%2+2)%2==1){//度为奇数
            return false;
        }
        if(degree[i]>0){//s->i
            tar_flow+=degree[i]/2;
            add_edge(tmp++,s,i,degree[i]/2);
            add_edge(tmp++,i,s,0);
        }
        if(degree[i]<0){//i->t
            add_edge(tmp++,i,t,-degree[i]/2);
            add_edge(tmp++,t,i,0);
        }
    }
    return dinic(s,t,n+2) == tar_flow;
}

int main()
{
    //freopen("/home/lu/Documents/r.txt","r",stdin);
    //freopen("/home/lu/Documents/w.txt","w",stdout);
    int T;
    scanf("%d",&T);
    while(T--){
        int n, m;
        scanf("%d%d",&n,&m);
        puts(slove(n,m)?"possible":"impossible");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值