1. 程式人生 > >G - Island Transport 網絡流

G - Island Transport 網絡流

rom art http mic pac passenger span 有一點 ear

  題目:  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 <cstdio>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#include <map>
#include <cstring>
#include <string>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn = 1e5 + 5;
const int INF = 0x3f3f3f3f;
struct edge
{
    int u, v, c, f;
    edge(int u, int v, int c, int f) :u(u), v(v), c(c), f(f) {}
};
vector<edge>e;
vector<int>G[maxn];
int level[maxn];//BFS分層,表示每個點的層數
int iter[maxn];//當前弧優化
int m, s, t;
void init(int n)
{
    for (int i = 0; i <= n; i++)G[i].clear();
    e.clear();
}
void add(int u, int v, int c)
{
    e.push_back(edge(u, v, c, 0));
    e.push_back(edge(v, u, c, 0));
    m = e.size();
    G[u].push_back(m - 2);
    G[v].push_back(m - 1);
}
void BFS(int s)//預處理出level數組
//直接BFS到每個點
{
    memset(level, -1, sizeof(level));
    queue<int>q;
    level[s] = 0;
    q.push(s);
    while (!q.empty())
    {
        int u = q.front();
        if (u == t) return;
        q.pop();
        for (int v = 0; v < G[u].size(); v++)
        {
            edge& now = e[G[u][v]];
            if (now.c > now.f && level[now.v] < 0)
            {
                level[now.v] = level[u] + 1;
                q.push(now.v);
            }
        }
    }
}
int dfs(int u, int t, int f)//DFS尋找增廣路
{
    if (u == t)return f;//已經到達源點,返回流量f
    for (int &v = iter[u]; v < G[u].size(); v++)
        //這裏用iter數組表示每個點目前的弧,這是為了防止在一次尋找增廣路的時候,對一些邊多次遍歷
        //在每次找增廣路的時候,數組要清空
    {
        edge &now = e[G[u][v]];
        if (now.c - now.f > 0 && level[u] < level[now.v])
            //now.c - now.f > 0表示這條路還未滿
            //level[u] < level[now.v]表示這條路是最短路,一定到達下一層,這就是Dinic算法的思想
        {
            int d = dfs(now.v, t, min(f, now.c - now.f));
            if (d > 0)
            {
                now.f += d;//正向邊流量加d
                e[G[u][v] ^ 1].f -= d;
                //反向邊減d,此處在存儲邊的時候兩條反向邊可以通過^操作直接找到
                return d;
            }
        }
    }
    return 0;
}
int Maxflow(int s, int t)
{
    int flow = 0;
    for (;;)
    {
        BFS(s);
        if (level[t] < 0)return flow;//殘余網絡中到達不了t,增廣路不存在
        memset(iter, 0, sizeof(iter));//清空當前弧數組
        int f;//記錄增廣路的可增加的流量
        while ((f = dfs(s, t, INF)) > 0)
        {
            flow += f;
        }
    }
    return flow;
}


int main()
{
    int qw;
    scanf("%d", &qw);
    while(qw--)
    {
        
        int n, m;
        scanf("%d%d", &n, &m);
        init(n);
        int mans = inf, mark = 0;
        int mana = -inf, mark1 = 0;
        for(int i=1;i<=n;i++)
        {
            int x, y;
            scanf("%d%d", &x, &y);
            if(x<mans)
            {
                mans = x;
                s = i;
            }
            if(x>mana)
            {
                mana = x;
                t = i;
            }
        }
        for(int i=1;i<=m;i++)
        {
            int x, y, c;
            scanf("%d%d%d", &x, &y, &c);
            add(x, y, c);
        }
        int ans = Maxflow(s, t);
        printf("%d\n", ans);
    }
    return 0;
}









G - Island Transport 網絡流