• HDU 4280Island Transport(Dinc非STL 模板)


    题意: n岛m条路,然后是 n个岛的坐标,然后是m条双向路,包括 岛和 岛 之间 最大客流量,让求 最左边的岛 到右边的岛 最大客流量

    分析: 建图 以 左边的岛为原点,最右边的为终点求最大客流量。

    刘汝佳STL会超时,因为vector牵扯到 复制操作

    然后看大神的将stl全换了 http://www.cnblogs.com/xiao-xin/articles/4452306.html

      1 #include <iostream>
      2 #include <cstring>
      3 #include <vector>
      4 #include <algorithm>
      5 #include <cstdio>
      6 #include <queue>
      7 using namespace std;
      8 const int INF = 0x3f3f3f3f;
      9 const int Max = 100000 + 10;
     10 struct Edge
     11 {
     12     int to, flow, Next;
     13 };
     14 struct Dinic
     15 {
     16     int n, s, t, now;
     17     Edge edges[Max * 2];
     18     bool vis[Max];
     19     int d[Max], head[Max], q[Max];
     20     void init(int _s, int _t, int _n)
     21     {
     22         s = _s; t = _t; n = _n;
     23         memset(head, -1, sizeof(head));
     24         now = 0;
     25     }
     26     void addEdge(int from, int to, int flow)
     27     {
     28         edges[now].to = to; edges[now].flow = flow;
     29         edges[now].Next = head[from]; head[from] = now++;
     30 
     31         edges[now].to = from; edges[now].flow = flow;
     32         edges[now].Next = head[to]; head[to] = now++;
     33     }
     34     bool BFS()
     35     {
     36         memset(vis, 0, sizeof(vis));
     37         int Front =0, rear = 0;
     38         q[rear++] = s;
     39         d[s] = 0;
     40         vis[s] = 1;
     41         while (Front < rear)
     42         {
     43             int x = q[Front++];
     44             for (int i = head[x]; i != -1; i = edges[i].Next)
     45             {
     46                 if ( vis[edges[i].to] == 0 && edges[i].flow > 0)
     47                 {
     48                     vis[edges[i].to] = 1;
     49                     d[ edges[i].to ] = d[x] + 1;
     50                     q[ rear++ ] = edges[i].to;
     51                 }
     52             }
     53         }
     54         return vis[t];
     55     }
     56     int DFS(int x, int a)
     57     {
     58         if (x == t || a == 0)
     59             return a;
     60         int flow = 0, f;
     61         for (int i = head[x]; i != -1; i = edges[i].Next)
     62         {
     63             if (d[x] + 1 == d[edges[i].to] && edges[i].flow > 0)
     64             {
     65                 f = DFS(edges[i].to, min(a - flow, edges[i].flow));
     66                 edges[i].flow -= f;
     67                 edges[i ^ 1].flow += f;
     68                 flow += f;
     69                 if (flow == a)
     70                     return flow;
     71             }
     72         }
     73         if (flow == 0)
     74             d[x] = 0;
     75         return flow;
     76     }
     77     int MaxFlow()
     78     {
     79         int flow = 0;
     80         while ( BFS() )
     81         {
     82             flow += DFS(s, INF);
     83         }
     84         return flow;
     85     }
     86 
     87 }g;
     88 /*
     89 struct Dinic
     90 {
     91     int n, s, t;
     92     vector<Edge> edges;
     93     vector<int> G[Max];
     94     bool vis[Max];
     95     int d[Max];
     96     int cur[Max];
     97     void init(int _n, int _s, int _t)
     98     {
     99         n = _n;
    100         s = _s;
    101         t = _t;
    102         edges.clear();
    103         for (int i = 0; i <= n; i++)
    104             G[i].clear();
    105     }
    106     void addEdge(int from, int to, int cap)
    107     {
    108         Edge edge;
    109         edge.from = from;
    110         edge.to = to;
    111         edge.cap = cap;
    112         edge.flow = 0;
    113         edges.push_back(edge);
    114         edge.from = to;
    115         edge.to = from;
    116         edge.cap = cap;
    117         edge.flow = 0;
    118         edges.push_back(edge);
    119         int m = (int) edges.size();
    120         G[from].push_back(m - 2);
    121         G[to].push_back(m - 1);
    122     }
    123     bool BFS()
    124     {
    125         memset(vis, 0, sizeof(vis));
    126         queue<int> Q;
    127         Q.push(s);
    128         d[s] = 0;
    129         vis[s] = 1;
    130         while (!Q.empty())
    131         {
    132             int x = Q.front();
    133             Q.pop();
    134             for (int i = 0; i < (int) G[x].size(); i++)
    135             {
    136                 Edge & e = edges[ G[x][i] ];
    137                 if (!vis[e.to] && e.cap > e.flow)
    138                 {
    139                     vis[e.to] = 1;
    140                     d[e.to] = d[x] + 1;
    141                     Q.push(e.to);
    142                 }
    143             }
    144         }
    145         return vis[t];
    146     }
    147     int DFS(int x, int a)
    148     {
    149         if (x == t || a == 0)
    150             return a;
    151         int flow = 0, f;
    152         for (int& i = cur[x]; i < (int)G[x].size(); i++)
    153         {
    154             Edge & e = edges[ G[x][i] ];
    155             if (d[x] + 1 == d[e.to] && (f = DFS(e.to,  min(a, e.cap - e.flow))) > 0)
    156             {
    157                 e.flow += f;
    158                 edges[G[x][i] ^ 1].flow -= f;
    159                 flow += f;
    160                 a -= f;
    161                 if (a == 0)
    162                     break;
    163             }
    164         }
    165         return flow;
    166     }
    167     int MaxFlow()
    168     {
    169         int flow = 0;
    170         while( BFS() )
    171         {
    172             memset(cur, 0, sizeof(cur));
    173             flow += DFS(s, INF);
    174         }
    175         return flow;
    176     }
    177 
    178 
    179 }g;
    180 */
    181 int main()
    182 {
    183     int T, n, m, east, west;
    184     scanf("%d", &T);
    185     while (T--)
    186     {
    187         scanf("%d%d", &n, &m);
    188         int maxx = -INF, minx = INF;
    189         int x, y, w;
    190         for (int i = 1; i <= n; i++)
    191         {
    192             scanf("%d%d", &x, &y);
    193             if (x > maxx)
    194             {
    195                 maxx = x;
    196                 west = i;
    197             }
    198             if (x < minx)
    199             {
    200                 minx = x;
    201                 east = i;
    202             }
    203         }
    204         g.init(east, west, n);
    205         for (int i = 1; i <= m; i++)
    206         {
    207             scanf("%d%d%d", &x, &y, &w);
    208             g.addEdge(x, y, w);
    209         }
    210         printf("%d
    ", g.MaxFlow());
    211     }
    212     return 0;
    213 }
    View Code
  • 相关阅读:
    十分钟了解HTTPS协议
    浅谈程序员的学历
    浅谈前后模板引擎的利与弊
    简单理解预加载技术
    简单理解懒加载技术
    C#.NET里面抽象类和接口有什么区别
    Select count(*)、Count(1)、Count(0)的区别和执行效率比较
    c#中decimal ,double,float的区别
    C#使用log4net记录日志
    SQLServer无法打开用户默认数据库 登录失败错误4064的解决方法
  • 原文地址:https://www.cnblogs.com/zhaopAC/p/5422133.html
Copyright © 2020-2023  润新知