Advertisement
Guest User

Untitled

a guest
Apr 9th, 2013
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.90 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstdlib>
  4. #include<memory.h>
  5. #include <cmath>
  6. #include <vector>
  7. #include <algorithm>
  8. using namespace std;
  9. typedef long long ll;
  10.  
  11. int ft(int i, int j){
  12.     return (5-j)+(i-1)*4;
  13. }
  14.  
  15.  
  16. const int V = 140+70+2 + 10;
  17. const int E = (V+V+V*4 + 10)*2;
  18. int nv, S, T;
  19. int eb[V], e[E], es[E], ed;
  20. int ef[E], ec[E];
  21. int ex[E], ep[V], d[V];
  22. int fr[V];
  23. bool u[V];
  24.  
  25. void adde(int i, int j, int c, int x, int f=0){
  26.     ++ed;
  27.     e[ed]=j;
  28.     ec[ed]=c;
  29.     ef[ed]=f;
  30.     ex[ed]=x;
  31.     es[ed]=eb[i];
  32.     eb[i]=ed;
  33.    
  34.     //inv
  35.     ++ed;
  36.     e[ed]=i;
  37.     ec[ed]=0;
  38.     ef[ed]=-f;
  39.     ex[ed]=-x;
  40.     es[ed]=eb[j];
  41.     eb[j]=ed;
  42. }
  43.  
  44. pair<int, int> mcmf(){
  45.     int inf = 1e9;
  46.     int i,j,k;
  47.     int cost = 0;
  48.     int flow = 0;
  49.     for(i=0;i<nv;++i) ep[i]=0;
  50.     while(true){
  51.         for(i=0;i<nv;++i) d[i]=inf, u[i]=0, fr[i]=-1;
  52.         d[S]=0;
  53.         for(;;){
  54.             for(i=-1,j=0;j<nv;++j) if(!u[j] && (i<0||d[i]>d[j])) i=j;
  55.             if(i<0||d[i]==inf) break;
  56.             u[i]=1;
  57.             for(k=eb[i];k;k=es[k]) if(ec[k]>ef[k]){
  58.                 j=e[k];
  59.                 if(u[j]) continue;
  60.                 int ee=d[i]+ex[k]+ep[i]-ep[j];
  61.                 if(d[j]>ee) d[j]=ee, fr[j]=k;
  62.             }
  63.         }
  64.         if(d[T]==inf) return make_pair(flow, cost);
  65.         int ff = inf;
  66.         for(i=T; i!=S; i=e[fr[i]^1]) ff=min(ff,ec[fr[i]]-ef[fr[i]]);
  67.         flow+=ff;
  68.         for(i=T; i!=S; i=e[fr[i]^1]) ef[fr[i]]+=ff, ef[fr[i]^1]-=ff, cost+=ff*ex[fr[i]];
  69.         for(i=0;i<nv;++i) ep[i]=d[i];
  70.     }
  71. }
  72.  
  73. int pc[V];
  74.  
  75. int main(){
  76.     int i,j,k;
  77.     for(;;){
  78.         int n,m;
  79.         scanf("%d%d",&n,&m);
  80.         if(n==0 && m==0) break;
  81.        
  82.         nv = n+m+2;
  83.         S = 0;
  84.         T = nv-1;
  85.         for(i=0;i<nv;++i) eb[i]=0;
  86.         ed=1;
  87.  
  88.  
  89.         for(i=0;i<n;++i){
  90.             scanf("%d",&pc[i]);
  91.             adde(m+i+1, T, pc[i], 0);
  92.         }
  93.  
  94.         for(i=0;i<m;++i){
  95.             scanf("%d",&k);
  96.  
  97.             adde(S,i+1,1,0);
  98.             for(j=0;j<4;++j){
  99.                 int w;
  100.                 scanf("%d",&w);
  101.                 ++w;
  102.                 adde(i+1,m+w,1,-ft(k,j+1));
  103.             }
  104.         }
  105.  
  106.         pair<int,int> res = mcmf();
  107.         int res1 = -res.second;
  108.        
  109.         cout<<res1<<endl;
  110.     }
  111.  
  112.     return 0;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement