zoukankan      html  css  js  c++  java
  • Flow Problem(最大流模板)

    Flow Problem

    Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
    Total Submission(s): 17963    Accepted Submission(s): 8464


    Problem Description
    Network flow is a well-known difficult problem for ACMers. Given a graph, your task is to find out the maximum flow for the weighted directed graph.
     
    Input
    The first line of input contains an integer T, denoting the number of test cases.
    For each test case, the first line contains two integers N and M, denoting the number of vertexes and edges in the graph. (2 <= N <= 15, 0 <= M <= 1000)
    Next M lines, each line contains three integers X, Y and C, there is an edge from X to Y and the capacity of it is C. (1 <= X, Y <= N, 1 <= C <= 1000)
     
    Output
    For each test cases, you should output the maximum flow from source 1 to sink N.
     
    Sample Input
    2
    3 2
    1 2 1
    2 3 1
    3 3
    1 2 1
    2 3 1
    1 3 1
     
    Sample Output
    Case 1: 1
    Case 2: 2
     
    Author
    HyperHexagon
     
    Source
     
     
    题意:n 个点 m 条有向边,给出 m 条边的 u,v ,cap 问求最大流,算是模板题
    练习了Dinic算法
      1 # include <bits/stdc++.h>
      2 using namespace std;
      3 # define eps 1e-8
      4 # define INF 0x3f3f3f3f
      5 # define pi  acos(-1.0)
      6 # define MXN  105
      7 # define MXM  1005
      8 
      9 struct Edge{
     10     int from, to, flow, cap;
     11 }edges[MXM*2];              //有向边数
     12 
     13 struct Dinic{
     14     int n, m, s ,t ,idx;    //点,边,源点,汇点,边数
     15     vector<int> G[MXN];     //记录边
     16     int vis[MXN];           //BFS用
     17     int dis[MXN];           //层次
     18     int cur[MXN];           //考虑到哪条弧
     19 
     20     void Init(){
     21         idx=0;
     22         for (int i=1;i<=n;i++) G[i].clear();    //有附加点时要注意
     23     }
     24     
     25     void Addedge(int u,int v,int c){
     26         edges[idx++] = (Edge){u,v,0,c};
     27         edges[idx++] = (Edge){v,u,0,0};
     28         G[u].push_back(idx-2);
     29         G[v].push_back(idx-1);
     30     }
     31 
     32     int BFS()
     33     {
     34         memset(vis,0,sizeof(vis));
     35         queue<int> Q;
     36         Q.push(s);
     37         dis[s] = 0, vis[s] = 1;
     38         while(!Q.empty())
     39         {
     40             int u = Q.front(); Q.pop();
     41             for (int i=0;i<(int)G[u].size();i++)
     42             {
     43                 Edge &e = edges[G[u][i]];
     44                 if (!vis[e.to]&&e.cap>e.flow)
     45                 {
     46                     dis[e.to]=dis[u]+1;
     47                     vis[e.to]=1;
     48                     Q.push(e.to);
     49                 }
     50             }
     51         }
     52         return vis[t];
     53     }
     54 
     55     int DFS(int x,int a)
     56     {
     57         if (x==t||a==0) return a;
     58         int flow = 0, temp;
     59         for (int &i=cur[x];i<(int)G[x].size();i++)
     60         {
     61             Edge &e = edges[G[x][i]];
     62             if (dis[e.to] == dis[x]+1 && (temp=DFS(e.to, min(a, e.cap-e.flow)))>0)
     63             {
     64                 e.flow+=temp;
     65                 edges[G[x][i]^1].flow-=temp;
     66                 flow += temp;
     67                 a-=temp;
     68                 if (a==0) break;
     69             }
     70         }
     71         return flow;
     72     }
     73 
     74     int MaxFlow(int s,int t)
     75     {
     76         this->s = s, this->t = t;
     77         int flow=0;
     78         while(BFS()){
     79             memset(cur,0,sizeof(cur));
     80             flow+=DFS(s,INF);
     81         }
     82         return flow;
     83     }
     84 }F;
     85 
     86 int main()
     87 {
     88     int T;
     89     scanf("%d",&T);
     90     for (int cas=1;cas<=T;cas++)
     91     {
     92         scanf("%d%d",&F.n,&F.m);
     93         F.Init();
     94         for (int i=1;i<=F.m;i++)
     95         {
     96             int u,v,c;
     97             scanf("%d%d%d",&u,&v,&c);
     98             F.Addedge(u,v,c);
     99         }
    100         printf("Case %d: %d
    ",cas,F.MaxFlow(1,F.n));
    101     }
    102     return 0;
    103 }
    View Code

     

  • 相关阅读:
    CSS实现雨滴动画效果
    大型网站架构系列:电商网站架构案例
    CSS 不定宽高的垂直水平居中方式总汇
    js中尺寸类样式
    Tiling
    排序二叉树
    算术表达式的转换
    Area
    catch that cow
    R中双表操作学习[转载]
  • 原文地址:https://www.cnblogs.com/haoabcd2010/p/7800579.html
Copyright © 2011-2022 走看看