HDU 4280 Island Transport 网络流
2014-05-31 12:02
316 查看
Island Transport
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 4167 Accepted Submission(s): 1410
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
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
Source
2012 ACM/ICPC Asia Regional Tianjin Online
题目链接:HDU 4280 Island Transport
题目分析:最大流果题
#include <stdio.h> #include <string.h> #include <algorithm> #define REP(I, X) for(int I = 0; I < X; ++I) #define FF(I, A, B) for(int I = A; I <= B; ++I) #define clear(A, B, SIZE) memset(A, B, sizeof(A[0]) * (SIZE + 1)) #define copy(A, B, SIZE) memcpy(A, B, sizeof(A[0]) * (SIZE + 1)) #define min(A, B) ((A) < (B) ? (A) : (B)) #define max(A, B) ((A) > (B) ? (A) : (B)) using namespace std; typedef long long ll; typedef long long LL; const int oo = 0x3f3f3f3f; const int maxE = 1000000; const int maxN = 100005; const int maxQ = 10000; struct Edge{ int v, c, n; }; Edge edge[maxE]; int adj[maxN], cntE; int Q[maxE], head, tail, inq[maxN]; int d[maxN], num[maxN], cur[maxN], pre[maxN]; int s, t, nv; int n, m, nm; int path[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; void addedge(int u, int v, int c){ edge[cntE].v = v; edge[cntE].c = c; edge[cntE].n = adj[u]; adj[u] = cntE++; edge[cntE].v = u; edge[cntE].c = c;//有向边c = 0; edge[cntE].n = adj[v]; adj[v] = cntE++; } void REV_BFS(){ clear(d, -1, n + 1); clear(num, 0, n + 1); head = tail = 0; d[t] = 0; num[0] = 1; Q[tail++] = t; while(head != tail){ int u = Q[head++]; for(int i = adj[u]; ~i; i = edge[i].n){ int v = edge[i].v; if(~d[v]) continue; d[v] = d[u] + 1; num[d[v]]++; Q[tail++] = v; } } } int ISAP(){ copy(cur, adj, n + 1); REV_BFS(); int flow = 0, u = pre[s] = s, i; while(d[s] < nv){ if(u == t){ int f = oo, neck; for(i = s; i != t; i = edge[cur[i]].v){ if(f > edge[cur[i]].c){ f = edge[cur[i]].c; neck = i; } } for(i = s; i != t; i = edge[cur[i]].v){ edge[cur[i]].c -= f; edge[cur[i] ^ 1].c += f; } flow += f; u = neck; } for(i = cur[u]; ~i; i = edge[i].n) if(edge[i].c && d[u] == d[edge[i].v] + 1) break; if(~i){ cur[u] = i; pre[edge[i].v] = u; u = edge[i].v; } else{ if(0 == (--num[d[u]])) break; int mind = nv; for(i = adj[u]; ~i; i = edge[i].n){ if(edge[i].c && mind > d[edge[i].v]){ mind = d[edge[i].v]; cur[u] = i; } } d[u] = mind + 1; num[d[u]]++; u = pre[u]; } } return flow; } int read(){ int flag = 0, x = 0; char ch = ' '; while(ch != '-' && (ch > '9' || ch < '0')) ch = getchar(); if(ch == '-') flag = 1, ch = getchar(); while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return flag ? -x : x; } void work(){ int x, u, v, c; int min = oo, lid = 0, max = -oo, rid = 0; n = read(); m = read(); clear(adj, -1, n + 1); cntE = 0; FF(i, 1, n){ x = read(); read();//第二个无用 if(x < min){ min = x; lid = i; } if(x > max){ max = x; rid = i; } } s = lid; t = rid; nv = n + 1; REP(i, m){ u = read(); v = read(); c = read(); addedge(u, v, c); } printf("%d\n", ISAP()); } int main(){ int T; for(scanf("%d", &T); T; --T) work(); return 0; }
相关文章推荐
- HDU 4280 Island Transport 网络流sap
- HDU 4280 Island Transport(网络流)
- HDU 4280 Island Transport (网络流 sap)
- HDU 4280 Island Transport 网络流
- hdu 4280 Island Transport 【图论-网络流-ISAP+栈优化】
- HDU 4280 Island Transport(网络流)
- HDU 4280 Island Transport 网络流裸题
- HDU 4280 Island Transport(网络流,SAP)
- HDU 4280 Island Transport (网络流)
- hdu 4280 Island Transport 【网络流+优化】
- HDU 4280 Island Transport(网络流)
- HDU 4280 Island Transport(网络流,最大流)
- HDU 4280 Island Transport(网络流)
- Island Transport HDU - 4280 (无向图形网络流)
- HDU - 4280 Island Transport (最大流 邻接表)
- Island Transport HDU - 4280 最大流 ISAP
- HDU - 4280 Island Transport(ISAP)
- HDU 4280 Island Transport (ISPA)
- 栈优化最大流-HDU-4280-Island Transport
- hdu 4280 Island Transport (最大流)