[24] network flow napkin

Old 题重 WA 233

Original title:

 

 n<=2000

 

A cost flow, simple

Split point, s to point p represents the flow directly buying cost ∞, point to the OUT point r [i] denotes must have daily r [i] Article, the point t ∞ is used to ensure the flow out side of the pigeon, the point then i + m i + n or the point represented wash, the point to point traffic ∞ represents the next day may be washed with Tun

Then the sample will not pass 233

Previous turned blog, found my first time to do this problem three years ago when he made the same mistake 233

My previous blog believe that if you choose the wrong wash, then the cost of this is to buy napkins + wash, greater than the direct buy, so it will not augmented

This interpretation does not, in the case of cost flow is to ensure maximum flow of minimum cost, then I'll buy a wash, each side is running full flow, lower costs, why not cost flow scheme? ? ?

Lower cost is yes, no cost flow problem, the problem is that each side ran full stream flow may not be equal to the maximum

This error is largely still unclear on the concept of maximum flow

The middle of the whole side of the pigeon, the use of flow is indeed the biggest flow

But if the above diagram built first to buy wash, then the two sides of a pigeon flow, total flow than the maximum flow

The maximum flow only prove Theorem both numerically equal

In fact, if the program finish buy + wash residue network (including anti-edge) drawn out, can be found from the source to a point, then the point away from the counter-flow edge to the point

This allows the anti side, although increased costs, but also to provide an opportunity to run again from source to sink

It can therefore sum up the experience, the greatest attention must flow from source to sink flow ah

Know the nature of the error, positive solution is not difficult to understand

To avoid errors before, we want to ensure that each clean towel (either bought or wash) must occupy a complete flow from source to sink

To note a critical property will stably produce r [i] Article dirty towels per day

Then we separate production and marketing, to buy and wash considered separately

Because fixed will produce dirty towels every day, so I do not need to lead a side out from wash clean towel representation

A day directly to split said dirty spots and good point, and then from the dirty to the point i + m i + n or days even better edge, then the source and flow of dirty spots r [i], r denotes a maximum yield per day [i ] Article dirty towel

Source then even better side, represents a direct buy, good to sink a flow rate of r [i], represents the day must scrounge r [i] piece of good towel

Finally, a good point to even better the next day the village edge represents a towel on the line

 

Code:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cmath>
 6 using namespace std;
 7 #define LL long long
 8 int rd(){int z=0,mk=1;  char ch=getchar();
 9     while(ch<'0'||ch>'9'){if(ch=='-')mk=-1;  ch=getchar();}
10     while(ch>='0'&&ch<='9'){z=(z<<3)+(z<<1)+ch-'0';  ch=getchar();}
11     return z*mk;
12 }
13 const int oo=1000000007;
14 struct edg{int nxt,y,v,u;}e[31000];  int lk[4100],ltp=1;
15 void ist(int x,int y,int z,int w){
16     e[++ltp]=(edg){lk[x],y,z,w};  lk[x]=ltp;
17     e[++ltp]=(edg){lk[y],x,0,-w};  lk[y]=ltp;
18 }
19 int n,m,ft,st,fc,sc,a[2100];
20 int s,t;
21 int dstc[4100];
22 int q[41000],hd=0;  bool vstd[4100];
23 int lst[4100],lse[4100];
24 bool spfa(){
25     for(int i=1;i<=t;++i){
26         vstd[i]=false;
27         dstc[i]=oo;
28     }
29     dstc[q[hd=1]=s]=0;
30     for(int k=1;k<=hd;++k){
31         for(int i=lk[q[k]];i;i=e[i].nxt)
32             if(e[i].v && dstc[q[k]]+e[i].u<dstc[e[i].y]){
33                 DSTC [E [I] .y] DSTC = [Q [K]] + E [I] .u;
 34 is                  LST [E [I] .y] = Q [K], LSE [E [I] .y] = I;
 35                  IF (! {Vstd [E [I] .y])
 36                      Q [HD ++] = E [I] .y;
 37 [                      Vstd [E [I] .y] = to true ;
 38 is                  }
 39              }
 40          Vstd [Q [K]] = to false ;
 41 is      }
 42 is      // return dstc [t]; note that not Analyzing dstc [t] is not 0 
43 is      return ! dstc [t] = OO;
 44 is  }
 45  LL cstflw () {
 46     LL bwl=0;
47     while(spfa()){
48         int flw=oo;
49         for(int i=t;i!=s;i=lst[i])
50             flw=min(flw,e[lse[i]].v);
51         for(int i=t;i!=s;i=lst[i]){
52             bwl+=flw*e[lse[i]].u;
53             e[lse[i]].v-=flw,e[lse[i]^1].v+=flw;
54             //cout<<i<<"<-";
55         }
56         //cout<<endl;
57     }
58     return bwl;
59 }
60 int main(){
61     //freopen("ddd.in","r",stdin);
62     cin>>n;
63     for(int i=1;i<=n;++i)  a[i]=rd();
64     cin>>m>>ft>>fc>>st>>sc;
65     s=n+n+1,t=n+n+2;
66     for(int i=1;i<=n;++i){
67         /*ist(i,i+n,a[i],0);
68         ist(s,i,oo,m);
69         ist(i+n,t,oo,0);
70         if(i<n)  ist(i,i+1,oo,0);
71         if(i+ft<=n)  ist(i+n,i+ft,oo,fc);
72         if(i+st<=n)  ist(i+n,i+st,oo,sc);*/
73         ist(s,i+n,oo,m);
74         ist(i+n,t,a[i],0);
75         ist(s,i,a[i],0);
76         if(i<n)  ist(i,i+1,oo,0);
77         if(i+ft<=n)  ist(i,i+ft+n,oo,fc);
78         if(i+st<=n)  ist(i,i+st+n,oo,sc);
79     }
80     cout << cstflw () << endl;
81      return  0 ;
82 }
View Code

 

Guess you like

Origin www.cnblogs.com/cdcq/p/11872070.html