Luo Gu P2604 + maximum flow minimum cost maximum flow

Topic links: https://www.luogu.org/problem/P2604

Title Description

Given a directed graph, each edge has a capacitance C and an expansion costs W. Here the expansion costs refer to the costs required to expand the capacity of 1. Requirements: 1, without expansion, to a maximum flow of N; 2, expansion will increase the minimum cost of K 1 to N, the maximum flow.

Problem-solving ideas:

1. For 1, the maximum flow can be directly run again, the cost to zero.

2. For 2, in the network covers a maximum residual stream bordered on each side plus INF capacity, cost side expansion costs. This ensures that the cost is correct, in order to ensure expansion of k, 0 plus a source, a capacity of k, 0 cost, even to 1:00. Run minimum cost (0, n) can be.

3. Think about why you can bordered on the remaining network, because the edge of the unit cost than traffic costs, but the cost of expansion. To have a side full expansion to increase the flow of traffic under other side of the stream, then the cost of the other side is zero, because there is no expansion. After the other side is also full flow, it needs expansion costs, but also will use the added edge of inf (because the residual network

The cost is zero, it will give priority to use)

Code:

  . 1 #include <stdio.h>
   2 #include < String .h>
   . 3 #include <Queue>
   . 4 #include <algorithm>
   . 5  #define MEM (A, B) Memset (A, B, the sizeof (A))
   . 6  the using  namespace STD;
   . 7  const  int MAXM = 5010 ;
   . 8  const  int MAXN = 5000 ;
   . 9  const  int INF = 0x3f3f3f3f ;
 10  
. 11  int n-, m, K; // n-point and m have the edges, expansion K 
12 is Queue < int> Q;
 13 int dep[MAXN], last[MAXN], pre[MAXN], dis[MAXN], flow[MAXN], vis[MAXN];
 14 
 15 struct Node
 16 {
 17     int a, b, c, d;
 18 }no[MAXM];
 19 int tot;
 20 
 21 struct Edge
 22 {
 23     int to, next, flow, dis;
 24 }edge[5 * MAXN];
 25 int head[MAXN], cnt;
 26 
 27 void add(int a, int b, int c, int d)
 28 {
 29     edge[++ cnt].to = b;
 30     edge[cnt].next = head[a];
 31     edge[cnt].flow = c;
 32     edge[cnt].dis = d;
 33     head[a] = cnt;
 34 }
 35 
 36 int spfa(int st, int ed)
 37 {
 38     while(!Q.empty())
 39         Q.pop();
 40     mem(vis, 0), mem(flow, inf), mem(dis, inf), pre[ed] = -1;
 41     dis[st] = 0;
 42     vis[st] = 1;
 43     Q.push(st);
 44     while(!Q.empty())
 45     {
 46         int now = Q.front();
 47         Q.pop();
 48         vis[now] = 0;
 49         for(int i = head[now]; i != -1; i = edge[i].next)
 50         {
 51             int to = edge[i].to;
 52             if(edge[i].flow > 0 && dis[to] > dis[now] + edge[i].dis)
 53             {
 54                 dis[to] = dis[now] + edge[i].dis;
 55                 last[to] = i;
 56                 pre[to] = now;
 57                 flow[to] = min(flow[now], edge[i].flow);
 58                 if(!vis[to])
 59                 {
 60                     vis[to] = 1;
 61                     Q.push(to);
 62                 }
 63             }
 64         }
 65     }
 66     return pre[ed] != -1;
 67 }
 68 
 69 int min_cost, max_flow;
 70 void MCMF(int st, int ed)
 71 {
 72     min_cost = 0;
 73     max_flow = 0;
 74     while(spfa(st, ed))
 75     {
 76         int now = ed;
 77         min_cost += flow[ed]*dis[ed];
 78         max_flow += flow[ed];
 79         while(now != st)
 80         {
 81             edge[last[now]].flow -= flow[ed];
 82             edge[last[now] ^ 1].flow += flow[ed];
 83             now = pre[now];
 84         }
 85     }
 86 }
 87 
 88 int main()
 89 {
 90     scanf("%d%d%d", &n, &m, &k);
 91     mem(head, -1), cnt = -1, tot = 0;
 92     for(int i = 1; i <= m; i ++)
 93     {
 94         int a, b, c, d;
 95         scanf("%d%d%d%d", &a, &b, &c, &d);
 96         no[i].a = a, no[i].b = b, no[i].c = c, no[i].d = d;  //把输入的边记录下来
 97         add(a, b, c, 0);
 98         add(b, a, 0, 0);
 99     }
100     MCMF(1, n-);
 101      the printf ( " % D " , max_flow);
 102      for ( int I = . 1 ; I <= m; I ++) // on the remaining capacity of the network plus Collage cost inf D 
103      {
 104          the Add (NO [I] II.A, NO [I] .B, INF, NO [I] 2.d);
 105          the Add (NO [I] .B, NO [I] II.A, 0 , - NO [I ] 2.d);
 106      }
 107      the add ( 0 , . 1 , k, 0 ); // add a new source 0, the capacity expansion requires k, 0 cost. So run full flow must be k. 
108      the Add ( . 1 , 0 ,0, 0);
109     MCMF(0,n);
110     printf("%d\n", min_cost);
111     return 0;
112 }
View Code

 

Guess you like

Origin www.cnblogs.com/yuanweidao/p/11275982.html