zoukankan      html  css  js  c++  java
  • HDU 4280 ISAP+BFS 最大流 模板

    Island Transport

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


    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
     

     最大流裸题 *可用模板

      1 #include<cstdio>
      2 #include<cstring>
      3 #include<iostream>
      4 #include<algorithm>
      5 #include<map>
      6 using namespace std;
      7 #define ll long long
      8 const int M = 100010;
      9 const int INF = 0x3f3f3f3f;
     10 struct Edge{
     11  int to,next,cap,flow;
     12 }edge[4*M];
     13 int tol;
     14 int head[M];
     15 int gap[M],dep[M],cur[M];
     16 void init()
     17 {
     18     tol=0;
     19     memset(head,-1,sizeof(head));
     20 }
     21 void addedge(int u,int v,int w){
     22     int rw=0;
     23     edge[tol].to=v;edge[tol].cap=w;edge[tol].flow=0;
     24     edge[tol].next=head[u];head[u]=tol++;
     25     edge[tol].to=u;edge[tol].cap=rw;edge[tol].flow=0;
     26     edge[tol].next=head[v];head[v]=tol++;
     27 }
     28 int Q[M];
     29 void bfs(int start,int end){
     30     memset(dep,-1,sizeof(dep));
     31     memset(gap,0,sizeof(gap));
     32     gap[0]=1;
     33     int front=0,rear=0;
     34     dep[end]=0;
     35     Q[rear++]=end;
     36     while(front !=rear){
     37         int u=Q[front++];
     38         for(int i=head[u];i!=-1;i=edge[i].next){
     39             int v=edge[i].to;
     40             if(dep[v]!=-1) continue;
     41             Q[rear++]=v;
     42             dep[v]=dep[u]+1;
     43             gap[dep[v]]++;
     44         }
     45     }
     46 }
     47 int S[M];
     48 int sap(int start,int end,int N){
     49     bfs(start,end);
     50     memcpy(cur,head,sizeof(head));
     51     int top=0;
     52     int u=start;
     53     int ans=0;
     54     while(dep[start]<N){
     55         if(u==end){
     56            int Min=INF;
     57            int inser;
     58            for(int i=0;i<top;i++){
     59             if(Min>edge[S[i]].cap-edge[S[i]].flow){
     60                 Min=edge[S[i]].cap-edge[S[i]].flow;
     61                 inser=i;
     62             }
     63            }
     64            for(int i=0;i<top;i++){
     65             edge[S[i]].flow+=Min;
     66             edge[S[i]^1].flow-=Min;
     67            }
     68            ans+=Min;
     69            top=inser;
     70            u=edge[S[top]^1].to;
     71            continue;
     72         }
     73         bool flag=false;
     74         int v;
     75         for(int i=cur[u];i!=-1;i=edge[i].next){
     76             v=edge[i].to;
     77             if(edge[i].cap-edge[i].flow&&dep[v]+1==dep[u]){
     78                 flag=true;
     79                 cur[u]=i;
     80                 break;
     81             }
     82         }
     83         if(flag){
     84             S[top++] = cur[u];
     85             u=v;
     86             continue;
     87         }
     88         int Min=N;
     89         for(int i=head[u];i!=-1;i=edge[i].next){
     90             if(edge[i].cap-edge[i].flow&&dep[edge[i].to]<Min){
     91                 Min=dep[edge[i].to];
     92                 cur[u]=i;
     93             }
     94         }
     95         gap[dep[u]]--;
     96         if(!gap[dep[u]]) return ans;
     97         dep[u]=Min+1;
     98         gap[dep[u]]++;
     99         if(u!=start) u=edge[S[--top]^1].to;
    100     }
    101     return ans;
    102 }
    103 int t;
    104 int n,m;
    105 int main()
    106 {
    107     scanf("%d",&t);
    108     while(t--){
    109        init();
    110         scanf("%d %d",&n,&m);
    111         int minx=10000000;
    112         int maxx=-10000000;
    113         int start,tail;
    114         for(int i=1;i<=n;i++){
    115             int x,y;
    116             scanf("%d %d",&x,&y);
    117             if(x<minx){minx=x;start=i;}
    118             else if(x>maxx){maxx=x;tail=i;}
    119         }
    120         for(int i=1;i<=m;i++){
    121             int  qq,w,e;
    122             scanf("%d %d %d",&qq,&w,&e);
    123             addedge(qq,w,e);
    124             addedge(w,qq,e);
    125         }
    126         printf("%d
    ",sap(start,tail,n));
    127     }
    128     return 0;
    129 }
  • 相关阅读:
    PowerShell_零基础自学课程_7_Powershell中重定向机制、目录和文件管理
    volcanol_C语言学习趣事汇总贴
    linux_shell_2_shell 中的变量特性
    PowerShell_零基础自学课程_5_自定义PowerShell环境及Powershell中的基本概念
    volcanol_Linux_问题汇总系列_2_如何在linux和windows主机之间共享文件
    C语言学习趣事_你不知道的C语言应用
    volcanol_Windows_Powershell_汇总贴
    PowerShell_零基础自学课程_4_PowerShell的别名功能、错误管理功能和系统资源区域导航
    Sqlite 管理工具 SQLiteDeveloper及破解
    android建立Menu详解
  • 原文地址:https://www.cnblogs.com/hsd-/p/7620491.html
Copyright © 2011-2022 走看看