zoukankan      html  css  js  c++  java
  • Flow Problem

    Flow Problem

    TimeLimit:5000MS  MemoryLimit:32768KB
    64-bit integer IO format:%I64d
     
    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.
    SampleInput
    2
    3 2
    1 2 1
    2 3 1
    3 3
    1 2 1
    2 3 1
    1 3 1
    SampleOutput
    Case 1: 1
    Case 2: 2
    题解:模板题,求最大流量
      1 #include<bits/stdc++.h>
      2 using namespace std;
      3 const int maxn=1000;
      4 const int inf=0x3f3f3f3f;
      5 struct node
      6 {
      7     int to,flow,next;
      8 } edge[maxn*4];
      9 int first[maxn],sign,vis[maxn],pre[maxn];
     10 void init()
     11 {
     12     memset(first,-1,sizeof(first));
     13     sign=0;
     14 }
     15 void add_edge(int u,int v,int flow)
     16 {
     17     edge[sign].to=v;
     18     edge[sign].flow=flow;
     19     edge[sign].next=first[u];
     20     first[u]=sign++;
     21     edge[sign].to=u;
     22     edge[sign].flow=0;///建一条反向边,流量为0;
     23     edge[sign].next=first[v];
     24     first[v]=sign++;
     25 }
     26 bool bfs(int s,int t)
     27 {
     28     memset(vis,0,sizeof(vis));
     29     memset(pre,-1,sizeof(pre));
     30        vis[s]=1;
     31        queue<int >que;
     32        que.push(s);
     33        while(!que.empty())
     34        {
     35            int now=que.front();
     36            que.pop();
     37            if(now==t)
     38            {
     39                return 1;
     40            }
     41            for(int i=first[now];~i;i=edge[i].next)
     42            {
     43                int to=edge[i].to,flow=edge[i].flow;
     44                if(!vis[to]&&flow>0)
     45                {
     46                    vis[to]=1;
     47                    que.push(to);
     48                    pre[to]=i;///记录路径  记录的是由上一条边到to这个点
     49                }
     50            }
     51        }
     52        return 0;
     53 
     54 }
     55 int edomon_krap(int s,int t)///起点 终点
     56 {
     57     int max_flow=0;
     58     while(bfs(s,t))
     59     {
     60         int min_flow=inf;
     61         int x=t;
     62         while(x!=s)
     63         {
     64            // printf("%d
    ",x);
     65             int index=pre[x];
     66             //printf("intdex  %d
    ",index);
     67             min_flow=min(min_flow,edge[index].flow);
     68             x=edge[index^1].to;
     69              //printf("x==%d
    ",x);
     70         }
     71          x=t;
     72         while(x!=s)
     73         {
     74             int index=pre[x];
     75             edge[index].flow-=min_flow;
     76             edge[index^1].flow+=min_flow;///反向边加上min_flow
     77             x=edge[index^1].to;
     78         }
     79         max_flow+=min_flow;
     80 
     81     }
     82    return max_flow;
     83 }
     84 int main()
     85 {
     86     int t,n,m;
     87     scanf("%d",&t);
     88     for(int i=1;i<=t;i++)
     89     {
     90         init();
     91         scanf("%d%d",&n,&m);
     92         for(int j=1;j<=m;j++)
     93         {
     94             int u,v,w;
     95             scanf("%d%d%d",&u,&v,&w);
     96             add_edge(u,v,w);
     97         }
     98         printf("Case %d: %d
    ",i,edomon_krap(1,n));
     99     }
    100 }
  • 相关阅读:
    【SAS BASE】PROC TABULATE(输出更加精美的tabulate报告)
    【SAS BASE】PROC FREQ
    【SAS BASE】PROC MEAS
    【SAS BASE】利用FILE语句和PUT语句输出简单报告
    表单验证类:jquery validate和nice Validator
    手机自适应
    微信公众号支付H5-java版代码
    php ueditor 后台配置项返回格式出错,上传功能将不能正常使用!
    html菜单三条横线
    thinkPHP更新数据库没有响应 setInc save setFiled
  • 原文地址:https://www.cnblogs.com/yuanlinghao/p/9432886.html
Copyright © 2011-2022 走看看