2879: [Noi2012] Food festival time limit:10 Sec Memory limit:512 MB
submit:1366 solved:737
[Submit] [Status] [Discuss] Description
CZ City in order to welcome students all over the country, specially held a grand food festival. As a gourmet guest who likes early taste, little M is naturally reluctant to miss the feast. He soon tasted all the delicacies of the Gourmet festival. However, the desires of the early adopters are difficult to satisfy. Even though all the dishes were delicious and the cooks were quick to cook, little m still felt that there was no food on his table that had been placed on others ' tables as an intolerable thing. So small m began to study the problem of cooking order, that is, arrange a cooking order so that the students have the shortest waiting time. Small m found that the food festival has a total of n different kinds of dishes. Every time you order a meal, each student can choose one of the dishes. There are a total of M cooks to make these dishes. When all the students ordered the meal, the food production task was assigned to each chef. Then each chef will start cooking at the same time. Cooks will make them in the order they are requested and can only produce one person at a time. In addition, small m also found another interesting thing: Although the M chef will make all the n vegetables, but for the same dish, the different cooks ' production time may not be the same. He will dish with 1, 2, ..., n sequentially numbered, cooks with 1, 2, ..., m sequentially numbered, the time of the first chef of the J cooks to make the vegetables is ti,j. Little M thinks: the waiting time for each classmate is the total length of time for all cooks to start cooking, to their own portion of the finished dish. In other words, if a classmate ordered a dish that was made by a chef, his waiting time was the sum of the time the chef cooked the K-course. The total waiting time is the sum of the waiting time for all the students. Now, small m found all the students a la carte information: There are PI students ordered the first I vegetable products (i=1, 2, ..., n). What he wants to know is what the minimum total waiting time is.
Input
The 1th line of the input file contains two positive integers n and m, indicating the number of dishes and the number of cooks. Line 2nd contains n positive integers, where number i is pi, which indicates the number of people who have vegetables in the first order. Then there are n rows, each containing m non-negative integers, and the number of J in line I of the n row is Ti,j, which indicates the time it takes for the chef to make the first I vegetable. Each line in the input file is separated by a space between two digits, and there are no extra spaces at the end of the line.
Output
The output contains only one line with an integer, which is the minimum value for the total wait time.
Sample Input3 2
3 1 1
5 7
3 6
8 9
Sample Output47
"Sample description"
Cook 1 First make 1 dishes 2, then make 2 dishes 1. The waiting time for the 3 students who ordered the 3 courses was 3,3+5=8,3+5+5=13.
Cook 2 First make 1 dishes 1, then make 1 dishes 3. The waiting time for the 2 students who ordered the 2 courses was 7,7+9=16.
The total waiting time is 3+8+13+7+16=47.
Although the dish 1 and the dish 3 are made faster by the chef 1, if these dishes are made by the Chef 1, the total waiting time is instead longer. If the above practice, the 1 dishes 1 and 1 dishes 3 adjusted to cook 2 made, so that the chef 2 will not idle, the total waiting time is shorter.
It can be proved that there is no better ordering scheme.
"Data size and conventions"
For 100% of the data, N <=, M <=, p <=, ti,j <= 1000 (of which P =∑PI, the total number of students in a la carte).
The N, m, and P values for each group of data are as follows:
Test point number N m p
1 n = 5 m = 5 p = 10
2 n = + M = 1 p = 400
3 N = + M = 2 p = 300
4 n = + M = p = 40
5 N = 5 m = p = 100
6 n = ten m = p = 200
7 n = m = p = 400
8 n = + M = p = 600
9 N = + M = p = 800
Ten n = + M = p = 800
Hintsource
Solution
From the data range, the normal network flow range of about 200, where the scope is not large , then consider the aspect of the split, the analysis found that the problem needs to be removed a large number of points, the normal processing must not
The method of building a map of violence, very good to think:
Split each chef into a $\sum_{i=1}^{n}p[i]$ point, with all the dishes connected to each point (and the car is a huge similarity)
But this time, it is found that such a plot to run the cost of the flow will undoubtedly tle, then in the process of building the diagram requires some skills:
Dynamic mapping, found that each chef's countdown to the first dish to answer the contribution of 1 time times the time, the second-to-last course of the answer to the contribution of twice times the time ... In turn
So consider the reverse dynamic plus edge, after a dish, a new point, so that can ensure that the chef points are (p+m) level
On an augmented road only one chef cooks--all the others are retired--so find the first chef to add some
Code
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<queue>using namespacestd;intRead () {intx=0, f=1;CharCh=GetChar (); while(ch<'0'|| Ch>'9') {if(ch=='-') f=-1; Ch=GetChar ();} while(ch>='0'&& ch<='9') {x=x*Ten+ch-'0'; Ch=GetChar ();} returnx*F;} #defineMAXP 1000#defineMAXN 100010#defineMAXM 1000010intN,m,s,t,ans,tot;intP[MAXP],TT[MAXP][MAXP];structdata{int from, Next,to,cap,cost;} edge[maxm<<2]; intHEAD[MAXN], from[maxn],cnt=1; voidAddintUintVintWintc) {cnt++;EDGE[CNT]. from=u;edge[cnt].to=v; Edge[cnt].next=head[u];head[u]=cnt; Edge[cnt].cost=c;edge[cnt].cap=W;} voidInsertintUintVintWintc) {Add (u,v,w,c); Add (V,u,0,-c);} #defineINF 0x7fffffffBOOLVISIT[MAXN];intDIS[MAXN];BOOLSPFA () {Queue<int>Q; for(intI=s; i<=t; i++) dis[i]=inf; Q.push (S); Visit[s]=1; dis[s]=0; while(!Q.empty ()) { intnow=Q.front (); Q.pop (); for(intI=head[now]; I I=edge[i].next)if(Edge[i].cap && dis[now]+edge[i].cost<Dis[edge[i].to]) {Dis[edge[i].to]=dis[now]+edge[i].cost; from[edge[i].to]=i; if(!visit[edge[i].to]) Q.push (edge[i].to), visit[edge[i].to]=1; } Visit[now]=0; } returndis[t]!=inf;} voidMincostmaxflow () {intflow=inf,a,b,x; for(intI= from[T]; I I= from[Edge[i]. from]) {Flow=min (flow,edge[i].cap); if(!edge[i]. from) x=edge[i].to,a= (x1)/tot+1, b=x%tot+1; } for(intI= from[T]; I I= from[Edge[i]. from]) Edge[i].cap-=flow,edge[i^1].cap+=flow,ans+=edge[i].cost*flow; for(intI=1; i<=n; i++) Insert ((a-1) *tot+b,m*tot+i,1, b*Tt[i][a]); } voidMake () {S=0, t=100001; for(intI=1; i<=tot*m; i++) Insert (S,i,1,0); for(intI=1; i<=n; i++) Insert (M*tot+i,t,p[i],0); for(intI=1; i<=m; i++) for(intj=1; j<=n; J + +) Insert ((I-1) *tot+1, M*tot+j,1, Tt[j][i]); } intMain () {n=read (), m=read (); for(intI=1; i<=n; i++) P[i]=read (), tot+=P[i]; for(intI=1; i<=n; i++) for(intj=1; j<=m; J + +) Tt[i][j]=read (); Make (); while(SPFA ()) Mincostmaxflow (); printf ("%d\n", ans); return 0; }
Usually write fee flow like to write zkw fee flow, this problem with SPFA based on the cost of flow will be more convenient (so yy a bit t ...) so I found a template ... )
"BZOJ-2879" Food festival minimum cost maximum flow + dynamic map