1 solutions
-
0
/* * __----~~~~~~~~~~~------___ * . . ~~//====...... __--~ ~~ * -. \_|// |||\\ ~~~~~~::::... /~ * ___-==_ _-~o~ \/ ||| \\ _/~~- * __---~~~.==~||\=_ -_--~/_-~|- |\\ \\ _/~ * _-~~ .=~ | \\-_ '-~7 /- / || \ / * .~ .~ | \\ -_ / /- / || \ / * / ____ / | \\ ~-_/ /|- _/ .|| \ / * |~~ ~~|--~~~~--_ \ ~==-/ | \~--===~~ .\ * ' ~-| /| |-~\~~ __--~~ * |-~~-_/ | | ~\_ _-~ /\ * / \ \__ \/~ \__ * _--~ _/ | .-~~____--~-/ ~~==. * ((->/~ '.|||' -_| ~~-/ , . _|| * -_ ~\ ~~---l__i__i__i--~~_/ * _-~-__ ~) \--______________--~~ * //.-~~~-~_--~- |-------~~~~~~~~ * //.-~~~--\ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * 神兽保佑 永无BUG */ /* * @Description: I want to be the weakest vegetable in the world! * @Author: I.Z. */ #include<bits/stdc++.h> using namespace std; #define MOD 998244353 #define speMOD 2933256077ll #define int long long #define pii pair<int,int> #define all(v) v.begin(),v.end() #define pb push_back #define REP(i,b,e) for(int i=(b);i<(int)(e);++i) #define over(x) {cout<<(x)<<endl;return;} #define lowbit(x) ((x)&(-(x))) #define cntbit(x) __builtin_popcount(x) #define deal(v) sort(all(v));v.erase(unique(v.begin(),v.end()),v.end()) #define lbound(v,x) lower_bound(all(v),x)-v.begin() mt19937 sd(random_device{}()); int qpow(int a,int b,int m=MOD,int res=1){ a%=m; while(b>0)res=(b&1)?(res*a%m):(res),a=a*a%m,b>>=1; return res; } int exgcd(int x,int y,int &a,int &b){ if(y==0){ a=1;b=0; return x; } int d=exgcd(y,x%y,a,b); int z=b; b=a-b*(x/y); a=z; return d; } int _x_,_y_; inline int inverse(int x,int y=MOD){ exgcd(x,y,_x_,_y_); return (_x_+y)%y; } int fac[2000005],inv[2000005]; void init(int n){ fac[0]=inv[0]=1; REP(i,1,n+1)fac[i]=fac[i-1]*i%MOD; inv[n]=qpow(fac[n],MOD-2); for(int i=n-1;i>=1;--i)inv[i]=inv[i+1]*(i+1)%MOD; } int binom(int x,int y){ return x<y||y<0? 0:fac[x]*inv[y]%MOD*inv[x-y]%MOD; } struct flow{ struct edge{ int y,fl,c; }a[4000005]; vector<int>v[1000005]; int tot,n; void addedge(int x,int y,int w,int c){ v[x].pb(tot);a[tot++]={y,w,c}; v[y].pb(tot);a[tot++]={x,0,-c}; } int dis[1000005],vis[1000005],E[1000005]; bool spfa(){ queue<int>q; REP(i,0,n)E[i]=0,vis[i]=0,dis[i]=1e18; dis[0]=0;q.push(0);vis[0]=1; while(!q.empty()){ int x=q.front();vis[x]=0;q.pop(); for(auto i:v[x]){ auto [y,fl,c]=a[i]; if(!fl)continue; if(dis[y]>dis[x]+c){ dis[y]=dis[x]+c; if(!vis[y])vis[y]=1,q.push(y); } } } REP(i,0,n)vis[i]=0; return dis[1]!=1e18; } int cost; int dfs(int x,int fl){ if(x==1||!fl)return fl; int ans=0;vis[x]=1; for(int &_=E[x];_<(int)v[x].size();++_){ int i=v[x][_]; auto [y,f,c]=a[i]; if(!vis[y]&&dis[y]==dis[x]+c&&f){ int d=dfs(y,min(fl-ans,f)); ans+=d;a[i].fl-=d;a[i^1].fl+=d;cost+=d*c; if(ans==fl)return vis[x]=0,fl; } } return vis[x]=0,ans; } int dinic(){ int ans=0;cost=0; while(spfa())ans+=dfs(0,1e18); return ans; } void init(int _n){ n=_n;tot=0; REP(i,0,n)v[i].clear(); } }f; int T=10; int n,m,k; int a[1000005]; vector<tuple<int,int,int>>e; uniform_int_distribution<int>rd(0,1); void Main() { cin>>n>>m>>k; REP(i,0,n)cin>>a[i]; e.clear(); REP(i,0,m){ int x,y,z; cin>>x>>y>>z; --x,--y; e.pb({x,y,z}); } int ans=0; REP(_,0,(T<<k)){ vector<int>op(n,0); for(auto &i:op)i=rd(sd); f.init(n+3); REP(i,0,n){ if(!op[i]){ f.addedge(n+2,i+2,1,-a[i]); f.addedge(n+2,i+2,1e18,0); }else{ f.addedge(i+2,1,1,-a[i]); f.addedge(i+2,1,1e18,0); } } for(auto [i,j,l]:e)if(op[i]!=op[j]){ if(!op[i]){ f.addedge(i+2,j+2,1,l); }else{ f.addedge(j+2,i+2,1,l); } } f.addedge(0,n+2,k,0); f.dinic(); ans=max(ans,-f.cost); } over(ans) } void TC() { int tc=1; cin>>tc; while(tc--){ Main(); cout.flush(); } } signed main() { freopen("storm.in","r",stdin); freopen("storm.out","w",stdout); return cin.tie(0),cout.tie(0),ios::sync_with_stdio(0),TC(),0; } /* 1. CLEAR the arrays (ESPECIALLY multitests) 2. DELETE useless output */
- 1
Information
- ID
- 3062
- Time
- 4000ms
- Memory
- 512MiB
- Difficulty
- 10
- Tags
- (None)
- # Submissions
- 1
- Accepted
- 1
- Uploaded By