poj1459网络流多源点多汇点模板题

来源:互联网 发布:c语言单竖线 编辑:程序博客网 时间:2024/06/05 07:20

题目链接


http://poj.org/problem?id=1459
 这道题我想用scanf输入坑死,,,最后老老实实用cin做了,,,
 后来网上学了下其实用scanf(“% * ^(“);scanf(“(%d,%d)%d”, &a, &b, &c);就可以。scanf(“%*^(“);的意思是读入一行字符串直到遇到(字符,并且不读入(字符。

#include <iostream>#include <cstdio>#include <cstring>#include <queue>#include <vector>using namespace std;const int maxn = 150;const int INF = 0x3f3f3f3f;struct Edge{    int from, to, cap, flow;    Edge(int u, int v, int c, int f) : from(u), to(v), cap(c), flow(f){}};struct EdmondsKarp{    vector<int>G[maxn];    vector<Edge>edges;    int a[maxn];    int p[maxn];    void init(int n)    {        for(int i = 0;i <= n + 2;i++)        {            G[i].clear();        }        edges.clear();    }    void AddEdge(int from, int to, int cap)    {        edges.push_back(Edge(from, to, cap, 0));        edges.push_back(Edge(to, from, 0, 0));        int m = edges.size();        G[from].push_back(m - 2);        G[to].push_back(m - 1);    }    int MaxFlow(int s, int t)    {        int flow = 0;        for(;;)        {            memset(a, 0, sizeof(a));            queue<int>Q;            Q.push(s);            a[s] = INF;            while(!Q.empty())            {                int x = Q.front();                Q.pop();                for(int i = 0;i < (int)G[x].size();i++)                {                    Edge &e = edges[G[x][i]];                    if(!a[e.to]&&e.cap - e.flow > 0)                    {                        p[e.to] = G[x][i];                        a[e.to] = min(a[x], e.cap - e.flow);                        Q.push(e.to);                    }                }                if(a[t])                    break;            }            if(!a[t])                break;            for(int u = t;u != s;u = edges[p[u]].from)            {                edges[p[u]].flow += a[t];                edges[p[u]^1].flow -= a[t];            }            flow += a[t];        }        return flow;    }}ek;int main(){    int n, np, nc, m;    while(cin>>n>>np>>nc>>m)    {        ek.init(n);        for(int i = 1;i <= m;i++)        {            int a, b, c;           char ch;           cin>>ch>>a>>ch>>b>>ch>>c;            ek.AddEdge(a, b, c);        }        for(int i = 1;i <= np;i++)        {            int a, b;            char ch;           cin>>ch>>a>>ch>>b;            ek.AddEdge(n + 1, a, b);        }        for(int i = 1;i <= nc;i++)        {             int a, b;            char ch;            cin>>ch>>a>>ch>>b;            ek.AddEdge(a, n + 2, b);        }        cout<<ek.MaxFlow(n + 1, n + 2)<<endl;    }    return 0;}