sdut2414 An interesting game fee flow

map title


Because the height of each newly added hill is between (0-30), we take these 31 points, that is, the height as the point

S->S' capacity is k, which is used to indicate that at most k can be added

S' points to 31 hills, and the capacity is the number of hills per height.

31 hills are pointing to n big hills. The capacity is 1, and the cost is more troublesome. When there was no such hill,

We are abs(a[j]-a[j-1]). Our calculation is redundant, so subtract this piece and add the value after adding the hill.

That is -(abs(ia[j])+abs(ia[j-1])-abs(a[j]-a[j-1])).

After that, n mountains are pointing to the end point, the flow is 1, and each mountain can only add one small mountain.



Note that the negative numbers are used to find the minimum value. Then add the difference output of the first n mountains.


  1.  int s=N+31,mid=N+32,t=N+33;  
  2.         g.init(N+34);  
  3.         for(int i=1;i<=N;i++){  
  4.             scanf("%d",&a[i]);  
  5.             if(i>1) ans+=abs(a[i]-a[i-1]);  
  6.         }  
  7.         memset(cnt,0,sizeof(cnt));  
  8.         int tmp;  
  9.         for(int i=1;i<=M;i++){  
  10.             scanf("%d",&tmp);  
  11.             cnt[tmp]++;  
  12.         }  
  13.         g.add_edge(s,mid,K,0);  
  14.         for(int i=0;i<=30;i++) if(cnt[i]){  
  15.             g.add_edge(mid,i,cnt[i],0);  
  16.             for(int j=2;j<=N;j++) g.add_edge(i,30+j,1,-(abs(i-a[j])+abs(i-a[j-1])-abs(a[j]-a[j-1])));  
  17.         }  
  18.         for(int i=2;i<=N;i++) g.add_edge(30+i,t,1,0); 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324861621&siteId=291194637