Food Festival: cost flow, build dynamic edge, split point

Description

CZ City to welcome the students across the country, specially organized a grand food festival.

As an early adopters of foodies, small M would not want to miss the party. He soon tasted the food festival all the food.

However, early adopters desire is insatiable. Despite all the dishes are delicious, chef cook very fast, small M still feel the table is not already on the table people food is an intolerable thing.

So the little M began to study the problem of cooking sequence, ie the order to arrange a cooking makes the students of the shortest waiting time.

M found that small, food festival there are n different dishes. Every meal, every student can choose one of a dish .

A total of m chefs to make these dishes. When all the students end point of the meal, dishes production tasks will be assigned to each chef.

Then each chef will also start the cooking. The chefs will be made in accordance with the requirements of the order, and you can only make one person .

In addition, also it found small M Another interesting thing - although this m are cooks all prepared n vegetables products, but for the same dishes, chef different production time is not necessarily the same .

He dishes are numbered, chefs are numbered, the i- th chefs j- time items recorded as vegetables Tij .

Small M said: waiting time for each student for all chefs cook from the beginning, to share their own dishes so far completed a total length of time.

In other words, if a student point of the dish is a chef's first i-course, he's waiting time is this chef i-1 of the time-course and. The total waiting time is the waiting time for all students and .

Now, to find a small M ordering information for all students - i have a student point of the j- grow vegetables products . He wanted to know the smallest total waiting time is.

 

n <= 40, m <= 100, the total ordering number <= 800

 

 

Such a small range of data, network flow is certainly ah. . .

But look closely, seemingly not small, 800 What the hell is that?

Since this question depends on the total time, so we went to consider each person's contribution.

Your contribution to the total time is the time your dish × (number +1 behind you). 1 that is your own.

So we cook split into multiple points indicates that the chefs do i penultimate course, traffic is limited to 1.

Then the first side of the cost of doing to grow vegetables k j th i cook penultimate dish is even i * t [k] [j].

Then run a cost flow calculated the total cost just fine.

One of the most simple and the idea of ​​violence is to the point completely open on all sides to build.

 1 #include<cstdio>
 2 int n,m,x[450],num[1005][8005],t[450][8005],cn,tot,ans;
 3 int fir[222222],l[20000005],to[20000005],c[20000005],v[20000005],cnt=1;
 4 int iq[222222],q[222222],dt[222222],pre[222222];
 5 void link(int a,int b,int w,int C){l[++cnt]=fir[a];fir[a]=cnt;to[cnt]=b;v[cnt]=w;c[cnt]=C;}
 6 bool SPFA(){
 7     for(int i=1;i<=cn;++i)dt[i]=1234567890;
 8     for(int h=1,t=1;h<=t;++h,iq[q[h]]=0)for(int i=fir[q[h]];i;i=l[i])if(dt[to[i]]>dt[q[h]]+c[i]&&v[i]){
 9         dt[to[i]]=dt[q[h]]+c[i];pre[to[i]]=i;
10         if(!iq[to[i]])q[++t]=to[i],iq[to[i]]=1;
11     }
12     return dt[cn]!=1234567890;
13 }
14 int main(){
15     scanf("%d%d",&n,&m);cn=n;
16     for(int i=1;i<=n;++i)scanf("%d",&x[i]),link(0,i,x[i],0),link(i,0,0,0),tot+=x[i];
17     for(int i=1;i<=n;++i)for(int j=1;j<=m;++j)scanf("%d",&t[i][j]);
18     for(int j=1;j<=m;++j)for(int k=1;k<=tot;++k)num[j][k]=++cn;
19     cn++;
20     for(int j=1;j<=m;++j)for(int k=1;k<=tot;++k)link(num[j][k],cn,1,0),link(cn,num[j][k],0,0);
21     for(int i=1;i<=n;++i)for(int j=1;j<=m;++j)for(int k=1;k<=tot;++k)
22         link(i,num[j][k],1,t[i][j]*k),link(num[j][k],i,0,-t[i][j]*k);
23     while(SPFA())for(int i=pre[cn];i;i=pre[to[i^1]])v[i]--,v[i^1]++,ans+=c[i];
24     printf("%d\n",ans);
25 }
T60

But the count a little, you build a 800 * 100 points, 40 * 100 * 800 changed, ran 800 times SPFA.

It is certainly unacceptable.

But in fact, you get a chef to do a countdown second course on the premise that he has to do the inverse of the first course, or else will be a penultimate penultimate UB.

Then optimized according to this idea, if a chef made a dish, then so do his side a point and built out of food.

An array of open bigger.

 1 #include<cstdio>
 2 int n,m,x[450],num[1005][8005],t[450][8005],cn,ans,tot;
 3 int fir[222222],l[20000005],to[20000005],c[20000005],v[20000005],tt[20000005],cnt=1;
 4 int iq[222222],q[222222],dt[222222],pre[222222],al[1005][8005];
 5 void link(int a,int b,int w,int C,int T){l[++cnt]=fir[a];fir[a]=cnt;to[cnt]=b;v[cnt]=w;c[cnt]=C;tt[cnt]=T;}
 6 bool SPFA(){
 7     for(int i=1;i<=cn;++i)dt[i]=1234567890;
 8     for(int h=1,t=1;h<=t;++h,iq[q[h]]=0)for(int i=fir[q[h]];i;i=l[i])if(dt[to[i]]>dt[q[h]]+c[i]&&v[i]){
 9         dt[to[i]]=dt[q[h]]+c[i];pre[to[i]]=i;
10         if(!iq[to[i]])q[++t]=to[i],iq[to[i]]=1;
11     }
12     return dt[cn]!=1234567890;
13 }
14 int main(){
15     scanf("%d%d",&n,&m);cn=n;
16     for(int i=1;i<=n;++i)scanf("%d",&x[i]),link(0,i,x[i],0,0),link(i,0,0,0,0),tot+=x[i];
17     for(int i=1;i<=n;++i)for(int j=1;j<=m;++j)scanf("%d",&t[i][j]);
18     for(int j=1;j<=m;++j)for(int k=1;k<=tot;++k)num[j][k]=++cn;
19     cn++;
20     for(int j=1;j<=m;++j)for(int k=1;k<=tot;++k)link(num[j][k],cn,1,0,0),link(cn,num[j][k],0,0,0);
21     for(int i=1;i<=n;++i)for(int j=1;j<=m;++j)for(int k=1;k<=1;++k)
22         link(i,num[j][k],1,t[i][j]*k,1),link(num[j][k],i,0,-t[i][j]*k,1);
23     while(SPFA()){
24         for(int i=pre[cn];i;i=pre[to[i^1]]){
25             v[i]--,v[i^1]++,ans+=c[i];
26             int x=to[i],y=to[i^1],a;
27             if(!tt[i])continue;
28             if(x<y)continue;else x^=y^=x^=y;
29             for(int j=1;j<=m;++j)if(num[j][tt[i]]==y)a=j;
30             if(!al[a][tt[i]+1])for(int j=1;j<=n;++j)link(j,num[a][tt[i]+1],1,t[j][a]*(tt[i]+1),tt[i]+1),link(num[a][tt[i]+1],j,0,-t[j][a]*(tt[i]+1),tt[i]+1);
31             al[a][tt[i]+1]=1;
32         }
33     }
34     printf("%d\n",ans);
35 }
View Code

Guess you like

Origin www.cnblogs.com/hzoi-DeepinC/p/11576880.html