Algorithm Implementation Question 8-10 napkin plan question (Exercise 8-21)
? Problem description:
In the successive n days of a restaurant, the number of napkins required each day varies. Assume that RI napkin (I = 1,
2 ,..., N ). You can purchase a new napkin at a restaurant. The cost of each napkin is P. Or, you can send the old napkin to the wash area,
It takes M days to wash a piece, and the cost is f points; or it is sent to the slow wash part, it takes n days to wash a piece (n> m), the cost is S <F points.
At the end of each day, the restaurant must decide how many dirty napkins will be delivered to the quick wash department, how many napkins will be delivered to the slow wash department, and how many
Save a few blocks to extend the traffic. However, the sum of the number of napkins washed each day and the number of new napkins purchased must meet the needs of the day.
Design an algorithm to reasonably arrange the napkin use plan for the restaurant in N days to minimize the total cost.
? Programming task:
Program to find an optimal napkin use plan.
? Data input:
Input data is provided by the file input.txt. There are 6 positive integers N, P, M, F, N, S in the row 1st of the file. N is to arrange the napkin
Use the planned number of days; P indicates the cost of each new napkin; M indicates the number of days required for the quick wash part; F indicates the number of days required for the quick wash part.
The cost of a napkin; n is the number of days required to wash a napkin in the slow WASH; s is the cost required to wash a napkin in the slow wash.
The next n rows are the number of napkins required by the restaurant every day in N successive days.
? Result output:
At the end of the program running, output the minimum total cost of using the napkin in the restaurant for N consecutive days to the file output.txt
.
Input File example
Output file example
Input.txt
Output.txt
3 10 2 3 2
5
6
7
145
Question:
In fact, it is not difficult to figure out the diagram. The key lies in the processing of details-edge capacity settings.
In addition, pay attention to reading questions. The question says that the washing can be postponed. Therefore, I should be connected to an edge with a capacity of inf from I to I + 1, because it may have been accumulated for several days.
After this point is noted, similarly, the I + T2 + n connection edge capacity also needs to be INF, and the I + T2 + N capacity is also inf
In fact, this question is a bit like saveprint, and the diagram is clever.
Code:
1 uses math; 2 const inf=maxlongint; 3 type node=record 4 from,go,next,v,c:longint; 5 end; 6 var e:array[0..2000000] of node; 7 pre,head,q,d:array[0..1000000] of longint; 8 v:array[0..1000000] of boolean; 9 i,j,n,s,t,l,r,mincost,tot,p,t1,t2,p1,p2,x:longint; 10 procedure ins(x,y,z,w:longint); 11 begin 12 inc(tot); 13 with e[tot] do 14 begin 15 from:=x;go:=y;v:=z;c:=w;next:=head[x];head[x]:=tot; 16 end; 17 end; 18 procedure insert(x,y,z,w:longint); 19 begin 20 ins(x,y,z,w);ins(y,x,0,-w); 21 end; 22 function spfa:boolean; 23 var i,x,y:longint; 24 begin 25 fillchar(v,sizeof(v),false); 26 for i:=s to t do d[i]:=inf; 27 l:=0;r:=1;q[1]:=s;d[s]:=0;v[s]:=true; 28 while l<r do 29 begin 30 inc(l); 31 x:=q[l];v[x]:=false; 32 i:=head[x]; 33 while i<>0 do 34 begin 35 y:=e[i].go; 36 if (e[i].v<>0) and (d[x]+e[i].c<d[y]) then 37 begin 38 d[y]:=d[x]+e[i].c; 39 pre[y]:=i; 40 if not(v[y]) then 41 begin 42 v[y]:=true; 43 inc(r); 44 q[r]:=y; 45 end; 46 end; 47 i:=e[i].next; 48 end; 49 end; 50 exit(d[t]<>inf); 51 end; 52 procedure mcf; 53 var i,tmp:longint; 54 begin 55 mincost:=0; 56 while spfa do 57 begin 58 tmp:=inf; 59 i:=pre[t]; 60 while i<>0 do 61 begin 62 tmp:=min(tmp,e[i].v); 63 i:=pre[e[i].from]; 64 end; 65 inc(mincost,tmp*d[t]); 66 i:=pre[t]; 67 while i<>0 do 68 begin 69 dec(e[i].v,tmp); 70 inc(e[i xor 1].v,tmp); 71 i:=pre[e[i].from]; 72 end; 73 end; 74 end; 75 procedure init; 76 begin 77 tot:=1; 78 readln(n,p,t1,p1,t2,p2); 79 s:=0;t:=2*n+1; 80 for i:=1 to n do 81 begin 82 readln(x); 83 insert(s,i,x,0); 84 insert(s,i+n,x,p); 85 insert(i+n,t,x,0); 86 if i+1<=n then insert(i,i+1,inf,0); 87 if i+t1<=n then insert(i,i+t1+n,inf,p1); 88 if i+t2<=n then insert(i,i+t2+n,inf,p2); 89 end; 90 end; 91 procedure main; 92 begin 93 mincost:=0; 94 mcf; 95 writeln(mincost); 96 end; 97 begin 98 assign(input,‘input.txt‘);assign(output,‘output.txt‘); 99 reset(input);rewrite(output);100 init;101 main;102 close(input);close(output);103 end.
View code