首页 > ACM题库 > HDU-杭电 > HDU 4280-Island Transport[解题报告]HOJ
2015
05-23

HDU 4280-Island Transport[解题报告]HOJ

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.

输入:

  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.

输出:

  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.

样例输入:

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

样例输出:

9
6

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

using namespace std;

const int N=100010, E=400010, INF=0x3fffffff;

struct Edge
{
	int en, cap, flow, next, low;
	inline void init(int a, int b, int c, int d, int e)
	{
		en=a, cap=b, flow=c, next=d, low=e;
	}
} edge[E];
int head[N], now[N], dis[N], gap[N], pre[N], D[N];
int source, sink, tot_num, tot, n, m, t;
queue <int> q;

void add_edge(int st, int en, int cap, int low)
{
	edge[tot].init(en, cap, 0, head[st], low), head[st]=tot++;
	edge[tot].init(st, 0, 0, head[en], low), head[en]=tot++;
}

void BFS()
{
	memset(dis, -1, sizeof dis);
	memset(gap, 0, sizeof gap);
	while(!q.empty()) q.pop();
	gap[dis[sink]=0]=1, q.push(sink);
	for(int u=q.front(), i; !q.empty(); q.pop(), u=q.front())
		for(int v=edge[i=head[u]].en; i!=-1; v=edge[i=edge[i].next].en)
		{
			if(edge[i].cap!=0 || dis[v]!=-1) continue;
			q.push(v);
			++gap[dis[v]=dis[u]+1];
		}
}

int SAP()
{
	memset(pre, -1, sizeof pre);
	BFS();
	memcpy(now, head, sizeof head);
	int cur=source, flow=0, cut=INF;
	for(bool fg=0; dis[source]<tot_num; fg=0)
	{
		for(int i=now[cur]; i!=-1; i=edge[i].next)
		{
			if(!(edge[i].cap-edge[i].flow>0 && dis[cur]==dis[edge[i].en]+1)) continue;
			cut=min(cut, edge[i].cap-edge[i].flow);
			now[cur]=i, pre[edge[i].en]=cur;
			cur=edge[i].en;
			if(cur==sink)
			{
				flow+=cut;
				for(int u=source; u!=sink; u=edge[now[u]].en)
					edge[now[u]].flow+=cut, edge[now[u]^1].flow-=cut;
				cur=source, cut=INF;
			}
			fg=true;
			break;
		}
		if(fg) continue;
		if(--gap[dis[cur]]==0) break;
		int Min=tot_num;
		for(int i=head[cur]; i!=-1; i=edge[i].next)
			if(edge[i].cap-edge[i].flow>0 && Min>dis[edge[i].en])
                Min=dis[edge[i].en], now[cur]=i;
		gap[dis[cur]=Min+1]++;
		if(cur!=source) cur=pre[cur];
	}
	return flow;
}

void build(int n,int m)
{
	memset(head, -1, sizeof head);
	tot=0, tot_num=n+1;
	int Min=INF, Max=-INF;
	for(int i=1, x, y; i<=n; i++)
	{
		scanf("%d%d", &x, &y);
		if(Min>x) Min=x, source=i;
		if(Max<x) sink=i, Max=x;
	}
	for(int i=1, a, b, c; i<=m; i++)
	{
		scanf("%d%d%d", &a, &b, &c);
		add_edge(a, b, c, 0);
		add_edge(b, a ,c, 0);
	}
}

int main()
{
	scanf("%d", &t);
	for(int ca=1; ca<=t; ca++)
	{
		scanf("%d%d", &n, &m);
		build(n, m);
		int ans=SAP();
		printf("%d\n", ans);
	}
	return 0;
}