HDU 4280 Island Transport(最大流)

Description:

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

题目链接

裸最大流题目,但是很卡时限,可以用来优化自己的最大流模板。

TLE不一定是超时,也可能是数组开的小了。

AC代码:

#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define pb push_back
#define mp make_pair
#define lowbit(x) (x&(-x))
#define min(x,y) x>y?y:x
#define XDebug(x) cout<<#x<<"="<<x<<endl;
#define ArrayDebug(x,i) cout<<#x<<"["<<i<<"]="<<x[i]<<endl;
#define print(x) out(x);putchar('\n')
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<double,double> PDD;
typedef pair<ll,ll> PLL;
const int INF = 0x7fffffff;
const int maxn = 1e5 + 5;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = asin(1.0) * 2;
const double e = 2.718281828459;
template <class T>
inline bool read(T &ret) {
    char c;
    int sgn;
    if (c = getchar(), c == EOF) {
        return false;
    }
    while (c != '-' && (c < '0' || c > '9')) {
        c = getchar();
    }
    sgn = (c == '-') ? -1 : 1;
    ret = (c == '-') ? 0 : (c - '0');
    while (c = getchar(), c >= '0' && c <= '9') {
        ret = ret * 10 + (c - '0');
    }
    ret *= sgn;
    return true;
}
template <class T>
inline void out(T x) {
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    if (x > 9) {
        out(x / 10);
    }
    putchar(x % 10 + '0');
}

struct Edge {
    int V, W, Next;
    Edge(int _V = 0, int _W = 0, int _Next = -1): V(_V), W(_W), Next(_Next) {}
};

Edge edges[maxn << 1];
int Head[maxn];
int Tot;
int Depth[maxn];
int T;
int N, E;
int Current[maxn];
queue<int> Que;

void Init() {
    Tot = 0;
    mem(Head, -1);
}

void AddEdge(int U, int V, int W) {
    edges[Tot].V = V;
    edges[Tot].W = W;
    edges[Tot].Next = Head[U];
    Head[U] = Tot++;
}

bool Bfs(int Start, int End) {
    for (int i = 0; i <= N; ++i) {
        Depth[i] = -1;
    }
    while (!Que.empty()) {
        Que.pop();
    }
    Depth[Start] = 0;
    Que.push(Start);
    while (!Que.empty()) {
        int Vertex = Que.front(); Que.pop();
        if (Vertex == End) {
            break;
        }
        for (int i = Head[Vertex]; i != -1; i = edges[i].Next) {
            int V = edges[i].V;
            if (Depth[V] == -1 && edges[i].W > 0) {
                Depth[V] = Depth[Vertex] + 1;
                Que.push(V);
            }
        }
    }
    return Depth[End] != -1;
}

int Dfs(int Vertex, int End, int NowFlow) {
    if (Vertex == End || NowFlow == 0) {
        return NowFlow;
    }
    int FindFlow = 0, Flow;
    for (int &i = Current[Vertex]; i != -1; i = edges[i].Next) {
        int V = edges[i].V;
        if (edges[i].W > 0 && Depth[V] == Depth[Vertex] + 1) {
            Flow = Dfs(V, End, min(NowFlow - FindFlow, edges[i].W));
            if (Flow > 0) {
                edges[i].W -= Flow;
                edges[i ^ 1].W += Flow;
                FindFlow += Flow;
                if (FindFlow == NowFlow) {
                    return NowFlow;
                }
            }
        }
    }
    if (!FindFlow) {
        Depth[Vertex] = -2;
    }
    return FindFlow;
}

int Dinic(int Start, int End) {
    int MaxFlow = 0;
    while (Bfs(Start, End)) {
        for (int i = 1; i <= N; ++i) {
            Current[i] = Head[i];
        }
        MaxFlow += Dfs(Start, End, INF);
    }
    return MaxFlow;
}

int main(int argc, char *argv[]) {
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    scanf("%d", &T);
    for (int Case = 1; Case <= T; ++Case) {
        Init();
        int Start = INF, End = -INF;
        int StartIndex = 1, EndIndex = 1;
        scanf("%d%d", &N, &E);
        for (int i = 1, X, Y; i <= N; ++i) {
            scanf("%d%d", &X, &Y);
            if (X < Start) {
                Start = X;
                StartIndex = i;
            }
            if (X > End) {
                End = X;
                EndIndex = i;
            }
        }
        for (int i = 1, U, V, W; i <= E; ++i) {
            scanf("%d%d%d", &U, &V, &W);
            AddEdge(U, V, W);
            AddEdge(V, U, W);
        }
        printf("%d\n", Dinic(StartIndex, EndIndex));
    }
#ifndef ONLINE_JUDGE
    fclose(stdin);
    fclose(stdout);
    system("gedit out.txt");
#endif
    return 0;
}
全部评论

相关推荐

那么好了好了:他本来公司就是做这个的,不就是正常的游戏客户端和服务器开发,软硬件联动,有啥恶心不恶心的,提前告诉你就是怕你接受不了,接受不了就没必要再往后走流程浪费时间,虽然这公司是一坨。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务