lightoj 1153 - Internet Bandwidth 【无向图最大流】

1153 - Internet Bandwidth
PDF (English) Statistics Forum
Time Limit: 2 second(s) Memory Limit: 32 MB

On the Internet, machines (nodes) are richly interconnected, and many paths may exist between a given pair of nodes. The total message-carrying capacity (bandwidth) between two given nodes is the maximal amount of data per unit time that can be transmitted from one node to the other. Using a technique called packet switching; this data can be transmitted along several paths at the same time.

For example, the following figure shows a network with four nodes (shown as circles), with a total of five connections among them. Every connection is labeled with a bandwidth that represents its data-carrying capacity per unit time.

In our example, the bandwidth between node 1 and node 4 is 25, which might be thought of as the sum of the bandwidths 10 along the path 1-2-4, 10 along the path 1-3-4, and 5 along the path 1-2-3-4. No other combination of paths between nodes 1 and 4 provides a larger bandwidth.

You must write a program that computes the bandwidth between two given nodes in a network, given the individual bandwidths of all the connections in the network. In this problem, assume that the bandwidth of a connection is always the same in both directions (which is not necessarily true in the real world).

Input

Input starts with an integer T (≤ 30), denoting the number of test cases.

Every description starts with a line containing an integer n (2 ≤ n ≤ 100), which is the number of nodes in the network. The nodes are numbered from 1 to n. The next line contains three numbers st, and c. The numbers s and t are the source and destination nodes, and the number c (c ≤ 5000, s ≠ t) is the total number of connections in the network. Following this are c lines describing the connections. Each of these lines contains three integers: the first two are the numbers of the connected nodes, and the third number is the bandwidth of the connection. The bandwidth is a non-negative number not greater than 1000.

There might be more than one connection between a pair of nodes, but a node cannot be connected to itself. All connections are bi-directional, i.e. data can be transmitted in both directions along a connection, but the sum of the amount of data transmitted in both directions must be less than the bandwidth.

Output

For each case of input, print the case number and the total bandwidth between the source node s and the destination node t.

Sample Input

Output for Sample Input

2

4

1 4 5

1 2 20

1 3 10

2 3 5

2 4 10

3 4 20

4

1 4 2

1 4 20

1 4 20

Case 1: 25

Case 2: 40

 

SPECIAL THANKS: JANE ALAM JAN (SOLUTION, DATASET)


题意:给出一个无向图——n个点,m条边和每条边的容量,指定源点汇点s、t,问你s -> t的最大流。

以前没做过这样的网络流题目,还以为又是新东西。。。 o(╯□╰)o

思路:建图时,把反向弧的容量设为该边的容量,然后跑一次最大流就ok了。


AC代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define MAXM 20000+10
#define MAXN 200+10
#define INF 0x3f3f3f3f
using namespace std;
struct MAXFLOW
{
    struct Edge{
        int from, to, cap, flow, next;
    };
    Edge edge[MAXM];
    int head[MAXN], edgenum;
    int cur[MAXN];
    void init(){
        edgenum = 0;
        memset(head, -1, sizeof(head));
    }
    void addEdge(int u, int v, int w)
    {
        Edge E1 = {u, v, w, 0, head[u]};
        edge[edgenum] = E1;
        head[u] = edgenum++;
        Edge E2 = {v, u, w, 0, head[v]};//注意反向弧 容量为w
        edge[edgenum] = E2;
        head[v] = edgenum++;
    }
    int dist[MAXN]; bool vis[MAXN];
    bool BFS(int s, int t)
    {
        queue<int> Q;
        memset(dist, -1, sizeof(dist));
        memset(vis, false, sizeof(vis));
        dist[s] = 0;
        Q.push(s);
        vis[s] = true;
        while(!Q.empty())
        {
            int u = Q.front(); Q.pop();
            for(int i = head[u]; i != -1; i = edge[i].next)
            {
                Edge E = edge[i];
                if(!vis[E.to] && E.cap > E.flow)
                {
                    dist[E.to] = dist[u] + 1;
                    if(E.to == t) return true;
                    vis[E.to] = true;
                    Q.push(E.to);
                }
            }
        }
        return false;
    }
    int DFS(int x, int a, int t)
    {
        if(x == t || a == 0) return a;
        int flow = 0, f;
        for(int &i = cur[x]; i != -1; i = edge[i].next)
        {
            Edge &E = edge[i];
            if(dist[E.to] == dist[x] + 1 && (f = DFS(E.to, min(a, E.cap-E.flow), t)) > 0)
            {
                edge[i].flow += f;
                edge[i^1].flow -= f;
                flow += f;
                a -= f;
                if(a == 0) break;
            }
        }
        return flow;
    }
    int Maxflow(int s, int t)
    {
        int flow = 0;
        while(BFS(s, t))
        {
            memcpy(cur, head, sizeof(head));
            flow += DFS(s, INF, t);
        }
        return flow;
    }
};
MAXFLOW dinic;

int k = 1;
int S, T;
int N, M;
void solve()
{
    scanf("%d", &N);
    scanf("%d%d%d", &S, &T, &M);
    dinic.init();
    while(M--)
    {
        int a, b, c;
        scanf("%d%d%d", &a, &b, &c);
        dinic.addEdge(a, b, c);
    }
    printf("Case %d: %d\n", k++, dinic.Maxflow(S, T));
}
int main()
{
    int t;
    scanf("%d", &t);
    while(t--){
        solve();
    }
    return 0;
}


你可能感兴趣的:(lightoj 1153 - Internet Bandwidth 【无向图最大流】)