結果

問題 No.3201 Corporate Synergy
ユーザー houren
提出日時 2025-07-11 22:46:44
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,515 bytes
コンパイル時間 3,900 ms
コンパイル使用メモリ 299,492 KB
実行使用メモリ 7,848 KB
最終ジャッジ日時 2025-07-11 22:46:59
合計ジャッジ時間 4,712 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#include <atcoder/maxflow>
#include <atcoder/scc>
using namespace atcoder;
using ll = long long;
#define fix(x) fixed << setprecision(x)
#define rep(i, n) for(int i = 0; i < n; ++i)
#define all(x) (x).begin(),(x).end()
template<class T>bool chmin(T&a, const T&b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool chmax(T&a, const T&b){if(a<b){a=b;return 1;}return 0;}
constexpr ll INFLL = (300LL << 30), MOD = 998244353;
constexpr int INF = (1 << 30);

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n,m,k;
    cin >> n;
    vector<ll> p(n);
    rep(i,n) cin >> p[i];
    scc_graph scc(n);
    cin >> m;
    vector<int> u(m), v(m);
    rep(i,m){
        cin >> u[i] >> v[i];
        --u[i], --v[i];
        scc.add_edge(u[i], v[i]);
    }
    auto sc = scc.scc();
    vector<int> c(n,-1);
    int z = sc.size();
    rep(i,z){
        for(int x:sc[i]) c[x] = i;
    }
    mf_graph<ll> g(z+2);
    rep(i,m){
        if(c[u[i]]==c[v[i]]) continue;
        g.add_edge(c[v[i]], c[u[i]], INFLL);
    }
    cin >> k;
    rep(i,k){
        int a,b,s;
        cin >> a >> b >> s;
        --a, --b;
        p[a] += s;
        if(c[a]!=c[b]) g.add_edge(c[a], c[b], s);
    }
    vector<ll> tot(z,0);
    rep(i,n) tot[c[i]] += p[i];
    ll ans = 0;
    rep(i,z){
        ans += max(0LL, tot[i]);
        g.add_edge(z, i, max(0LL, tot[i]));
        g.add_edge(i, z+1, -min(0LL, tot[i]));
    }
    cout << ans-g.flow(z,z+1) << '\n';
    return 0;
}
0