• poj 3469(网络流模版)


    题目链接:http://poj.org/problem?id=3469

    思路:终于把网络流的模版测试好了,在Dinic和Sap之间还是选择了Sap,事实证明Sap确实比Dinic效率高,在此贴出自己的网络流模版:

      1 #include<iostream>
      2 #include<cstdio>
      3 #include<cstring>
      4 #include<algorithm>
      5 #include<queue>
      6 using namespace std;
      7 #define MAXN 44444
      8 #define MAXM 999999
      9 #define inf 1<<30
     10 
     11 struct Edge {
     12     int v,cap,next;
     13 } edge[MAXM];
     14 
     15 int n,m,vs,vt,NE,NV;
     16 int head[MAXN];
     17 
     18 void Insert(int u,int v,int cap)
     19 {
     20     edge[NE].v=v;
     21     edge[NE].cap=cap;
     22     edge[NE].next=head[u];
     23     head[u]=NE++;
     24 
     25     edge[NE].v=u;
     26     edge[NE].cap=0;
     27     edge[NE].next=head[v];
     28     head[v]=NE++;
     29 }
     30 
     31 int level[MAXN];
     32 int gap[MAXN];
     33 
     34 void bfs(int vt)
     35 {
     36     memset(level,-1,sizeof(level));
     37     memset(gap,0,sizeof(gap));
     38     level[vt]=0;
     39     gap[level[vt]]++;
     40     queue<int>que;
     41     que.push(vt);
     42     while(!que.empty()) {
     43         int u=que.front();
     44         que.pop();
     45         for(int i=head[u]; i!=-1; i=edge[i].next) {
     46             int v=edge[i].v;
     47             if(level[v]!=-1)continue;
     48             level[v]=level[u]+1;
     49             gap[level[v]]++;
     50             que.push(v);
     51 
     52         }
     53     }
     54 }
     55 
     56 
     57 int pre[MAXN];
     58 int cur[MAXN];
     59 
     60 int SAP(int vs,int vt)
     61 {
     62     bfs(vt);
     63     memset(pre,-1,sizeof(pre));
     64     memcpy(cur,head,sizeof(head));
     65     int u=pre[vs]=vs,flow=0,aug=inf;
     66     gap[0]=NV;
     67     while(level[vs]<NV) {
     68         bool flag=false;
     69         for(int &i=cur[u]; i!=-1; i=edge[i].next) {
     70             int v=edge[i].v;
     71             if(edge[i].cap&&level[u]==level[v]+1) {
     72                 flag=true;
     73                 pre[v]=u;
     74                 u=v;
     75                 //  aug=(aug==-1?edge[i].cap:min(aug,edge[i].cap));
     76                 aug=min(aug,edge[i].cap);
     77                 if(v==vt) {
     78                     flow+=aug;
     79                     for(u=pre[v]; v!=vs; v=u,u=pre[u]) {
     80                         edge[cur[u]].cap-=aug;
     81                         edge[cur[u]^1].cap+=aug;
     82                     }
     83                     //     aug=-1;
     84                     aug=inf;
     85                 }
     86                 break;
     87             }
     88         }
     89         if(flag)continue;
     90         int minlevel=NV;
     91         for(int i=head[u]; i!=-1; i=edge[i].next) {
     92             int v=edge[i].v;
     93             if(edge[i].cap&&level[v]<minlevel) {
     94                 minlevel=level[v];
     95                 cur[u]=i;
     96             }
     97         }
     98         if(--gap[level[u]]==0)break;
     99         level[u]=minlevel+1;
    100         gap[level[u]]++;
    101         u=pre[u];
    102     }
    103     return flow;
    104 }
    105 
    106 
    107 int main()
    108 {
    109     int u,v,w,w1,w2;
    110     scanf("%d%d",&n,&m);
    111     vs=0;
    112     vt=n+1;
    113     NV=n+2;
    114     NE=0;
    115     memset(head,-1,sizeof(head));
    116     for(int i=1; i<=n; i++) {
    117         scanf("%d%d",&w1,&w2);
    118         Insert(vs,i,w1);
    119         Insert(i,vt,w2);
    120     }
    121     for(int i=1; i<=m; i++) {
    122         scanf("%d%d%d",&u,&v,&w);
    123         Insert(u,v,w);
    124         Insert(v,u,w);
    125     }
    126     printf("%d
    ",SAP(vs,vt));
    127     return 0;
    128 }
    View Code
  • 相关阅读:
    USACO 1.2 Broken Necklace
    USACO 1.2 Friday the Thirteenth
    USACO 1.1 Greedy Gift Givers
    USACO 1.1 Your Ride Is Here
    CSP考试策略
    CF444A DZY Loves Physics【结论】
    树状数组-复习笔记
    CF792E Colored Balls【思维】
    USACO4.4 Shuttle Puzzle【bfs+优化】
    拓扑排序-学习笔记
  • 原文地址:https://www.cnblogs.com/wally/p/3274438.html
Copyright © 2020-2023  润新知