zoukankan      html  css  js  c++  java
  • HDU4280 Island Transport —— 最大流 ISAP算法

    题目链接:https://vjudge.net/problem/HDU-4280

    Island Transport

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


    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
     
    Recommend
    liuyiding

    题解:

    最大流的裸题,不过对时间效率要求较高。所以就用了ISAP。

    代码如下:

      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <algorithm>
      5 #include <vector>
      6 #include <cmath>
      7 #include <queue>
      8 #include <stack>
      9 #include <map>
     10 #include <string>
     11 #include <set>
     12 using namespace std;
     13 typedef long long LL;
     14 const int INF = 2e9;
     15 const LL LNF = 9e18;
     16 const int mod = 1e9+7;
     17 const int MAXN = 1e5+10;
     18 
     19 struct Edge
     20 {
     21     int to, next, cap, flow;
     22 }edge[MAXN<<2];
     23 int tot, head[MAXN];
     24 
     25 int gap[MAXN], dep[MAXN], cur[MAXN];
     26 
     27 void init()
     28 {
     29     tot = 0;
     30     memset(head, -1, sizeof(head));
     31 }
     32 
     33 void add(int u, int v, int w)
     34 {
     35     edge[tot].to = v; edge[tot].cap = w; edge[tot].flow = 0;
     36     edge[tot].next = head[u]; head[u] = tot++;
     37     edge[tot].to = u; edge[tot].cap = 0; edge[tot].flow = 0;
     38     edge[tot].next = head[v]; head[v] = tot++;
     39 }
     40 
     41 int Q[MAXN];
     42 void BFS(int start, int end)
     43 {
     44     memset(dep,-1,sizeof(dep));
     45     memset(gap,0,sizeof(gap));
     46     dep[end] = 0;
     47     gap[0] = 1;
     48     int front = 0, rear = 0;
     49     Q[rear++] = end;
     50     while(front!=rear)
     51     {
     52         int u = Q[front++];
     53         for(int i = head[u]; i!=-1; i=edge[i].next)
     54         {
     55             int v = edge[i].to;
     56             if(dep[v]!=-1) continue;
     57             Q[rear++] = v;
     58             dep[v] = dep[u]+1;
     59             gap[dep[v]]++;
     60         }
     61     }
     62 }
     63 
     64 int S[MAXN];
     65 int sap(int start, int end, int N)
     66 {
     67     BFS(start, end);
     68     memcpy(cur,head,sizeof(head));
     69     int top = 0;
     70     int u = start;
     71     int ans = 0;
     72     while(dep[start]<N)
     73     {
     74         if(u==end)
     75         {
     76             int Min = INF;
     77             int inser;
     78             for(int i = 0; i<top; i++)
     79                 if(Min>edge[S[i]].cap-edge[S[i]].flow)
     80                 {
     81                     Min = edge[S[i]].cap-edge[S[i]].flow;
     82                     inser = i;
     83                 }
     84             for(int i = 0; i<top; i++)
     85             {
     86                 edge[S[i]].flow += Min;
     87                 edge[S[i]^1].flow -= Min;
     88             }
     89             ans += Min;
     90             top = inser;
     91             u = edge[S[top]^1].to;
     92             continue;
     93         }
     94 
     95         bool flag = false;
     96         int v;
     97         for(int i = cur[u]; i!=-1; i = edge[i].next)
     98         {
     99             v = edge[i].to;
    100             if(edge[i].cap-edge[i].flow && dep[v]+1==dep[u])
    101             {
    102                 flag = true;
    103                 cur[u] = i;
    104                 break;
    105             }
    106         }
    107 
    108         if(flag)
    109         {
    110             S[top++] = cur[u];
    111             u = v;
    112             continue;
    113         }
    114 
    115         int Min = N;
    116         for(int i = head[u]; i!=-1; i = edge[i].next)
    117             if(edge[i].cap-edge[i].flow && dep[edge[i].to]<Min)
    118             {
    119                 Min = dep[edge[i].to];
    120                 cur[u] = i;
    121             }
    122         if((--gap[dep[u]])==0) break;
    123         gap[dep[u]=Min+1]++;
    124         if(u!=start) u = edge[S[--top]^1].to;
    125     }
    126     return ans;
    127 }
    128 
    129 int main()
    130 {
    131     int T, n, m;
    132     scanf("%d", &T);
    133     while(T--)
    134     {
    135         scanf("%d%d", &n,&m);
    136         int start, end, le = INF, ri = -INF;
    137         for(int i = 1; i<=n; i++)
    138         {
    139             int x, y;
    140             scanf("%d%d", &x,&y);
    141             if(x<le) { le = x; start = i; }
    142             if(x>ri) { ri = x; end = i; }
    143         }
    144 
    145         init();
    146         for(int i = 1; i<=m; i++)
    147         {
    148             int u, v, c;
    149             scanf("%d%d%d", &u,&v,&c);
    150             add(u, v, c);
    151             add(v, u, c);
    152         }
    153 
    154         cout<< sap(start, end, n) <<endl;
    155     }
    156 }
    View Code
  • 相关阅读:
    提升Android编译速度
    NYOJ 158 省赛来了
    浅谈 ZipArchive 类
    块状元素的text-align对齐属性
    BestCoder Round #2 1001 TIANKENG’s restaurant
    Saltstack运行cmd.run重新启动tomcat后出现日志乱码(15)
    【HRS项目】Axure兴许问题解决---与SVN结合
    软件质量之道:PCLint之中的一个
    字典树 一种高速插入查询数据结构
    【JS】JavaScript引擎的内部执行机制
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/8076130.html
Copyright © 2011-2022 走看看