poj 1637 混合欧拉 最大流

来源:互联网 发布:mac电脑发热严重 编辑:程序博客网 时间:2024/06/14 03:25

Sightseeing tour

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Java/Other)
Total Submission(s) : 1   Accepted Submission(s) : 0
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
45 82 1 01 3 04 1 11 5 05 4 13 4 04 2 12 2 04 41 2 12 3 03 4 01 4 13 31 2 02 3 03 2 03 41 2 02 3 11 2 03 2 0
 

Sample Output
possibleimpossibleimpossiblepossible
 

1 定义

欧拉通路 (Eulertour)——通过图中每条边一次且仅一次,并且过每一顶点的通路。

欧拉回路 (Eulercircuit)——通过图中每条边一次且仅一次,并且过每一顶点的回路。

欧拉图——存在欧拉回路的图。

2 无向图是否具有欧拉通路或回路的判定

G有欧拉通路的充分必要条件为:G 连通,G中只有两个奇度顶点(它们分别是欧拉通路的两个端点)。

G有欧拉回路(G为欧拉图):G连通,G中均为偶度顶点。

3 有向图是否具有欧拉通路或回路的判定

D有欧拉通路:D连通,除两个顶点外,其余顶点的入度均等于出度,这两个特殊的顶点中,一个顶点的入度比出度大1,另一个顶点的入度比出度小1。

D有欧拉回路(D为欧拉图):D连通,D中所有顶点的入度等于出度。

4 混合图。混合图也就是无向图与有向图的混合,即图中的边既有有向边也有无向边。

5 混合图欧拉回路

混合图欧拉回路用的是网络流。

把该图的无向边随便定向,计算每个点的入度和出度。如果有某个点出入度之差为奇数,那么肯定不存在欧拉回路。因为欧拉回路要求每点入度 = 出度,也就是总度数为偶数,存在奇数度点必不能有欧拉回路。

现在每个点入度和出度之差均为偶数。将这个偶数除以2,得x。即是说,对于每一个点,只要将x条边反向(入>出就是变入,出>入就是变出),就能保证出 = 入。如果每个点都是出 = 入,那么很明显,该图就存在欧拉回路。

现 在的问题就变成了:该改变哪些边,可以让每个点出 = 入?构造网络流模型。有向边不能改变方向,直接删掉。开始已定向的无向边,定的是什么向,就把网络构建成什么样,边长容量上限1。另新建s和t。对于入 > 出的点u,连接边(u, t)、容量为x,对于出 > 入的点v,连接边(s, v),容量为x(注意对不同的点x不同。当初由于不小心,在这里错了好几次)。之后,察看是否有满流的分配。有就是能有欧拉回路,没有就是没有。查看流值分配,将所有流量非 0(上限是1,流值不是0就是1)的边反向,就能得到每点入度 = 出度的欧拉图。

由于是满流,所以每个入> 出的点,都有x条边进来,将这些进来的边反向,OK,入 = 出了。对于出 > 入的点亦然。那么,没和s、t连接的点怎么办?和s连接的条件是出 > 入,和t连接的条件是入 > 出,那么这个既没和s也没和t连接的点,自然早在开始就已经满足入 = 出了。那么在网络流过程中,这些点属于“中间点”。我们知道中间点流量不允许有累积的,这样,进去多少就出来多少,反向之后,自然仍保持平衡。

所以,就这样,混合图欧拉回路问题,解了。

混合图的欧拉回路判断方法在黑书上面有具体提到。

 

这里采用的方法:

先给无向边定向,让后统计每一个点的出入度,如果有一个点的出入度只差为奇数,则该图不存在欧拉回路(有向图的欧拉回路每个点的度数都是偶数,至于出度=入度,在求最大流时我们会进行调整)。

全部判断完后,开始建图。

1:把每一条无向边建成一条容量为1的弧。

2:出入度之差不为0的点:

如果出>入 则将改点和源点相连 容量为出入度之差/2。

如果入>出 则将点和汇点相连,容量为出入度之差/2。

完成后求一个最大流。如果是满流则存在偶来回路



#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>using namespace std;const int N=4002;const int M=30002;const int INF=1<<29;int t,n,m,tot;int gap[M],dis[M],pre[M],head[N],cur[N];int NE,NV,sink,source,flag,in[N],out[N];struct Node{    int c,pos,next;} E[M*4];#define FF(i,NV) for(int i=0;i<NV;i++)int sap(int s,int t){    memset(dis,0,sizeof(int)*(NV+1));    memset(gap,0,sizeof(int)*(NV+1));    FF(i,NV) cur[i] = head[i];    int u = pre[s] = s,maxflow = 0,aug =INF;    gap[0] = NV;    while(dis[s] < NV)    {loop:        for(int &i = cur[u]; i != -1; i = E[i].next)        {            int v = E[i].pos;            if(E[i].c && dis[u] == dis[v] + 1)            {                aug=min(aug,E[i].c);                pre[v] = u;                u = v;                if(v == t)                {                    maxflow += aug;                    for(u = pre[u]; v != s; v = u,u = pre[u])                    {                        E[cur[u]].c -= aug;                        E[cur[u]^1].c += aug;                    }                    aug =INF;                }                goto loop;            }        }        if( (--gap[dis[u]]) == 0)   break;        int mindis = NV;        for(int i = head[u]; i != -1 ; i = E[i].next)        {            int v = E[i].pos;            if(E[i].c && mindis > dis[v])            {                cur[u] = i;                mindis = dis[v];            }        }        gap[ dis[u] = mindis+1 ] ++;        u = pre[u];    }    return maxflow;}void addEdge(int u,int v,int c ){    E[NE].c = c;    E[NE].pos = v;    E[NE].next = head[u];    head[u] = NE++;    E[NE].c = 0;    E[NE].pos = u;    E[NE].next = head[v];    head[v] = NE++;}int main(){    scanf("%d",&t);    while(t--)    {        int i,j,u,v,id,sum=0;        scanf("%d%d",&n,&m);        NE=flag=0;        source=0,sink=n+1,NV=sink+1;        for(i=0; i<=NV; i++)head[i]=-1,in[i]=out[i]=0;        for(i=1; i<=m; i++)        {            scanf("%d%d%d",&u,&v,&id);            out[u]++,in[v]++;            if(!id)addEdge(u,v,1);        }        for(i=1; i<=n; i++)        {            int x=(out[i]-in[i])/2;            if(abs(out[i]-in[i])&1)            {                flag=1;                break;            }            if(x>0) addEdge(source,i,x),sum+=x;            else  addEdge(i,sink,-x);        }        if(flag)printf("impossible\n");        else        {            int ans=sap(source,sink);            if(sum==ans) printf("possible\n");            else printf("impossible\n");        }    }    return 0;}/*45 82 1 01 3 04 1 11 5 05 4 13 4 04 2 12 2 04 41 2 12 3 03 4 01 4 13 31 2 02 3 03 2 03 41 2 02 3 11 2 03 2 0*/


原创粉丝点击