Euler circuit determination of mixed graphs of ACM/ICPC-network flow (POJ1637)

Source: Internet
Author: User
Tags in degrees

Network flow judgment mixed Graph//through the network flow so that the points of the same degree of access possible, otherwise the weight of the impossible//residual network is the number of changes in the direction, that is, n two-way Edge has n times//time:157ms memory:348k# Include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <queue    >using namespace std; #define MAXN 205#define INF 0x3f3f3f3fint n,m;int s,t;int dif[maxn];int RES[MAXN][MAXN];    Residual network-Represents the variable direction number int Pre[maxn];bool BFs () {memset (pre,-1,sizeof (pre));    Queue<int> Q; Q.push (s);    Pre[s] = 0;        while (!q.empty ()) {int cur = q.front ();        Q.pop (); for (int i = 1; I <= t; i++) {if (pre[i] = = 1 && res[cur][i]) {Pre                [I] = cur;                if (i = = t) return true;            Q.push (i); }}} return false;}    int EK () {int maxflow = 0;        while (BFS ()) {int mind = INF;        for (int i = t; I! = s; i = pre[i]) mind = min (mind, res[pre[i]][i]); for (int i = t; i = s; i = Pre[i]) {res[pre[i]][i]-= mind;        Res[i][pre[i] + + + mind;    } Maxflow + = mind; } return maxflow;}    int main () {//freopen ("In.txt", "R", stdin);    int T;    scanf ("%d", &t);        while (t--) {memset (dif,0,sizeof (DIF));        memset (res,0,sizeof (res));        scanf ("%d%d", &n, &m);        int total = 0; s = 0;        t = n+1;            for (int i = 0; i < m; i++) {int u,v,t;            scanf ("%d%d%d", &u,&v,&t);   dif[u]++;            dif[v]--;  if (t = = 0) res[u][v] + = 1;        Heavy edge then variable direction +1} bool flag = TRUE; for (int i = 1; I <= n; i++) {if (Dif[i] > 0) {//out many-odd-through source points res[s][i] = dif[                I]/2;            Total + = DIF[I]/2;  } if (Dif[i] < 0) res[i][t] =-DIF[I]/2;                In degrees-give odd-out if (ABS (Dif[i])% 2 = = 1) by meeting point {flag = false;            Break }} (Flag && EK () = = TotAL)?    printf ("possible\n"): printf ("impossible\n"); } return 0;}

Euler circuit determination of mixed graphs of ACM/ICPC-network flow (POJ1637)

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.