The minimum cost maximum flow Los Valley 1251

Meaning of the questions:

This is a minimum cost (cost refers to a monovalent) topic maximum flow.

First, we split point, will one day split into morning and night, every night will be dirty napkins (Source: Spent the morning napkin, in order to obtain from the origin of this question in understandable), every morning there are clean napkin (source: buy, quick wash shop and slow wash shop).

1. From the origin to a traffic every day and even at night the same day with the napkin x, 0 cost side, showing every night to get dirty napkins article x from the starting point.

2. x napkin that day with every day from morning even to a meeting point for the flow rate, the cost side of the 0 per day during the day, providing representation x bar napkins to clean the sink, represents the i-day napkin enough flow over time. 3. From each night to even the next night is a flow INF, 0 cost side, showing every night dirty napkins can be left to the next night (not a morning note, because dirty napkins can not be used in the morning) .

4. From each night to this day + quick wash of the number of days t1 with that one morning not even a flow of INF, cost edge amount of money quick wash used to indicate every night can send quick wash unit, in the i + t1 days of receipt napkin morning.

5. Similarly, every day from night to day + slow to wash the number of days t2 morning, not even a traffic that day as INF, costs slow wash of the amount of money used for the side representing every night can be sent to the slow, washing machine, on the ground i + t2 morning received napkin.

6. From the start to the morning not even a traffic every day to INF, the cost for the purchase of the number of edges of napkins money, represents every morning to buy napkins. Note that the above 6:00 need to build the reverse side! 3-6 points need to make a judgment (i.e., the side to be connected to <= n)

 1 #include<cstdio>
 2 #include<queue>
 3 #include<cstring>
 4 #include<queue>
 5 #include<algorithm>
 6 #define INF 2147483647
 7 #define LL long long
 8 using namespace std;
 9 const int maxn=1e5+10;
10 queue<int>q;
11 int n,m,m1,t1,m2,t2,num=-1,st,ed;
12 struct node{
13     int u,v,w,cost,next;
14 }G[maxn];
15 int b[maxn],head[maxn],pre[maxn],pos[maxn],max_flow[maxn];
16 LL dis[maxn];
17 bool vis[maxn];
18 void build(int u,int v,int w,int cost)
19 {
20     G[++num].u=u;G[num].v=v;G[num].w=w;G[num].cost=cost;G[num].next=head[u];head[u]=num;
21     G[++num].u=v;G[num].v=u;G[num].w=0;G[num].cost=-cost;G[num].next=head[v];head[v]=num;
22 }
23 bool spfa()
24 {
25     memset(vis,true,sizeof(vis));
26     vis[st]=false;
27     memset(dis,63,sizeof(dis));
28     dis[st]=0;
29     max_flow[st]=INF;
30     q.push(st);
31     while(!q.empty()){
32         int u=q.front();
33         vis[u]=true;
34         q.pop();
35         for(int i=head[u];i!=-1;i=G[i].next){
36             int v=G[i].v;
37             if(G[i].w>0&&dis[v]>dis[u]+G[i].cost){
38                 dis[v]=dis[u]+G[i].cost;
39                 pos[v]=u;
40                 pre[v]=i;
41                 max_flow[v]=min(max_flow[u],G[i].w);
42                 if(vis[v]){
43                     q.push(v);
44                     vis[v]=false;
45                 }
46             }
47         }
48     }
49     return dis[ed]<4557430888798830399;
50 }
51 LL flow()
52 {
53     LL ans=0;
54     while(spfa()){
55         ans+=max_flow[ed]*dis[ed];
56         for(int i=ed;i!=st;i=pos[i]){
57             G[pre[i]].w-=max_flow[ed];
58             G[pre[i]^1].w+=max_flow[ed];
59         }
60     }
61     return ans;
62 }
63 int main()
64 {
65     int x;
66     scanf("%d",&n);
67     st=0,ed=2*n+1;
68     memset(head,-1,sizeof(head));
69     for(int i=1;i<=n;i++){
70         scanf("%d",&x);
71         build(st,i,x,0);// every night obtained starting from the dirty strip napkins x 
72          Build (n-i +, ED, x, 0 ); // every day during the day, provided to clean napkins x sink article, denotes the i-th day napkin enough flow over time with 
73 is      }
 74      Scanf ( " % D% D% D% D% D " , & m, & T1, & M1, & T2, & M2);
 75      for ( int I = . 1 ; I <= n-; I ++ ) {
 76          IF ( + I . 1 <= n-) Build (I, I + . 1 , INF, 0 ); // every evening for the dirty napkins in the following night 
77          IF (T1 + I <= n-) Build (I, I + n-+ t1, INF, M1); // every night can send quick wash unit, received at the i + t1 morning napkin 
78          IF(i + t2 <= n-) Build (I, i + n-+ t2, INF, M2); // every night can be sent to the slow wash unit, received at the i + t2 morning napkin 
79          Build (ST, I n-+, INF, m); // every morning can later napkin 
80      }
 81      the printf ( " % LLD " , Flow ());
 82 }

 

Guess you like

Origin www.cnblogs.com/pangbi/p/11577720.html