Island Transport(最大流模板)

 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
 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
struct haha{
	int to;
	int fr;
	int s;
	int biao;
}we[200010];
int f[200010];
int vis[200010];
int bis[200010];
int n,m;
int e;
void add(int ss,int tt,int w){
	we[e].to=ss;
	we[e].fr=tt;
	we[e].s=w;
	we[e].biao=f[ss];
	f[ss]=e++;
	
	we[e].to=tt;
	we[e].fr=ss;
	we[e].s=w;
	we[e].biao=f[tt];
	f[tt]=e++;
}
int bfs(int s,int t){
	memset(vis,0,sizeof(vis));
	queue<int>q;
	vis[s]=1;
	q.push(s);
	while(!q.empty()){
		int u=q.front();
		q.pop();
		if(u==t)
			return 1;
		for(int i=f[u];i!=-1;i=we[i].biao){
			int v=we[i].fr;
			if(vis[v]==0 && we[i].s){
				vis[v]=vis[u]+1;
				q.push(v);
			}
		}
	}
	return 0;
}
int dfs(int s,int mmax,int t){
	int flow;

    if(s == t)
        return mmax;

    int uflow = 0;

    for(int i = f[s]; i != -1; i = we[i].biao)
    {
        int v = we[i].fr;
        if(vis[v] == vis[s]+1 && we[i].s)
        {
            flow = min(mmax-uflow, we[i].s);  // 路径总流量减去从这分叉开的其他路的流量就是可能流过这个点的流量,取能流的最小值
            flow = dfs(v, flow, t);

            we[i].s -= flow;
            we[i^1].s += flow;
            uflow += flow;   //有可能找到不止一条路

            if(uflow == mmax)
                break;
        }
    }
    if(uflow == 0)
        vis[s] = 0;
    return uflow;
}
int din(int s,int t){
	int sum=0;
	while(bfs(s,t)){
		sum+=dfs(s,0x3f3f3f3f,t);
	}
	return sum;
}
int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		scanf("%d %d",&n,&m);
		int minn=0x3f3f3f3f,maxx=-0x3f3f3f3f,xx,yy;
		memset(f,-1,sizeof(f));
		int x,y,c;
		e=0;
		for(int i=1;i<=n;i++){
			scanf("%d %d",&x,&y);
			if(x>maxx){//最西 
				maxx=x;
				xx=i;
			}
			if(x<minn){//最东 
				minn=x;
				yy=i;
			}
		}
		for(int i=1;i<=m;i++){
			scanf("%d %d %d",&x,&y,&c);
			add(x,y,c);
		}
		printf("%d\n",din(xx,yy));
	}
} 


Sample Output
 
  
9 6

猜你喜欢

转载自blog.csdn.net/doublekillyeye/article/details/80697558