您的位置:首页 > 理论基础 > 计算机网络

Island Transport HDU - 4280 (无向图形网络流)

2017-11-05 15:59 351 查看
  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

这题是纯的网络流,唯一需要注意的就是这是个无向图,只要在加反向边的时候直接流量设的和正向边一样即可,然后直接上模板…

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn=100005;
const int maxx=200005;
int edge;
int to[maxx],flow[maxx],nex[maxx];
int head[maxn];

void addEdge(int v,int u,int cap)
{
to[edge]=u,flow[edge]=cap,nex[edge]=head[v],head[v]=edge++;
to[edge]=v,flow[edge]=cap,nex[edge]=head[u],head[u]=edge++;
}
int vis[maxn];
int pre[maxn];
bool bfs(int s,int e)
{
queue<int> que;
pre[s]=-1;
memset(vis,-1,sizeof(vis));
que.push(s);
vis[s]=0;
while(!que.empty())
{
int u=que.front();
que.pop();
for(int i=head[u];~i;i=nex[i])
{
int v=to[i];
if(vis[v]==-1&&flow[i])
{
vis[v]=vis[u]+1;
if(v==e)
return true;
que.push(v);
}

}
}
return false;
}
int dfs(int s,int t,int f)
{
if(s==t||!f)
return f;
int r=0;
for(int i=head[s];~i;i=nex[i])
{
int v=to[i];
if(vis[v]==vis[s]+1&&flow[i])
{
int d=dfs(v,t,min(f,flow[i]));
if(d>0)
{
flow[i]-=d;
flow[i^1]+=d;
r+=d;
f-=d;
if(!f)
break;
}
}
}
if(!r)
vis[s]=INF;
return r;
}

int maxFlow(int s ,int e)//然后直接调用这个即可
{
int ans=0;
while(bfs(s,e))
ans+=dfs(s,e,INF);
return ans;
}

void init()//记得每次使用前初始化
{
memset(head,-1,sizeof(head));
edge=0;
}

int main()
{
int t;
scanf("%d",&t);
int x,y,w;
int n,m;
int s,e;
int maxI,minI;
while(t--)
{
init();
scanf("%d%d",&n,&m);
scanf("%d%d",&x,&y);
s=e=1;
maxI=minI=x;
for(int i=2;i<=n;i++)
{
scanf("%d%d",&x,&y);
if(maxI<x)
e=i,maxI=x;
if(minI>x)
s=i,minI=x;
}
while(m--)
{
scanf("%d%d%d",&x,&y,&w);
addEdge(x,y,w);
}
int ans=maxFlow(s,e);
printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: