Island Transport
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 8100 Accepted Submission(s): 2573
Problem Description
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
25 73 33 13 00 01 3 34 52 3 44 5 32 4 31 5 6-1 -11 4 43 4 22 36 70 11 00 21 15 6 31 2 12 3 63 6 44 5 51 4 62 5 5
Sample Output
96
#include <queue> #include <functional> #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <stack> #include <vector> #include <set> #include <map> #include <string> #include <cmath> #include <cstdlib> #include <ctime> #include <assert.h> #define REP(i,k,n) for(int i=k;i<n;i++) #define REPP(i,k,n) for(int i=k;i<=n;i++) #define scan(d) scanf("%d",&d) #define scann(n,m) scanf("%d%d",&n,&m) #define mst(a,k) memset(a,k,sizeof(a)); #define LL long long #define eps 1e-8 #define INF 0x3f3f3f3f #define inf INF*INF #define mod 1000000007 using namespace std; #define N 100005 #define M 400005 struct edge { int u,v,flow,next; }edge[N<<1]; int head[N]; int d[N]; //距离汇点的距离 int cnt[N]; //cnt[a] 距离汇点为a的点的个数 int n; int q[N]; //在一次寻找中,记录从 源点 到 汇点 用过的边 int top; //当前存入 数组 q中 边数 int low[N]; //记录 到 low[top] 的所有边的最小流值 int cur[N]; //一开始将其与 head[] 数组相同,不断修正以剪枝 int Max_flow(int s,int e) { REPP(i,1,n) d[i]=n,cnt[i]=0; //一开始距 汇点 的距离都置为 n cnt[n] = n-1; d[e] = 0; //汇点 距离本身为0 cnt[0]++; queue<int>que; que.push(e); while(!que.empty()){ //BFS 求 d[] int w = que.front(); que.pop(); for(int i=head[w]; ~i; i=edge[i].next){ int v = edge[i].v; if(d[v]>d[w]+1 && edge[i].flow>0){ cnt[d[v]]--; d[v]=d[w]+1; cnt[d[v]]++; que.push(v); } } } int flow = 0, u = s; //flow 记录所有的流 top = 0; //u为当前处理的点 low[0] = INF; for(int i = 1; i <= n; ++i) { cur[i] = head[i]; } while(d[s] < n) { int &i = cur[u]; for(; i != -1; i = edge[i].next) { if(edge[i].flow > 0 && d[u] == d[edge[i].v]+1) { //找到则跳出 low[top+1] = min(low[top], edge[i].flow); q[++top] = i; u = edge[i].v; break; } } if(i != -1) { if(u == e) { //如果当前点为汇点 int minf = low[top]; for(int p = 1, i; p <= top; ++p) { i = q[p]; edge[i].flow -= minf; edge[i^1].flow += minf; //增加回流,再下次查找中如果用到则可以抵消,实现増广 } flow += minf; //答案加上这次的流量 u = s; low[0] = INF; top = 0; } } else { //否则 int old_du = d[u]; //记录当前点 cnt[old_du]--; d[u] = n-1; for(int i = head[u]; i != -1; i = edge[i].next) //寻找当前点是否存在可以进行增广的边 if(edge[i].flow > 0 && d[u] > d[edge[i].v]) { d[u] = d[edge[i].v]; } cnt[++d[u]]++; if(d[u]<n) //如果 d[u]==n 则说明此点不可用,及cur[u]=-1,下次再遇到cur【u】, i = -1 cur[u] = head[u]; if(u != s) { //注意! 如果是源点,则不可以退一步 u = edge[q[top]].u; --top; } if(cnt[old_du] == 0) break; //如果距离 汇点 为 d[old_du] 的点不存在,说明什么? 源点的流量不可能再到达汇点 } } return flow; } int main() { int t; scan(t); while(t--){ int l=INF,r=-INF; int m; scann(n,m); int st,ed; REPP(i,1,n){ int a,b; scann(a,b); if(a<l){ l=a; st=i; } if(a>r){ r=a; ed=i; } } // cout<<endl<<st<<" "<<ed<<endl; int tot=0; mst(head,-1); while(m--){ int a,b,c; scann(a,b); scan(c); edge[tot].u = a; edge[tot].v = b; edge[tot].flow = c; edge[tot].next = head[a]; head[a] = tot++; edge[tot].u = b; edge[tot].v = a; edge[tot].flow = c; edge[tot].next = head[b]; head[b] = tot++; } printf("%d\n",Max_flow(st,ed)); } }