Farmer John always wants his cows to have enough water and thus has made a map of the N (1 <= N <= 700) water pipes on the farm that connect the well to the barn. He was surprised to find a wild mess of different size pipes connected in an apparently haphazard way. He wants to calculate the flow through the pipes.

Two pipes connected in a row allow water flow that is the minimum of the values of the two pipe's flow values. The example of a pipe with flow capacity 5 connecting to a pipe of flow capacity 3 can be reduced logically to a single pipe of flow capacity 3:

+---5---+---3---+ -> +---3---+

Similarly, pipes in parallel let through water that is the sum of their flow capacities:

+---5---+

---+ +--- -> +---8---+

+---3---+

Finally, a pipe that connects to nothing else can be removed; it contributes no flow to the final overall capacity:

+---5---+

---+ -> +---3---+

+---3---+--

All the pipes in the many mazes of plumbing can be reduced using these ideas into a single total flow capacity.

Given a map of the pipes, determine the flow capacity between the well (A) and the barn (Z).

Consider this example where node names are labeled with letters:

+-----------6-----------+

A+---3---+B +Z

+---3---+---5---+---4---+

C D

Pipe BC and CD can be combined:

+-----------6-----------+

A+---3---+B +Z

+-----3-----+-----4-----+

D Then BD and DZ can be combined:

+-----------6-----------+

A+---3---+B +Z

+-----------3-----------+

Then two legs of BZ can be combined:

B A+---3---+---9---+Z

Then AB and BZ can be combined to yield a net capacity of 3:

A+---3---+Z

Write a program to read in a set of pipes described as two endpoints and then calculate the net flow capacity from 'A' to 'Z'. All

networks in the test data can be reduced using the rules here.

Pipe i connects two different nodes a_i and b_i (a_i in range

'A-Za-z'; b_i in range 'A-Za-z') and has flow F_i (1 <= F_i <= 1,000). Note that lower- and upper-case node names are intended to be treated as different.

The system will provide extra test case feedback for your first 50 submissions.

 

思路:建立源点为A,汇点为Z的网络跑最大流

注意读入格式

#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
#include<vector>
#include<algorithm>

using namespace std;

const int maxn=1e4 + 7;
const int inf=0x3f3f3f3f;

template<class T>inline void read(T &res)
{
    char c;T flag=1;
    while((c=getchar())<'0'||c>'9')if(c=='-')flag=-1;res=c-'0';
    while((c=getchar())>='0'&&c<='9')res=res*10+c-'0';res*=flag;
}

struct edge{int from,to,cap,flow;};

struct isap
{
    int n,s,t,p[maxn],d[maxn],cur[maxn],num[maxn];
    bool vis[maxn];
    vector<int>g[maxn];
    vector<edge>edges;
    void init(int n,int s,int t) {
        this->n = n;
        this->s = s;
        this->t = t;
        for(int i = 1;i <= n;i++) g[i].clear();
        edges.clear();
    }
    void addegde(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 augment() {///找增广路
        int x = t,a = inf;
        while(x!=s) {
            a = min(a, edges[p[x]].cap - edges[p[x]].flow);
            x = edges[p[x]].from;
        }
        x=t;
        while(x != s) {
            edges[p[x]].flow += a;
            edges[p[x]^1].flow = -a;
            x = edges[p[x]].from;
        }
        return a;
    }
    int maxflow() {///更新最大流
        int flow = 0;
        memset(num, 0, sizeof(num));
        memset(cur, 0, sizeof(cur));
        for(int i = 1; i <= n; i++) num[d[i]]++;
        int x = s;
        while(d[s] < n) {///最长的一条链上,最大的下标是nv-1,如果大于等于nv说明已断层
            if(x == t) {
                flow += augment();
                x = s;//回退
            }
            bool ok = 0;
            for(int i = cur[x]; i < g[x].size(); i++) {
                edge &e = edges[g[x][i]];
                if(d[x] == d[e.to] + 1 && e.cap > e.flow) {
                    p[e.to] = g[x][i];
                    cur[x] = i;x = e.to;
                    ok = 1;
                    break;
                }
            }
            if(!ok) {
                int m = n-1;
                for(int i = 0; i < g[x].size();i++) {
                    edge &e=edges[g[x][i]];
                    if(e.cap>e.flow) m=min(m,d[e.to]);
                }
                num[d[x]]--;
                if(!num[d[x]]) break;
                d[x] = m+1;
                num[d[x]]++;
                cur[x] = 0;
                if(x != s) x = edges[p[x]].from;
            }
        }
        return flow;
    }
}ISAP;

int main()
{
    int n,m,s,t,u,v,w;
    n = 1007;
    s = 1,t = 26;
    cin >> m;
    ISAP.init(n,s,t);
    for(int i = 1; i <= m; i++) {
        char u,v;
        int c;
        cin >> u >> v >> c;
        ISAP.addegde(u - 'A' + 1,v - 'A' + 1,c);
    }
    printf("%d\n",ISAP.maxflow());
    return 0;
}