HDU4280

本文介绍了一种计算在多个岛屿间通过特定航线进行的最大运输能力的方法。在一个由多个岛屿组成的系统中,每条航线连接两个岛屿,并且可以双向运输乘客。文章提供了一个具体的算法实现,用于确定从最西端到最东端岛屿的最大小时运输容量。

In the vast waters far far away, there are many islands. People are living on the islands, and all the transport among the islands relies on the ships.
  You have a transportation company there. Some routes are opened for passengers. Each route is a straight line connecting two different islands, and it is bidirectional. Within an hour, a route can transport a certain number of passengers in one direction. For safety, no two routes are cross or overlap and no routes will pass an island except the departing island and the arriving island. Each island can be treated as a point on the XY plane coordinate system. X coordinate increase from west to east, and Y coordinate increase from south to north.
  The transport capacity is important to you. Suppose many passengers depart from the westernmost island and would like to arrive at the easternmost island, the maximum number of passengers arrive at the latter within every hour is the transport capacity. Please calculate it.
Input
  The first line contains one integer T (1<=T<=20), the number of test cases.
  Then T test cases follow. The first line of each test case contains two integers N and M (2<=N,M<=100000), the number of islands and the number of routes. Islands are number from 1 to N.
  Then N lines follow. Each line contain two integers, the X and Y coordinate of an island. The K-th line in the N lines describes the island K. The absolute values of all the coordinates are no more than 100000.
  Then M lines follow. Each line contains three integers I1, I2 (1<=I1,I2<=N) and C (1<=C<=10000) . It means there is a route connecting island I1 and island I2, and it can transport C passengers in one direction within an hour.
  It is guaranteed that the routes obey the rules described above. There is only one island is westernmost and only one island is easternmost. No two islands would have the same coordinates. Each island can go to any other island by the routes.
Output
  For each test case, output an integer in one line, the transport capacity.
Sample Input
2
5 7
3 3
3 0
3 1
0 0
4 5
1 3 3
2 3 4
2 4 3
1 5 6
4 5 3
1 4 4
3 4 2
6 7
-1 -1
0 1
0 2
1 0
1 1
2 3
1 2 1
2 3 6
4 5 5
5 6 3
1 4 6
2 5 5
3 6 4
Sample Output
9
6

感觉是个水题。。

其实就是个水题。。

#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<stack>
using namespace std;
const int inf = 0x3fffffff;
template <int N, int M>
struct Isap
{
    int top;
    int d[N], pre[N], cur[N], gap[N];
    struct Vertex {
        int head;
    } V[N];
    struct Edge {
        int v, next;
        int c, f;
    } E[M];
    void init() {
        memset(V, -1, sizeof(V));
        top = 0;
    }
    void add_edge(int u, int v, int c) {
        E[top].v = v;
        E[top].c = c;
        E[top].f = 0;
        E[top].next = V[u].head;
        V[u].head = top++;
    }
    void add(int u, int v, int c) {
        add_edge(u, v, c);
        add_edge(v, u, 0);
    }
    void set_d(int t) {
        queue<int> Q;
        memset(d, -1, sizeof(d));
        memset(gap, 0, sizeof(gap));
        d[t] = 0;
        Q.push(t);
        while (!Q.empty()) {
            int v = Q.front(); Q.pop();
            ++gap[d[v]];
            for (int i = V[v].head; ~i; i = E[i].next) {
                int u = E[i].v;
                if (d[u] == -1) {
                    d[u] = d[v] + 1;
                    Q.push(u);
                }
            }
        }
    }
    int sap(int s, int t, int num) {
        set_d(t);
        int ans = 0, u = s;
        int flow = inf;
        memcpy(cur, V, sizeof(V));
        while (d[s] < num) {
            int &i = cur[u];
            for (; ~i; i = E[i].next) {
                int v = E[i].v;
                if (E[i].c > E[i].f && d[u] == d[v] + 1) {
                    u = v;
                    pre[v] = i;
                    flow = min(flow, E[i].c - E[i].f);
                    if (u == t) {
                        while (u != s) {
                            int j = pre[u];
                            E[j].f += flow;
                            E[j ^ 1].f -= flow;
                            u = E[j ^ 1].v;
                        }
                        ans += flow;
                        flow = inf;
                    }
                    break;
                }
            }
            if (i == -1) {
                if (--gap[d[u]] == 0)
                    break;
                int dmin = num - 1;
                cur[u] = V[u].head;
                for (int j = V[u].head; ~j; j = E[j].next)
                    if (E[j].c > E[j].f)
                        dmin = min(dmin, d[E[j].v]);
                d[u] = dmin + 1;
                ++gap[d[u]];
                if (u != s)
                    u = E[pre[u] ^ 1].v;
            }
        }
        return ans;
    }
};
Isap<200005, 400005> Sap;
int tux[100001], tuy[100001];
int main()
{
    int T;
    cin >> T;
    while (T--)
    {
        int n, m;
        cin >> n >> m;
        int st = 9999999, zt = -1;
        int stx = 9999999, ztx = -1;
        for (int a = 1; a <= n; a++)
        {
            scanf("%d%d", &tux[a], &tuy[a]);
            if (stx>tux[a])
            {
                stx = tux[a];
                st = a;
            }
            if (ztx<tux[a])
            {
                ztx = tux[a];
                zt = a;
            }
        }
        int q, w, e;
        Sap.init();
        for (int a = 1; a <= m; a++)
        {
            scanf("%d%d%d", &q, &w, &e);
            Sap.add(q, w, e);
            Sap.add(w, q, e);
        }
        cout << Sap.sap(st, zt, 9 * m + 2) << endl;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值