HDU - 4280 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
Sample Output
9
6

题解:

题不难,就是最大流的基础题。不过看这个数据范围就知道是道卡时间的题,用dinic的话需要优化的好点要不然过的很边缘(比如说我)。

代码:

9656ms

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>

using namespace std;

const int INF = 0x3f3f3f3f;
const int MAXN = 100010;

struct Edge{
	int to,flow,rev;
	Edge(){}
	Edge(int a,int b,int c):to(a),flow(b),rev(c){}
};

vector<Edge> E[MAXN];

inline void Add(int from,int to,int flow){
	E[from].push_back(Edge(to,flow,E[to].size()));
	E[to].push_back(Edge(from,flow,E[from].size()-1));//注意因为是双向边所以这里flow不是0
}

int deep[MAXN];
int iter[MAXN];
 
int DFS(int from,int to,int flow){
	if(from == to || flow == 0)return flow;
	
	for(int& i=iter[from] ; i<E[from].size() ; ++i){
		Edge& e = E[from][i];
		if(e.flow > 0 && deep[e.to] == deep[from]+1){
			int nowflow = DFS(e.to,to,min(flow,e.flow));
			if(nowflow > 0){
				e.flow -= nowflow;
				E[e.to][e.rev].flow += nowflow;
				return nowflow;
			}
		}
	}
	return 0;
}
 
int Dinic(int from,int to){//这里把BFS放进了Dinic里,能快一点是一点。
	int sumflow = 0;
	while(1){
		memset(deep,-1,sizeof deep);
		deep[from] = 0;
		queue<int> Q;
		Q.push(from);
		while(!Q.empty()){
			int t = Q.front();
			Q.pop();
			for(int i=0 ; i<E[t].size() ; ++i){
				Edge& e = E[t][i];
				if(e.flow > 0 && deep[e.to] == -1){
					deep[e.to] = deep[t] + 1;
					Q.push(e.to);
				}
			}
		}
		if(deep[to] == -1)break;
		memset(iter,0,sizeof iter);
		int mid;
		while((mid=DFS(from,to,INF)) > 0)sumflow += mid;
	}
	return sumflow;
}

int main(){
	
	int T,N,M;
	int maxwest,t1;
	int maxeast,t2;
	scanf("%d",&T);
	while(T--){
		scanf("%d %d",&N,&M);
		maxwest = INF;
		maxeast = -INF;
		for(int i=1 ; i<=N ; ++i){
			int a,b;
			scanf("%d %d",&a,&b);
			if(a < maxwest){
				maxwest = a;
				t1 = i;
			}
			if(a > maxeast){
				maxeast = a;
				t2 = i;
			}
		}
		for(int i=0 ; i<M ; ++i){
			int a,b,c;
			scanf("%d %d %d",&a,&b,&c);
			Add(a,b,c);
		}
		printf("%d\n",Dinic(t1,t2));
		for(int i=0 ; i<=N ; ++i)E[i].clear();
	} 
	
	return 0;
} 

posted @ 2018-07-08 20:18  Assassin_poi君  阅读(167)  评论(0编辑  收藏  举报