您的位置:首页 > 其它

HDOJ 题目4280 Island Transport(最大流,sap)

2015-02-13 21:47 232 查看

Island Transport

Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 5038 Accepted Submission(s): 1636



[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

[align=left]Recommend[/align]
liuyiding | We have carefully selected several similar problems for you: 4277 4267 4269 4270 4271
ac代码
#include<stdio.h>
#include<string.h>
#include<queue>
#include<string>
#include<iostream>
#define INF 0xfffffff
#define max(a,b) (a>b?a:b)
#define min(a,b) (b>a?a:b)
using namespace std;
int n,m,sp,tp,cnt;
int head[100005],dis[100005],gap[100005],cur[100005],stack[100005];
struct s
{
int u,v,w,next;
}edge[100005*4];
void add(int u,int v,int w)
{
edge[cnt].u=u;
edge[cnt].v=v;
edge[cnt].w=w;
edge[cnt].next=head[u];
head[u]=cnt++;
edge[cnt].u=v;
edge[cnt].v=u;
edge[cnt].w=0;
edge[cnt].next=head[v];
head[v]=cnt++;
}
void bfs()
{
memset(dis,-1,sizeof(dis));
memset(gap,0,sizeof(gap));
gap[0]=1;
queue<int>q;
dis[tp]=0;
gap[0]=1;
q.push(tp);
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(edge[i].w!=0||dis[v]!=-1)
continue;
q.push(v);
dis[v]=dis[u]+1;
gap[dis[v]]++;
}
}
}
int sap()
{
int ans=0;
bfs();
int top=0;
memcpy(cur,head,sizeof(head));
int u=sp,i;
while(dis[sp]<n)
{
if(u==tp)
{
int temp=INF,k=n;
for(i=0;i!=top;i++)
{
if(temp>edge[stack[i]].w)
{
temp=edge[stack[i]].w;
k=i;
}
}
for(i=0;i!=top;i++)
{
edge[stack[i]].w-=temp;
edge[stack[i]^1].w+=temp;
}
ans+=temp;
top=k;
u=edge[stack[top]].u;
}
if(u!=tp&&gap[dis[u]-1]==0)
{
break;
}
for(i=cur[u];i!=-1;i=edge[i].next)
{
if(edge[i].w!=0&&dis[u]==dis[edge[i].v]+1)
break;
}
if(i!=-1)
{
cur[u]=i;
stack[top++]=i;
u=edge[i].v;
}
else
{
int minn=n;
for(i=head[u];i!=-1;i=edge[i].next)
{
if(edge[i].w==0)
continue;
if(minn>dis[edge[i].v])
{
minn=dis[edge[i].v];
cur[u]=i;
}
}
gap[dis[u]]--;
dis[u]=minn+1;
gap[dis[u]]++;
if(u!=sp)
u=edge[stack[--top]].u;
}
}
return ans;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int max_x=-INF,min_x=INF,i;
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
if(x<=min_x)
{
min_x=x;
sp=i;
}
if(x>=max_x)
{
max_x=x;
tp=i;
}
}
memset(head,-1,sizeof(head));
cnt=0;
for(i=0;i<m;i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
add(b,a,c);
}
printf("%d\n",sap());
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: