天天看點

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.

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

【解題報告】

來自http://blog.csdn.net/l04205613/article/details/6681300

給出一張混合圖(有有向邊,也有無向邊),判斷是否存在歐拉回路。

首先是對圖中的無向邊随意定一個方向,然後統計每個點的入度(indeg)和出度(outdeg),如果(indeg - outdeg)是奇數的話,一定不存在歐拉回路;

如果所有點的入度和出度之差都是偶數,那麼就開始網絡流構圖:

1,對于有向邊,舍棄;對于無向邊,就按照最開始指定的方向建權值為 1 的邊;

2,對于入度小于出度的點,從源點連一條到它的邊,權值為(outdeg - indeg)/2;出度小于入度的點,連一條它到彙點的權值為(indeg - outdeg)/2 的邊;

構圖完成,如果滿流(求出的最大流值 == 和彙點所有連邊的權值之和),那麼存在歐拉回路,否則不存在。

代碼如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
#define N 210   
#define inf 0x3f3f3f3f

struct Edge{int s,e,v,next;}edge[*N];  
int n,m,e_num,cnt,head[N],d[N],sp,tp;  
int sum,indeg[N],outdeg[N];  

void AddEdge(int a,int b,int c)
{  
    edge[e_num].s=a;edge[e_num].e=b;edge[e_num].v=c;  
    edge[e_num].next=head[a];head[a]=e_num++;    
    edge[e_num].s=b;edge[e_num].e=a;edge[e_num].v=;  
    edge[e_num].next=head[b];head[b]=e_num++;  
}    
void getmap()
{  
    sum=;  
    for(int i=;i<=n;i++)
    { 
        if(indeg[i]<outdeg[i])
        { 
            AddEdge(,i,(outdeg[i]-indeg[i])/);  
        }
        else if(indeg[i]>outdeg[i])
        {  
            AddEdge(i,n+,(indeg[i]-outdeg[i])/);  
            sum+=(indeg[i]-outdeg[i])/;  
        }  
    }  
}  
int bfs()
{  
    queue<int> q;  
    memset(d,-,sizeof(d));  
    d[sp]=;  
    q.push(sp);  
    while(!q.empty())
    {  
        int cur=q.front();  
        q.pop();  
        for(int i=head[cur];i!=-;i=edge[i].next)
        {  
            int u=edge[i].e;  
            if(d[u]==- && edge[i].v>)
            {  
                d[u]=d[cur]+;  
                q.push(u);  
            }  
        }  
    }  
    return d[tp]!=-;  
}  
int dfs(int a,int b)
{  
    int r=;  
    if(a==tp) return b;  
    for(int i=head[a];i!=- && r<b;i=edge[i].next)
    {  
        int u=edge[i].e;  
        if(edge[i].v>&&d[u]==d[a]+)
        {  
            int x=min(edge[i].v,b-r);  
            x=dfs(u,x);  
            r+=x;  
            edge[i].v-=x;  
            edge[i^].v+=x;  
        }  
    }  
    if(!r) d[a]=-;  
    return r;  
}  

int dinic(int sp,int tp)
{  
    int total=,t;  
    while(bfs())
    {  
        while(t=dfs(sp,inf))  
        total+=t;  
    }  
    return total;  
}  
void solve()
{  
    int i,flag=;  
    for(i=;i<=n;i++)
    {  
        if((indeg[i]-outdeg[i])%==)
        {  
            flag=;break;  
        }  
    }  
    if(!flag) puts("impossible");  
    else
    {  
        getmap();  
        int ans=dinic(sp,tp);  
        if(sum==ans) puts("possible");  
        else puts("impossible");  
    }  
}  
int main()  
{  
    int t;  
    for(scanf("%d",&t);t;--t)  
    {  
        e_num=;  
        memset(head,-,sizeof(head));  
        memset(indeg,,sizeof(indeg));  
        memset(outdeg,,sizeof(outdeg));  
        scanf("%d%d",&n,&m);  
        for(int i=;i<m;i++)
        {  
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);  
            indeg[b]++;  
            outdeg[a]++;  
            if(c==) AddEdge(a,b,);  
        }  
        sp=;tp=n+;  
        solve();  
    }  
    return ;  
}  
           

繼續閱讀