HDU 4280 Island Transport (无向图最大流)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/GYH0730/article/details/82584925

Island Transport

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


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

很裸的无向图最大流问题,在有向图最大流的基础上建立反向边的时候,流量和正向边一样,不过这道题卡时间,开始用紫书上的板子就T了,估计是用vector建图的原因,换了个邻接表模板的过了,但也跑了9600ms

#include<stdio.h>
#include<string.h>
#include<queue>
#include<math.h>
#include<algorithm>
#define MAX 100010
#define INF 0x7ffff
#define MAXM 200100
using namespace std;
struct node
{
    int from,to,cap,flow,next;
}edge[MAXM];
int dis[MAX],vis[MAX];
int cur[MAX];
int ans,head[MAX];
void init()
{
    ans=0;
    memset(head,-1,sizeof(head));
}
void add(int u,int v,int w)
{
    edge[ans]={u,v,w,0,head[u]};
    head[u]=ans++;
    edge[ans]={v,u,w,0,head[v]};//这是无向图  有向图edge[ans]={v,u,0,0,head[v]};
    head[v]=ans++;
}
int start,hui,sum;
int bfs(int beg,int end)
{
    queue<int>q;
    memset(vis,0,sizeof(vis));
    memset(dis,-1,sizeof(dis));
    while(!q.empty()) q.pop();
    vis[beg]=1;
    dis[beg]=0;
    q.push(beg);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            node E=edge[i];
            if(!vis[E.to]&&E.cap>E.flow)
            {
                dis[E.to]=dis[u]+1;
                vis[E.to]=1;
                if(E.to==end) return 1;
                q.push(E.to);
            }
        }
    }

    return 0;
}
int dfs(int x,int a,int end)
{
    if(x==end||a==0)
    return a;
    int flow=0,f;
    for(int& i=cur[x];i!=-1;i=edge[i].next)
    {
        node& E=edge[i];
        if(dis[E.to]==dis[x]+1&&(f=dfs(E.to,min(a,E.cap-E.flow),end))>0)
        {
            E.flow+=f;
            edge[i^1].flow-=f;
            flow+=f;
            a-=f;
            if(a==0) break;
        }
    }
    return flow;
}
int maxflow(int beg,int end)
{
    int flow=0;
    while(bfs(beg,end))
    {
        memcpy(cur,head,sizeof(head));
        flow+=dfs(beg,INF,end);
    }
    return flow;
}
inline int read() {
    int x = 0,f = 1;
    char ch = getchar();
    while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch = getchar();}
    while(ch>='0'&&ch<='9'){x = x*10+ch-48;ch = getchar();}
    return x*f;
}
int main(void)
{
    int t,S,T,Min,Max;
    int u,v,c;
    t = read();
    while(t--) {
        int n,m;
        n = read(),m = read();
        init();
        Min = 100005,Max = -100005;
        for(int i = 1; i <= n; i++) {
            u = read();
            v = read();
            if(u < Min) Min = u,S = i;
            if(u > Max) Max = u,T = i;
        }
        for(int i = 1; i <= m; i++) {
            u = read();v = read();c = read();
            add(u,v,c);
        }
        printf("%d\n",maxflow(S,T));
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/GYH0730/article/details/82584925
今日推荐