The solution network flow of Euler circuit of poj1637 mixed graph

Source: Internet
Author: User
Tags in degrees

Topic Links:

POJ1637



Test instructions

A picture that gives a forward and a no-edge, asking if there is a Euler circuit that passes through all sides only once



Problem Solving Ideas:

The solution of Euler circuit of mixed graph requires the use of network flow, and the concrete modeling method is as follows:

1, first to all the non-directional direction, and then the statistics of all points in the degree and the degree,

2, if a certain point into the degree-out of the odd number then must not constitute the Euler circuit//into the degree of +x-x degrees Chachi even the same

3, if a certain point out of the degree of > into a source point to build a connection with the capacity of the edge (out of the degree)/2;

If a certain point out of the degree < the degree of the building of a meeting point to connect with the capacity of the edge (in degrees-out of)/2;

4. All non-directed edges are built in the fixed direction and the edge capacity is 1

5, run the maximum flow, if the maximum flow in the figure is equal to all the sinks connected to the capacity of the edge and then can constitute the Euler circuit



In fact, the process of running the maximum flow can be understood as: let all the points in the graph is equal to the degree of the process

A pathway in the maximum flow indicates that all edges on this path should be in the opposite direction of the original direction.

In this way, the point of connection to the source point is reduced by 1, the degree of penetration plus 1, and the point out of the sink point +1, in degrees-1.

The last maximum flow ==sum (in degrees and out of difference)/2 means that all points are equal to the degree of attainment.




Code:

#include <iostream> #include <cstring> #include <cstdio> #include <queue>const int maxn = 505; const int Maxm=440020;const int inf=0x3f3f3f3f;using namespace std;struct edge{int to,cap,flow,next;} edge[maxm];int H    Ead[maxn],tot,gap[maxn],d[maxn],cur[maxn],que[maxn],p[maxn];void init () {tot=0; memset (head,-1,sizeof (Head));}    void Addedge (int u,int v,int c,int f) {edge[tot]= (edge) {V,c,f,head[u]};    Head[u] = tot++;    Edge[tot]= (Edge) {u,c,c,head[v]}; HEAD[V] = tot++;}    int isap (int source,int sink,int N) {memset (gap,0,sizeof (GAP));    Memset (d,0,sizeof (d));    memcpy (cur,head,sizeof (head));    int top = 0,x = Source,flow = 0;            while (D[source] < N) {if (x = = sink) {int Min = inf,inser=0;                     for (int i = 0; i < top; ++i) {if (Min > Edge[p[i]].cap-edge[p[i]].flow) {                    Min = Edge[p[i]].cap-edge[p[i]].flow;                Inser = i; }            } for (int i = 0; i < top; ++i) {edge[p[i]].flow + = Min;            Edge[p[i]^1].flow = Min;            } if (min!=inf) flow + = Min;            top = Inser;            x = edge[p[top]^1].to;        Continue        } int ok = 0;            for (int i = cur[x]; i =-1; i = edge[i].next) {int v = edge[i].to;                if (Edge[i].cap > edge[i].flow && d[v]+1 = = D[x]) {OK = 1;                CUR[X] = i;                p[top++] = i;                x = edge[i].to;            Break            }} if (!ok) {int Min = N; for (int i = head[x]; i =-1; i = edge[i].next) {if (Edge[i].cap > Edge[i].flow && d[                    Edge[i].to] < min) {min = d[edge[i].to];                CUR[X] = i;  }} if (--gap[d[x] = = 0) break;          GAP[D[X] = min+1]++;        if (x! = source) x = edge[p[--top]^1].to; }} return flow;}    int main () {//Freopen ("In.txt", "R", stdin);    int IN[MAXN],OUT[MAXN];    int t,n,m,a,b,c;    scanf ("%d", &t);        while (t--) {init ();        memset (In,0,sizeof (in));        Memset (out,0,sizeof (out));        scanf ("%d%d", &n,&m);            while (m--) {scanf ("%d%d%d", &a,&b,&c);            out[a]++,in[b]++;        if (c==0) Addedge (a,b,1,0);        } int flag=0,sum=0,d;            for (int i=1; i<=n; i++) {d=in[i]-out[i];            if (d&1) flag=1;            else if (d<0) Addedge (0,i, (-D) >>1,0);                else if (d>0) {Addedge (i,n+1,d>>1,0);            sum+=d>>1;        }} if (Sum!=isap (0,n+1,n+2)) flag=1;        if (flag==1) printf ("impossible\n"); else printf ("possible\n"); } return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The solution network flow of Euler circuit of poj1637 mixed graph

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.