Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #define gc c=getchar()
- #define r(x) read(x)
- #define ll long long
- template<typename T>
- inline void read(T&x){
- T k=1;char gc;x=0;
- while(!isdigit(c)){if(c=='-')k=-1;gc;}
- while(isdigit(c))x=x*10+c-'0',gc;x*=k;
- }
- const int N=500005;
- const ll INF=1e18;
- int ecnt=1;
- int fir[N];
- int nex[N];
- int to[N];
- ll w[N];
- inline void addedge(int u,int v,ll c){
- nex[++ecnt]=fir[u],fir[u]=ecnt,to[ecnt]=v,w[ecnt]=c;
- nex[++ecnt]=fir[v],fir[v]=ecnt,to[ecnt]=u,w[ecnt]=0;
- }
- int tot,S,T;
- int dep[N];
- int cur[N];
- inline bool bfs(){
- memset(dep+1,0,tot<<2);
- memcpy(cur+1,fir+1,tot<<2);
- queue<int>Q;
- Q.push(S);
- dep[S]=1;
- while(!Q.empty()){
- int x=Q.front();Q.pop();
- for(int i=fir[x];i;i=nex[i]){
- if(!w[i])continue;
- int v=to[i];
- if(dep[v])continue;
- dep[v]=dep[x]+1;
- if(v==T)return 1;
- Q.push(v);
- }
- }
- return 0;
- }
- inline ll dfs(int x,ll flow){
- if(!flow||x==T)return flow;
- ll f=0;
- for(int &i=cur[x];i;i=nex[i]){
- int v=to[i];
- if(dep[v]==dep[x]+1){
- ll t=dfs(v,min(flow,w[i]));
- w[i]-=t;
- w[i^1]+=t;
- f+=t;
- flow-=t;
- }
- }
- return f;
- }
- inline ll MaxFlow(){
- ll Ans=0;
- while(bfs())Ans+=dfs(S,INF);
- return Ans;
- }
- ll a[N],b[N];
- int main(){
- freopen("work.in","r",stdin);
- freopen("work.out","w",stdout);
- int n,m,k;r(n),r(m),r(k);tot=n;
- S=++tot;
- T=++tot;
- ll sum=0;
- for(int i=1;i<=n;++i){
- int a;r(a);
- sum+=a;
- addedge(S,i,a);
- }
- for(int i=1;i<=n;++i){
- int a;r(a);
- sum+=a;
- addedge(i,T,a);
- }
- for(int i=1;i<=m;++i){
- int u,v,c;
- r(u),r(v),r(c);
- addedge(u,v,c);
- addedge(v,u,c);
- }
- for(int i=1;i<=k;++i){
- int t,a,b;
- r(t),r(a),r(b);
- sum+=b;
- if(a){
- addedge(++tot,T,b);
- for(int i=1;i<=t;++i){
- int u;r(u);
- addedge(u,tot,INF);
- }
- }
- else{
- addedge(S,++tot,b);
- for(int i=1;i<=t;++i){
- int u;r(u);
- addedge(tot,u,INF);
- }
- }
- }
- printf("%lld\n",sum-MaxFlow());
- }
Advertisement
Add Comment
Please, Sign In to add comment