码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 1637 Sightseeing tour (混合图欧拉回路,网络最大流)

时间:2014-08-03 12:54:25      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:algorithm   图论   网络流   

http://poj.org/problem?id=1637

Sightseeing tour
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 7498   Accepted: 3123

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



混合图的欧拉回路求解方法为:

任意给无向边加方向,然后统计图中点的入度出度,如果所有点的入度和出度的差为偶数则有可能构成欧拉回路;

删除原来的有向边,加方向的无向边容量为1;

增加源点和汇点,如果某点入度大于出度,则和汇点相连,容量为(入度-出度)/2;如果某点出度大于入度,则源点与其相连,容量为(出度-入度)/2;

在新图中跑最大流,如果满流则有可能构成欧拉回路。


#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<algorithm>
#include<ctime>
#include<cctype>
#include<cmath>
#include<string>
#include<cstring>
#include<stack>
#include<queue>
#include<list>
#include<vector>
#include<map>
#include<set>
#define sqr(x) ((x)*(x))
#define LL long long
#define itn int
#define INF 0x3f3f3f3f
#define PI 3.1415926535897932384626
#define eps 1e-10
#define maxm 4004
#define maxn 404

using namespace std;

int fir[maxn];
int u[maxm],v[maxm],cap[maxm],flow[maxm],nex[maxm];
int e_max;
int iter[maxn],q[maxn],lv[maxn];

void add_edge(int _u,int _v,int _w)
{
    int e;
    e=e_max++;
    u[e]=_u;v[e]=_v;cap[e]=_w;
    nex[e]=fir[u[e]];fir[u[e]]=e;
    e=e_max++;
    u[e]=_v;v[e]=_u;cap[e]=0;
    nex[e]=fir[u[e]];fir[u[e]]=e;
}

void dinic_bfs(int s)
{
    int f,r;
    memset(lv,-1,sizeof lv);
    q[f=r=0]=s;
    lv[s]=0;
    while(f<=r)
    {
        int x=q[f++];
        for (int e=fir[x];~e;e=nex[e])
        {
            if (cap[e]>flow[e] && lv[v[e]]<0)
            {
                lv[v[e]]=lv[u[e]]+1;
                q[++r]=v[e];
            }
        }
    }
}

int dinic_dfs(int _u,int t,int _f)
{
    if (_u==t)  return _f;
    for (int &e=iter[_u];~e;e=nex[e])
    {
        if (cap[e]>flow[e] && lv[_u]<lv[v[e]])
        {
            int _d=dinic_dfs(v[e],t,min(_f,cap[e]-flow[e]));
            if (_d>0)
            {
                flow[e]+=_d;
                flow[e^1]-=_d;
                return _d;
            }
        }
    }

    return 0;
}

int max_flow(int s,int t)
{

    memset(flow,0,sizeof flow);
    int total_flow=0;

    for (;;)
    {
        dinic_bfs(s);
        if (lv[t]<0)    return total_flow;
        memcpy(iter,fir,sizeof iter);
        int _f;

        while ((_f=dinic_dfs(s,t,INF))>0)
            total_flow+=_f;
    }

    return total_flow;
}

struct EDGE
{
    int u,v,w;
}edge[1111];
int in[222],out[222];

int main()
{
    #ifndef ONLINE_JUDGE
        freopen("/home/fcbruce/文档/code/t","r",stdin);
    #endif // ONLINE_JUDGE

    int T_T;
    unsigned long long seed=~0ULL;
    scanf("%d",&T_T);

    while (T_T--)
    {
        int n,m,s,t,full_flow=0;
        e_max=0;
        memset(fir,-1,sizeof fir);
        memset(in,0,sizeof in);
        memset(out,0,sizeof out);
        scanf("%d%d",&n,&m);
        seed=seed^T_T^n^m^(~0ULL);

        srand(seed);

        s=0;t=n+1;

        for (int i=0;i<m;i++)
        {
            scanf("%d %d %d",&edge[i].u,&edge[i].v,&edge[i].w);
            if (edge[i].w==1)
            {
                out[edge[i].u]++;
                in[edge[i].v]++;
                edge[i].w=0;
                continue;
            }
            if (rand()&1)
            {
                edge[i].w=1;
                out[edge[i].u]++;
                in[edge[i].v]++;
                add_edge(edge[i].u,edge[i].v,1);
            }
            else
            {
                edge[i].w=-1;
                out[edge[i].v]++;
                in[edge[i].u]++;
                add_edge(edge[i].v,edge[i].u,1);
            }
        }


        bool flag=true;
        for (int i=1;i<=n;i++)
        {
            if (abs(in[i]-out[i])&1)
            {
                flag=false;
                break;
            }
            else
            {
                if (in[i]==out[i])  continue;
                if (in[i]>out[i])
                    add_edge(i,t,in[i]-out[i]>>1);
                else
                    add_edge(s,i,out[i]-in[i]>>1),full_flow+=out[i]-in[i]>>1;
            }
        }

        if (!flag)
        {
            puts("impossible");
            continue;
        }
        else
        {
            if (max_flow(s,t)==full_flow)
                puts("possible");
            else
                puts("impossible");
        }

    }

    return 0;
}


POJ 1637 Sightseeing tour (混合图欧拉回路,网络最大流),布布扣,bubuko.com

POJ 1637 Sightseeing tour (混合图欧拉回路,网络最大流)

标签:algorithm   图论   网络流   

原文地址:http://blog.csdn.net/u012965890/article/details/38355769

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!