您的位置:首页 > 其它

Island Transport

2015-08-15 18:04 204 查看

Island Transport

Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 6354 Accepted Submission(s): 1995


[align=left]Problem Description[/align]
  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.

[align=left]Input[/align]
  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.

[align=left]Output[/align]
  For each test case, output an integer in one line, the transport capacity.

[align=left]Sample Input[/align]

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

[align=left]Sample Output[/align]

9
6

[align=left]Source[/align]
2012 ACM/ICPC Asia Regional Tianjin Online
题意:从最西边点到最东边点的最大流,双向图

wa哭了。

#pragma comment(linker, "/STACK:102400000,102400000")  // 固定格式,中间不能有空格,防止RE,爆栈,只能用C++提交
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>

using namespace std;

#define N 1350000
#define INF 0xfffffff

struct node
{
int v, flow, next;
}edge
;

int cnt, head
, layer
;

void addedge(int u, int v, int flow)
{
edge[cnt].v = v;
edge[cnt].flow = flow;
edge[cnt].next = head[u];
head[u] = cnt;
cnt++;

swap(v, u);

edge[cnt].v = v;
edge[cnt].flow = flow;
edge[cnt].next = head[u];
head[u] = cnt;
cnt++;
}
// bfs分层,构图
int bfs(int u, int e)   // 函数写在主函数后也TLE。。写前边~,
{
memset(layer, 0, sizeof(layer));
queue<int> Q;
Q.push(u);
layer[u] = 1;

while(Q.size())
{
u = Q.front();
Q.pop();

if(u == e)
return true;
for(int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].v;

if(edge[i].flow && !layer[v])
{
layer[v] = layer[u]+1;
Q.push(v);
}
}
}
return false;
}

int dfs(int u, int Maxflow, int e)
{
int flow;

if(u == e)
return Maxflow;

int uflow = 0;

for(int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].v;
if(layer[v] == layer[u]+1 && edge[i].flow)
{
flow = min(Maxflow-uflow, edge[i].flow);  // 路径总流量减去从这分叉开的其他路的流量就是可能流过这个点的流量,取能流的最小值
flow = dfs(v, flow, e);

edge[i].flow -= flow;
edge[i^1].flow += flow;

uflow += flow;   //有可能找到不止一条路

if(uflow == Maxflow)
break;
}
}
if(uflow == 0)
layer[u] = 0;
return uflow;
}

int dinic(int s, int e)  // dinic
{
int ans = 0;

while(bfs(s, e))
ans += dfs(s, INF, e);
return ans;
}

int main()
{
int c, n, m, x, y, s, e, u, v, flow;

scanf("%d", &c);

while(c--)
{
cnt = 0;
memset(head, -1, sizeof(head));

scanf("%d%d", &n, &m);
int west = INF, east = -INF;

for(int i = 1; i <= n; i++)
{
scanf("%d%d", &x, &y);
if(x < west)
west = x, s = i;
if(x > east)
east = x, e = i;   //  找最西边还有最东边的点
}
while(m--)
{
scanf("%d%d%d", &u, &v, &flow);
addedge(u, v, flow);   // 只能写一个addedge不然tle
//addedge(v, u, flow);
}
printf("%d\n", dinic(s, e));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: