• 最大流(Isap模板)


    http://acm.hdu.edu.cn/showproblem.php?pid=1532

      1 #include <iostream>
      2 #include <cstdio>
      3 #include <climits>
      4 #include <cstring>
      5 #include <algorithm>
      6 using namespace std;
      7 typedef  struct {int v,next,val;} edge;
      8 const int MAXN=20010;
      9 const int MAXM=500010;
     10 edge e[MAXM];
     11 int p[MAXN],eid;
     12 inline void init(){memset(p,-1,sizeof(p));eid=0;}
     13 //有向
     14 inline void insert1(int from,int to,int val)
     15 {
     16     e[eid].v=to;e[eid].val=val;
     17     e[eid].next=p[from];
     18     p[from]=eid++;
     19     swap(from,to);
     20     e[eid].v=to;e[eid].val=0;
     21     e[eid].next=p[from];
     22     p[from]=eid++;
     23 }
     24 //无向
     25 inline void insert2(int from,int to,int val)
     26 {
     27     e[eid].v=to;e[eid].val=val;
     28     e[eid].next=p[from];
     29     p[from]=eid++;
     30     swap(from,to);
     31     e[eid].v=to;e[eid].val=val;
     32     e[eid].next=p[from];
     33     p[from]=eid++;
     34 }
     35 int n,m;//n为点数 m为边数
     36 int h[MAXN];
     37 int gap[MAXN];
     38 int source,sink;
     39 inline int dfs(int pos,int cost)
     40 {
     41     if (pos==sink) return cost;
     42 
     43     int j,minh=n-1,lv=cost,d;
     44     for (j=p[pos];j!=-1;j=e[j].next)
     45     {
     46         int v=e[j].v,val=e[j].val;
     47         if(val>0)
     48         {
     49             if (h[v]+1==h[pos])
     50             {
     51                 if (lv<e[j].val) d=lv;
     52                 else d=e[j].val;
     53 
     54                 d=dfs(v,d);
     55                 e[j].val-=d;
     56                 e[j^1].val+=d;
     57                 lv-=d;
     58                 if (h[source]>=n) return cost-lv;
     59                 if (lv==0) break;
     60             }
     61             if (h[v]<minh)    minh=h[v];
     62         }
     63     }
     64     if (lv==cost)
     65     {
     66         --gap[h[pos]];
     67         if (gap[h[pos]]==0) h[source]=n;
     68         h[pos]=minh+1;
     69         ++gap[h[pos]];
     70     }
     71     return cost-lv;
     72 }
     73 
     74 int isap(int st,int ed)
     75 {
     76 
     77     source=st;sink=ed;
     78     int ret=0;
     79     memset(gap,0,sizeof(gap));
     80     memset(h,0,sizeof(h));
     81     gap[st]=n;
     82     while (h[st]<n)
     83     {
     84         ret+=dfs(st,INT_MAX);
     85     }
     86     return ret;
     87 }
     88 int main()
     89 {
     90     while(cin>>m>>n)
     91     {
     92         init();
     93         for(int i=0;i<m;i++)
     94         {
     95             int u,v,c;
     96             scanf("%d%d%d",&u,&v,&c);
     97             insert1(u,v,c);
     98         }
     99         printf("%d\n",isap(1,n));
    100     }
    101     return 0;
    102 }
  • 相关阅读:
    Windows脚本 Bat批处理命令使用教程
    [Jobdu] 题目1420:Jobdu MM分水果
    排序算法的时间复杂度和空间复杂度
    (转) CS的顶级会议和期刊
    句柄是什么?1
    批处理for命令详解(转)
    cmd批处理常用符号详解
    【转帖】请问两个大于号>>代表什么意思?
    今天玩angry birds
    C/C++数组名与指针区别深入探索
  • 原文地址:https://www.cnblogs.com/qijinbiao/p/2601412.html
Copyright © 2020-2023  润新知