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): 11475    Accepted Submission(s): 5437

    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
     

    题解:最大流入门题目,就是个模版,就是不要知道为啥,要是+=。。。

    代码:

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<algorithm>
     6 #include<queue>
     7 #define mem(x,y) memset(x,y,sizeof(x))
     8 using namespace std;
     9 const int INF=0x3f3f3f3f;
    10 const int MAXN=20;
    11 int N;
    12 queue<int>dl;
    13 int vis[MAXN],pre[MAXN];
    14 int map[MAXN][MAXN];
    15 int s,e;
    16 bool bfs(){
    17     while(!dl.empty())dl.pop();
    18     mem(vis,0);
    19     mem(pre,0);
    20     vis[s]=1;
    21     dl.push(s);
    22     int a;
    23     while(!dl.empty()){
    24         a=dl.front();
    25         dl.pop();
    26         if(a==e)return true;
    27         for(int i=2;i<=N;i++){
    28             if(!vis[i]&&map[a][i])dl.push(i),vis[i]=1,pre[i]=a;
    29         }
    30     }
    31     return false;
    32 }
    33 int maxflow(){
    34     int ans=0;
    35     while(1){
    36     if(!bfs())return ans;
    37     int a=e,temp=INF;
    38     while(a!=s){
    39         temp=min(temp,map[pre[a]][a]);
    40         a=pre[a];
    41     }a=e;
    42     while(a!=s){
    43         map[pre[a]][a]-=temp;
    44         map[a][pre[a]]+=temp;
    45         a=pre[a];
    46     }
    47     ans+=temp;
    48     }
    49 }
    50 int main(){
    51     int T,M,flot=0;
    52     scanf("%d",&T);
    53     while(T--){
    54         mem(map,0);
    55         scanf("%d%d",&N,&M);
    56         int a,b,c;
    57         while(M--){
    58             scanf("%d%d%d",&a,&b,&c);
    59             map[a][b]+=c;//加等是因为在两个点可能存在多条管道,需要合并容量。。。         }
    61         s=1;e=N;
    62         printf("Case %d: %d
    ",++flot,maxflow());
    63     }
    64     return 0;
    65 }

     dinic算法:

      1 #include<cstdio>
      2 #include<iostream>
      3 #include<cstring>
      4 #include<cmath>
      5 #include<queue>
      6 #include<algorithm>
      7 #define mem(x,y) memset(x,y,sizeof(x))
      8 using namespace std;
      9 const int INF=0x3f3f3f3f;
     10 const int MAXN=20;
     11 const int MAXM=2020;
     12 int edgnum;
     13 int vis[MAXN],dis[MAXN];
     14 int head[MAXM];
     15 queue<int>dl;
     16 int s,e,tflow;
     17 struct Node{
     18     int from,to,next,flow,cup;
     19 }dt[MAXM];
     20 void initial(){
     21     mem(head,-1);
     22     edgnum=0;
     23 }
     24 void add(int u,int v,int w){
     25     Node E={u,v,head[u],0,w};
     26     dt[edgnum]=E;
     27     head[u]=edgnum++;
     28     E={v,u,head[v],0,0};
     29     dt[edgnum]=E;
     30     head[v]=edgnum++;
     31 } 
     32 bool bfs(){
     33     mem(vis,0);mem(dis,-1);
     34     dis[s]=0;
     35     while(!dl.empty())dl.pop();
     36     dl.push(s);
     37     vis[s]=1;
     38     int a;
     39     while(!dl.empty()){
     40          a=dl.front();
     41          dl.pop();
     42          for(int i=head[a];i!=-1;i=dt[i].next){
     43              Node b=dt[i];
     44              if(!vis[b.to]&&b.cup>b.flow){
     45                  dis[b.to]=dis[a]+1;
     46                  vis[b.to]=1;
     47                  if(b.to==e)return true;
     48                  dl.push(b.to);
     49              }
     50          }
     51     }
     52     return false;
     53 }
     54 /************/
     55 int dfs(int x,int a)//把找到的这个路径上所有的边的当前流量都增加a(a是所找出路径的边中 残余流量的最小值)
     56 {
     57 if(x==e||a==0)
     58         return a;
     59     int flow=0,f;
     60     for(int i=head[x];i!=-1;i=dt[i].next)//从上次考虑的弧开始
     61     {
     62         Node &E=dt[i];
     63         if(dis[E.to]==dis[x]+1&&(f=dfs(E.to,min(a,E.cup-E.flow)))>0)//可继续增广
     64         {
     65             E.flow+=f;//正向边  
     66             dt[i^1].flow-=f;//反向边
     67             flow+=f;//总流量 加上 f 
     68             a-=f;//最小可增流量 减去f 
     69             if(a==0)
     70                 break;
     71         }
     72     }
     73     return flow;}
     74 /***************/
     75 /*void dfs(int x,int a){
     76     for(int i=head[x];i!=-1;i=dt[i].next){
     77         Node &b=dt[i];
     78         if(dis[b.to]==dis[x]+1){
     79             if(b.cup-b.flow>0&&b.to!=e){
     80                 tflow=min(tflow,b.cup-b.flow);
     81             dfs(b.to,min(a,b.cup-b.flow));
     82             b.flow+=tflow;
     83             dt[i^1].flow-=tflow;
     84             a-=tflow;
     85             if(!a)break;
     86             }
     87         }
     88     }
     89 }*/
     90 int maxflow(){
     91     int flow=0;
     92     while(bfs()){
     93         //tflow=INF;
     94         flow+=dfs(s,INF);
     95         //flow+=tflow;
     96     }
     97     return flow;
     98 }
     99 int main(){
    100     int T,N,M,flot=0;
    101     scanf("%d",&T);
    102     while(T--){
    103         initial();
    104         scanf("%d%d",&N,&M);
    105         int u,v,w;
    106         while(M--){
    107             scanf("%d%d%d",&u,&v,&w);
    108             add(u,v,w);
    109         }
    110         s=1;e=N;
    111         printf("Case %d: %d
    ",++flot,maxflow());
    112     }
    113     return 0;
    114 }
  • 相关阅读:
    Learning NFS/NIS 2nd 读书笔记-Chapter3 NIS Operation
    Linux Enterprise Cluster Notes Ch11 LVS Introduction Theory
    Linux Enterprise Cluster NOtes Ch7 A Sample HA config
    Linux Enterprise Cluster Notes Ch10 build a Linux cluster
    Linux Enterprise Cluster NOtes Ch8 Heartbeat配置和维护
    当被监控的应用发生问题时,heartbeat会failover么?
    Linux Enterprise Cluster NOtes Ch9 Stonith and IPFail
    Linux Enterprise Cluster NOtes Ch6 Heartbeat介绍和原理
    客户端不支持javascript怎么办
    js 返回对象|js返回多个值的方法|js如何返回多个值
  • 原文地址:https://www.cnblogs.com/handsomecui/p/4934111.html
Copyright © 2011-2022 走看看