Island Transport HDU - 4280(網絡流基礎)

  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

題意:有N個島嶼 M條無向路 每個路有一最大允許的客流量,求從最西的那個島嶼最多能運用多少乘客到最東的那個島嶼。

對於網絡流的詳細理解:https://www.cnblogs.com/ZJUT-jiangnan/p/3632525.html

代碼:

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define inf 0x3f3f3f3f
int fir[100020],nex[100020*2],to[100020*2];
int c[100020*2] ;//容量
int r;
int s,t,lev[100010*2];
void build(int x,int y,int v)
{
    to[r]=y;
    c[r]=v;
    nex[r]=fir[x];
    fir[x]=r++;//正向弧

    to[r]=x;
    c[r]=v;
    nex[r]=fir[y];
    fir[y]=r++;//反向弧
}
int bfs()//尋找增廣路
{
    queue<int>q;
    memset(lev,0,sizeof(lev));
    lev[s]=1;//起點的深度爲1
    q.push(s);
    while(!q.empty())//隊列不爲空
    {
        int f=q.front();
        q.pop();
        if(f==t) return 1;//如果當前點爲匯點,表示存在增廣路
        for(int i=fir[f];i!=-1;i=nex[i])//以f爲頂點的邊的數量
        {
            int u=to[i];
            if(lev[u]==0&&c[i]>0)//如果該點未被搜索過,且容量大於零
            {
                lev[u]=lev[f]+1;//標記該點,並記錄該點所在的深度,且把該點進入隊列
                q.push(u);
            }
        }
    }
    return lev[t]!=0;
}
int dfs(int x,int flow)//尋找最小殘容量
{
    if(x==t) return flow;
    int sum=0;
    for(int i=fir[x];i!=-1;i=nex[i])
    {
        int u=to[i];
        if(lev[u]==lev[x]+1&&c[i]>0)
        {
            int df=dfs(u,min(flow-sum,c[i]));
            c[i]-=df;
            c[i^1]+=df;
            sum+=df;
            if(sum==flow)
                break;
        }
    }
    if(sum==0) lev[x]=-1;
    return sum;
}
int dinic()
{
    int ans=0;
    while(bfs())
    {
        ans+=dfs(s,inf);
    }
    return ans;
}
int main()
{
    int tt;
    scanf("%d",&tt);
    while(tt--)
    {
        int maxx=-inf,minn=inf;
        memset(fir,-1,sizeof(fir));
        r=0;
        int n,m;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)//找源點和匯點
        {
            int x,y;
            scanf("%d%d",&x,&y);
            if(x>maxx) maxx=x,t=i;
            if(x<minn) minn=x,s=i;
        }
        for(int i=1;i<=m;i++)
        {
            int a,b,v;
            scanf("%d%d%d",&a,&b,&v);
            build(a,b,v);
        }
        printf("%d\n",dinic());
    }
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章