Bzoj 2879: [Noi2012] food festival (fee stream + dynamic plus edge)

Source: Internet
Author: User

2879: [Noi2012] Food festival time limit: ten Sec Memory Limit: MB
Submit: 1453 Solved: 778
[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 Input
3 2
3 1 1
5 7
3 6
8 9
Sample Output
47

"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
HINT

Source

[Submit] [Status] [Discuss]


Puzzle: Cost flow + dynamic plus edge

Just started to write the simple cost stream tle.

So we can only use some tall things-dynamic add-on.

In fact, this problem in addition to the data range, and SCOI 2007 repair car is a problem.

Building Map

Point of origin, all chefs split (one chef Sigma Pi) capacity of 1, cost 0

Meeting point capacity of 1 per vegetable, at a cost of 0

Should be a chef split each point with a different dish with a capacity of 1, the cost of t[i][j]xk side (k means the countdown to do, because the last to do only this person need to wait, do this dish time will not be other dishes), but we found that in fact these sides must have a lot of will not go to.

So we just have to start with the countdown to the first person, that is t[i][j] even good.

Each time the augmentation, in fact, as long as one is to do the rest of the dish is to withdraw vegetables, so we need to find out the source point of the latter point, and then calculate which chef he belongs to the first dish, such as the 2nd course of the chef, then we will all the dishes to the first chef of the 3rd side of the link, and then

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <  cmath> #define N 100003 #define M 1200000 #define INF 1000000000 using namespace std;  int n,m,tot,mincost;  int next[m],v[m],remain[m],c[m],sum,q[n];  int point[n],can[n],dis[n],last[n],p[n],t[50][120];      void Add (int x,int y,int z,int k) {tot++; next[tot]=point[x]; point[x]=tot; v[tot]=y; remain[tot]=z; c[tot]=k; tot++; Next[tot]=point[y]; Point[y]=tot; V[tot]=x; remain[tot]=0;  C[tot]=-k;      } int Addflow (int s,int t) {int now=t; int ans=inf;      int k,a,b;          while (now!=s) {ans=min (Ans,remain[last[now]]);              if (v[last[now]^1]==s) {K=v[last[now]]; A= (k-1)/sum+1;           b= (k)%sum+1;      } Now=v[last[now]^1];      } now=t;          while (now!=s) {Remain[last[now]]-=ans;          Remain[last[now]^1]+=ans;     NOW=V[LAST[NOW]^1]; } mincost+=ans*dis[t];  for (int i=1;i<=m;i++) Add ((A-1) *sum+b,n*sum+i,1,b*t[i][a]);    } bool SPFA (int s,int t) {for (int i=s;i<=t;i++) dis[i]=inf,can[i]=0; dis[s]=0;      Can[s]=1;    int head=0,tail=0;    Q[++tail]=s;          while (head<tail) {int now=q[++head];              for (int i=point[now];i!=-1;i=next[i]) if (Dis[v[i]]>dis[now]+c[i]&&remain[i]) {              Dis[v[i]]=dis[now]+c[i];              Last[v[i]]=i;                  if (!can[v[i]]) {can[v[i]]=1;             Q[++tail]=v[i];      }} can[now]=0;      } if (Dis[t]==inf) return false;  return true;      } int main () {scanf ("%d%d", &m,&n);  Tot=-1;      sum=0; memset (Next,-1,sizeof (next));      memset (point,-1,sizeof (point));      for (int i=1;i<=m;i++) scanf ("%d", &p[i]), sum+=p[i];      int num=100001; for (int i=1;i<=m;i++) for (int j=1;j<=n;j++) scanf ("%d", &t[i][j]);    for (int i=1;i<=n*sum;i++) Add (0,i,1,0);    for (int i=1;i<=m;i++) Add (n*sum+i,num,p[i],0);    for (int i=1;i<=n;i++) for (int k=1;k<=m;k++) Add ((i-1) *sum+1,n*sum+k,1,t[k][i]);      while (SPFA (0,num)) Addflow (0,num);      printf ("%d\n", mincost);  return 0;   }



Bzoj 2879: [Noi2012] food festival (fee stream + dynamic plus edge)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.