UV 11506 angry programmer (minimum cut Max stream, split point, graph)

Source: Internet
Author: User

This question was first written in the adjacent table, WA, and then changed the matrix. The result is still incorrect, but I found something wrong.

It is mainly to create a graph and determine whether the two points are the starting point or sink point. For these two points, do not split them because there is no cost.

All the other points are split into two points, and the capacity is the cost of this computer. The others follow the rules, starting from the original point

The matrix code is as follows:

#include <cstdio>#include <cstring>#include <queue>#include <algorithm>using namespace std;const int N = 100;const int INF = 100000000;int n, m;int cap[N][N], flow[N][N];int p[N], a[N];int maxflow() {    queue<int> q;    memset(flow, 0, sizeof(flow));    int f = 0;    while ( 1 ) {        memset(a, 0, sizeof(a));        a[1] = INF;        q.push(1);        while ( !q.empty() ) {            int u = q.front(); q.pop();            for ( int v = 1; v <= n + n - 1; v++ ) if ( !a[v] && cap[u][v] > flow[u][v] ) {                p[v] = u;                a[v] = min( a[u], cap[u][v] - flow[u][v] );                q.push(v);            }        }        if ( a[n] == 0 ) break;        for ( int u = n; u != 1; u = p[u] ) {            //printf("%d ", u );            flow[p[u]][u] += a[n];            flow[u][p[u]] -= a[n];        }        f += a[n];    }    return f;}int main(){    while ( scanf("%d%d", &n, &m) != EOF && !( !n && !m ) ) {        memset( cap, 0, sizeof(cap) );        for ( int i = 2; i < n; ++i ) {            int com, c;            scanf("%d%d", &com, &c);            cap[com][com+n] = cap[com+n][com] = c;        }        for ( int i = 0; i < m; ++i ) {            int u, v, c;            scanf("%d%d%d", &u, &v, &c);            if ( u == 1 && v != n ) cap[u][v] = cap[v+n][u] = c;            else if ( u == 1 && v == n ) cap[u][v] = cap[v][u] = c;            else if ( u == n && v != 1 ) cap[v+n][u] = cap[u][v] = c;            else if ( u == n && v == 1 ) cap[u][v] = cap[v][u] = c;            else cap[u+n][v] = cap[v+n][u] = c;        }        /*for ( int i = 1; i < 2*n; ++i ) {             for ( int j = 1; j < 2*n; ++j ) printf("%d ", cap[i][j]);            printf("\n");        }*/        int ans = maxflow();        printf("%d\n", ans);    }}

Code of the adjacent table: (pay attention to the number of edges × 4)

#include <cstdio>#include <cstring>#include <queue>#include <algorithm>using namespace std;const int N = 150;const int M = 4500;const int INF = 100000000;struct edge {    int v, next, cap, flow;}e[M];int n, m, id;int head[N];int maxflow() {    int f = 0, a[N], p[N], r[N];    while ( 1 ) {        queue <int> q;        int s = 1, t = n, np = n + n - 1;        q.push(s);        memset( a, 0, sizeof(a) );        a[s] = INF;        while ( !q.empty() ) {            int u = q.front(); q.pop();            for ( int i = head[u]; i != -1; i = e[i].next ) {                int v = e[i].v, cap = e[i].cap, flow = e[i].flow;                if ( cap > flow && !a[v] ) {                    a[v] = min( a[u], cap - flow );                    p[v] = u;                    r[v] = i;                    q.push(v);                }            }        }        if ( a[t] == 0 ) break;        for ( int u = t; u != s; u = p[u] ) {            int ei = r[u];            e[ei].flow += a[t];            e[ei^1].flow -= a[t];        }        f += a[t];    }    return f;}void add( int u, int v, int c ) {    e[id].next = head[u];    e[id].v = v, e[id].cap = c, e[id].flow = 0;    head[u] = id++;    e[id].next = head[v];    e[id].v = u, e[id].cap = 0, e[id].flow = 0;    head[v] = id++;}int main(){    while ( scanf("%d%d", &n, &m) != EOF && !(!n && !m) ) {        id = 0;        for ( int i = 0; i <= 2*n; ++i ) head[i] = -1;        for ( int i = 2, node, cost; i < n; ++i ) {          scanf("%d%d", &node, &cost);           add( node, node + n, cost );          add( node + n, node, cost );        }        for ( int i = 0; i < m; ++i ) {            int u, v, c;            scanf("%d%d%d", &u, &v, &c);            if ( u == 1 && v == n )  add( u, v, c ), add( v, u, c );            else if ( u == 1 && v != n ) add( u, v, c ), add( v+n, u, c ) ;            else if ( v == 1 && u == n ) add( v, u, c ), add( u, v, c );            else add( v + n, u, c ), add( u+n, v, c );        }        printf("%d\n", maxflow() );    }}

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.