• ACM 第六天


    图论 网络流

    最大流 INF(初始值)

    路径上权值最小的边,决定流量大小。

     

    流量网络的三个特性:

    ①流量控制

    ②反对称性

    ③流量守恒

     

    残余网络:保留了c(e)容量<f(e)流量【可以继续流,因为还有f(e)-c(e)的流量】和 c(e)>0的反向边【可以回退】

     增广路定理:网络中达到最大流当且仅当残余网络中不存在增广路。

    所以总结做题方法就是不断找增广路,有三种方法:

    ①Ford-Fvkerson(DFS)

    伪代码:

    不断地在残余网络中找增广路伪代码:

    FF{

    flow=0;

    while(true)

    {

     

    }

    DFS遍历传参伪代码过程:

    dfs(int now,int t,int cnt)

    {

    if(now==t) return cnt;

    for(i)

    if(!vis[i] && cap>0)

    vis[i]=true;

    d=dfs(next,t,min(cnt,cap)

    if(d>0)

    else

    return 0;

    }

     

    两个技巧:

    ①邻接表

    ②邻接矩阵 [i][j]=cap [j][i]=0;

    F - Flow Problem

    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.

    InputThe 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)OutputFor 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<stdio.h>
     2 #include<string.h>
     3 #include<queue>
     4 using namespace std;
     5 queue <int> q;
     6 int n,m,gra[20][20],path[20],flow[20],st,ed;
     7 int bfs()
     8 {
     9     int i,t;
    10     while(!q.empty()) q.pop();
    11     memset(path,-1,sizeof(path));
    12     path[st]=0;
    13     flow[st]=99999999;
    14     q.push(st);
    15     while(!q.empty())
    16     {
    17         t=q.front();
    18         q.pop();
    19         if(t==ed) break;
    20         for(i=1;i<=n;i++)
    21         {
    22             if(i!=st && path[i]==-1 && gra[t][i])
    23             {
    24                 flow[i]=flow[t]<gra[t][i]?flow[t]:gra[t][i];
    25                 q.push(i);
    26                 path[i]=t;
    27             }
    28         }
    29     }
    30     if(path[ed]==-1) return -1;
    31     return flow[n];
    32 }
    33 
    34 int ek()
    35 {
    36     int max_flow=0,step,now,pre;
    37     while((step=bfs())!=-1)
    38     {
    39         max_flow+=step;
    40         now=ed;
    41         while(now!=st)
    42         {
    43             pre=path[now];
    44             gra[pre][now]-=step;
    45             gra[now][pre]+=step;
    46             now=pre;
    47         }
    48     }
    49     return max_flow;
    50 }
    51 
    52 int main()
    53 {
    54     int i,u,v,cost,T,c,d,e,cas=0;
    55     scanf("%d",&T);
    56     while(T--)
    57     {
    58         scanf("%d%d",&n,&m);
    59         memset(gra,0,sizeof(gra));
    60         for(i=1;i<=m;i++)
    61         {
    62             scanf("%d%d%d",&c,&d,&e);
    63             gra[c][d]+=e;
    64         }
    65         st=1;ed=n;
    66         printf("Case %d: %d
    ",++cas,ek());
    67     }
    68     return 0;
    69 }

    H - Farm Tour

    When FJ's friends visit him on the farm, he likes to show them around. His farm comprises N (1 <= N <= 1000) fields numbered 1..N, the first of which contains his house and the Nth of which contains the big barn. A total M (1 <= M <= 10000) paths that connect the fields in various ways. Each path connects two different fields and has a nonzero length smaller than 35,000.

    To show off his farm in the best way, he walks a tour that starts at his house, potentially travels through some fields, and ends at the barn. Later, he returns (potentially through some fields) back to his house again.

    He wants his tour to be as short as possible, however he doesn't want to walk on any given path more than once. Calculate the shortest tour possible. FJ is sure that some tour exists for any given farm.
    Input
    * Line 1: Two space-separated integers: N and M.

    * Lines 2..M+1: Three space-separated integers that define a path: The starting field, the end field, and the path's length.
    Output
    A single line containing the length of the shortest tour.
    Sample Input
    4 5
    1 2 1
    2 3 1
    3 4 1
    1 3 2
    2 4 2
    Sample Output
    6
    【题意】:给你N个农田、M条无向边以及每条边的长度。现在让你从1走到N再从N走回1,要求不能走重复的边。问你所走的路径总长的最小值。题目保证1到N至少会存在两条边不重复的路径。
    【思路】:最小费用最大流问题:首先设置一个超级源点0,一个超级汇点N+1,因为是1到N的双源最短路,所以直接将0与1相连,cap为2,N与N+1相连,cap为2,因为是无向图,所以每条边要连两次。

    建图:超级源点source,超级汇点sink

    1,source连点1,容量为2,费用为0;

    2,对题目给出的无向边<u, v>建双向边,容量为1(意味着该边只能走一次),费用为边的长度;

    3,N到sink建边,容量为2,费用为0。

    最后跑一次最小费用最大流就可以了。

     
      1 #include <cstdio>
      2 #include <cstring>
      3 #include <queue>
      4 #define MAXN 1010
      5 #define MAXM 50000+10
      6 using namespace std;
      7 struct Edge
      8 {
      9     int from, to, cap, flow, cost, next;
     10 };
     11 Edge edge[MAXM];
     12 int head[MAXN], edgenum;
     13 int dist[MAXN];
     14 int pre[MAXN];
     15 bool vis[MAXN];
     16 int N, M;
     17 int source, sink;
     18 void init()
     19 {
     20     edgenum = 0;
     21     memset(head, -1, sizeof(head));
     22 }
     23 void addEdge(int u, int v, int w, int c) //加边uv
     24 {
     25     Edge E1 = {u, v, w, 0, c, head[u]};
     26     edge[edgenum] = E1;
     27     head[u] = edgenum++;
     28     Edge E2 = {v, u, 0, 0, -c, head[v]};
     29     edge[edgenum] = E2;
     30     head[v] = edgenum++;
     31 }
     32 void getMap()
     33 {
     34     int a, b, c;
     35     source = 0, sink = N+1;
     36     while(M--)
     37     {
     38         scanf("%d%d%d", &a, &b, &c);
     39         addEdge(a, b, 1, c);//建双向边
     40         addEdge(b, a, 1, c);
     41     }
     42     addEdge(source, 1, 2, 0);//超级源点连起点
     43     addEdge(N, sink, 2, 0);//终点连超级汇点
     44 }
     45 bool SPFA(int s, int t) //spfa算法求最小流
     46 {
     47     queue<int> Q;
     48     memset(dist, 0x3f3f3f3f, sizeof(dist));
     49     memset(vis, false, sizeof(vis));
     50     memset(pre, -1, sizeof(pre));
     51     dist[s] = 0;
     52     vis[s] = true;
     53     Q.push(s);
     54     while(!Q.empty())
     55     {
     56         int u = Q.front();
     57         Q.pop();
     58         vis[u] = false;
     59         for(int i = head[u]; i != -1; i = edge[i].next)
     60         {
     61             Edge E = edge[i];
     62             if(dist[E.to] > dist[u] + E.cost && E.cap > E.flow)
     63             {
     64                 dist[E.to] = dist[u] + E.cost;
     65                 pre[E.to] = i;
     66                 if(!vis[E.to])
     67                 {
     68                     vis[E.to] = true;
     69                     Q.push(E.to);
     70                 }
     71             }
     72         }
     73     }
     74     return pre[t] != -1;
     75 }
     76 void MCMF(int s, int t, int &cost)//最小费用最大流算法
     77 {
     78     cost = 0;
     79     while(SPFA(s, t))
     80     {
     81         int Min = 0x3f3f3f3f;
     82         for(int i = pre[t]; i != -1; i = pre[edge[i^1].to])
     83         {
     84             Edge E = edge[i];
     85             Min = min(Min, E.cap - E.flow);
     86         }
     87         for(int i = pre[t]; i != -1; i = pre[edge[i^1].to])
     88         {
     89             edge[i].flow += Min;
     90             edge[i^1].flow -= Min;
     91             cost += edge[i].cost * Min;
     92         }
     93     }
     94 }
     95 int main()
     96 {
     97     while(scanf("%d%d", &N, &M) != EOF)
     98     {
     99         init();
    100         getMap();
    101         int cost;//设置最小费用
    102         MCMF(source, sink, cost);
    103         printf("%d
    ", cost);
    104     }
    105     return 0;
    106 }


    雪儿言
  • 相关阅读:
    (转)树状数组
    poj 3041 Asteroids(二分图最小顶点覆盖)
    poj 2513 Colored Sticks
    (转)优先队列的用法 附:poj2442 poj1442
    poj 1094 Sorting It All Out (拓补)
    poj 3026 Borg Maze(bfs+最小生成树)
    poj 3349 Snowflake Snow Snowflakes
    poj 3020 Antenna Placement(二分图的最大匹配)
    mysql explain
    php strtotime
  • 原文地址:https://www.cnblogs.com/weixq351/p/9380663.html
Copyright © 2020-2023  润新知