yicongli

BZ4177

Apr 3rd, 2019
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.86 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define gc c=getchar()
  6. #define r(x) read(x)
  7. #define ll long long
  8.  
  9. template<typename T>
  10. inline void read(T&x){
  11.     T k=1;char gc;x=0;
  12.     while(!isdigit(c)){if(c=='-')k=-1;gc;}
  13.     while(isdigit(c))x=x*10+c-'0',gc;x*=k;
  14. }
  15.  
  16. const int N=500005;
  17. const ll INF=1e18;
  18.  
  19. int ecnt=1;
  20. int fir[N];
  21. int nex[N];
  22. int to[N];
  23. ll w[N];
  24.  
  25. inline void addedge(int u,int v,ll c){
  26.     nex[++ecnt]=fir[u],fir[u]=ecnt,to[ecnt]=v,w[ecnt]=c;
  27.     nex[++ecnt]=fir[v],fir[v]=ecnt,to[ecnt]=u,w[ecnt]=0;
  28. }
  29.  
  30. int tot,S,T;
  31. int dep[N];
  32. int cur[N];
  33.  
  34. inline bool bfs(){
  35.     memset(dep+1,0,tot<<2);
  36.     memcpy(cur+1,fir+1,tot<<2);
  37.     queue<int>Q;
  38.     Q.push(S);
  39.     dep[S]=1;
  40.     while(!Q.empty()){
  41.         int x=Q.front();Q.pop();
  42.         for(int i=fir[x];i;i=nex[i]){
  43.             if(!w[i])continue;
  44.             int v=to[i];
  45.             if(dep[v])continue;
  46.             dep[v]=dep[x]+1;
  47.             if(v==T)return 1;
  48.             Q.push(v);
  49.         }
  50.     }
  51.  
  52.     return 0;
  53. }
  54.  
  55. inline ll dfs(int x,ll flow){
  56.     if(!flow||x==T)return flow;
  57.     ll f=0;
  58.     for(int &i=cur[x];i;i=nex[i]){
  59.         int v=to[i];
  60.         if(dep[v]==dep[x]+1){
  61.             ll t=dfs(v,min(flow,w[i]));
  62.             w[i]-=t;
  63.             w[i^1]+=t;
  64.             f+=t;
  65.             flow-=t;
  66.         }
  67.     }
  68.     return f;
  69. }
  70.  
  71. inline ll MaxFlow(){
  72.     ll Ans=0;
  73.     while(bfs())Ans+=dfs(S,INF);
  74.     return Ans;
  75. }
  76.  
  77. ll a[N],b[N];
  78.  
  79. int main(){
  80.     freopen("work.in","r",stdin);
  81.     freopen("work.out","w",stdout);
  82.     int n,m,k;r(n),r(m),r(k);tot=n;
  83.     S=++tot;
  84.     T=++tot;
  85.     ll sum=0;
  86.     for(int i=1;i<=n;++i){
  87.         int a;r(a);
  88.         sum+=a;
  89.         addedge(S,i,a);
  90.     }
  91.     for(int i=1;i<=n;++i){
  92.         int a;r(a);
  93.         sum+=a;
  94.         addedge(i,T,a);
  95.     }
  96.     for(int i=1;i<=m;++i){
  97.         int u,v,c;
  98.         r(u),r(v),r(c);
  99.         addedge(u,v,c);
  100.         addedge(v,u,c);
  101.     }
  102.     for(int i=1;i<=k;++i){
  103.         int t,a,b;
  104.         r(t),r(a),r(b);
  105.         sum+=b;
  106.         if(a){
  107.             addedge(++tot,T,b);
  108.             for(int i=1;i<=t;++i){
  109.                 int u;r(u);
  110.                 addedge(u,tot,INF);
  111.             }
  112.         }
  113.         else{
  114.             addedge(S,++tot,b);
  115.             for(int i=1;i<=t;++i){
  116.                 int u;r(u);
  117.                 addedge(tot,u,INF);
  118.             }
  119.         }
  120.     }
  121.     printf("%lld\n",sum-MaxFlow());
  122. }
Advertisement
Add Comment
Please, Sign In to add comment