結果
問題 |
No.3201 Corporate Synergy
|
ユーザー |
|
提出日時 | 2025-07-11 22:17:39 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 2 ms / 2,000 ms |
コード長 | 3,159 bytes |
コンパイル時間 | 2,431 ms |
コンパイル使用メモリ | 213,736 KB |
実行使用メモリ | 6,272 KB |
最終ジャッジ日時 | 2025-07-11 22:17:43 |
合計ジャッジ時間 | 3,605 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 20 |
ソースコード
#include <bits/stdc++.h> using namespace std; long long inf = 2e18; struct edge{ int to; long long cap; int rev; bool isrev;}; class Dinic{ public: int siz = 0; vector<vector<edge>> Graph; vector<int> searched,dist; vector<bool> visited; void make(int N){ siz = N; Graph.resize(N); visited.resize(N); dist.resize(N,-1); } void addedge(int u, int v, long long c){ int gv = Graph.at(v).size(), gu = Graph.at(u).size(); Graph.at(u).push_back({v,c,gv,false}); Graph.at(v).push_back({u,0,gu,true}); } void bfs(int s, int t){ queue<int> Q; Q.push(s); dist.at(s) = 0; while(Q.size()){ int pos = Q.front(); Q.pop(); for(auto &[to,c,r,ign] : Graph.at(pos)){ if(c > 0 && dist.at(to) == -1){ dist.at(to) = dist.at(pos)+1; if(to != t) Q.push(to); } } } } long long dfs(int pos, int start, long long f){ if(pos == start) return f; visited.at(pos) = true; long long flow = 0; for(auto &[t,c,r,ign] : Graph.at(pos)){ long long &revc = Graph.at(t).at(r).cap; if(visited.at(t) || revc == 0 || f-flow == 0 || dist.at(pos) <= dist.at(t)) continue; long long now = dfs(t,start,min(revc,f-flow)); c += now; revc -= now; flow += now; } return flow; } long long maxflow(int s, int t){ long long ret = 0; while(true){ visited.assign(siz,false); dist.assign(siz,-1); bfs(s,t); if(dist.at(t) == -1) break; ret += dfs(t,s,inf); } return ret; } vector<pair<int,int>> mincost(int s){ //maxflowの後の復元 vector<int> distS(siz,-1); distS.at(s) = 1; queue<int> Q; Q.push(s); while(Q.size()){ int pos = Q.front(); Q.pop(); for(auto &[to,c,r,ign] : Graph.at(pos)){ if(c == 0 || distS.at(to) != -1) continue; distS.at(to) = 1; Q.push(to); } } vector<pair<int,int>> ret; for(int i=0; i<siz; i++){ for(auto &[to,c,r,isr] : Graph.at(i)){ if(isr) continue; if(distS.at(i) == 1 && distS.at(to) == -1) ret.push_back({i,to}); } } return ret; } }; int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int N; cin >> N; int n = N+3,s = N,t = N+1,V = N+2; Dinic Z; Z.make(n+300); long long answer = 0; for(int i=0; i<N; i++){ int p; cin >> p; if(p >= 0) answer += p,Z.addedge(i,t,p); else p = -p,Z.addedge(s,i,p); } int M; cin >> M; while(M--){ int u,v; cin >> u >> v; u--; v--; Z.addedge(u,v,inf); } cin >> M; while(M--){ int u,v,k; cin >> u >> v >> k; u--,v--; answer += k; Z.addedge(u,V,inf),Z.addedge(v,V,inf),Z.addedge(V,t,k); V++; } cout << answer-Z.maxflow(s,t) << endl; }