結果

問題 No.2604 Initial Motion
ユーザー aaaaaaaaaa2230aaaaaaaaaa2230
提出日時 2024-01-12 23:00:33
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,779 ms / 3,000 ms
コード長 1,960 bytes
コンパイル時間 3,495 ms
コンパイル使用メモリ 282,424 KB
実行使用メモリ 71,632 KB
最終ジャッジ日時 2024-09-27 23:47:27
合計ジャッジ時間 20,396 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 39
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

#include <atcoder/mincostflow>
using namespace atcoder;

const long long INF = 1e18;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
    int n,k,m;
    cin >> k >> n >> m;

    vector<int> A(k);
    vector<int> B(n);

    for (auto &a: A) cin >> a;
    for (auto &b: B) cin >> b;

    vector<vector<pair<int, long long>>> e(n);
    for (int i = 0; i < m ; i++){
        int u,v;
        long long d;
        cin >> u >> v >> d;
        u--;v--;
        e[u].emplace_back(v,d);
        e[v].emplace_back(u,d);
    }

    int s = n+n, t = s+1;
    mcf_graph<int, long long> g(t+1);

    vector<int> count(n);
    for (int i = 0; i < k; i++){
        count[A[i]-1]++;
    }
    for (int i = 0; i < n; i++){
        if (B[i]){
            int mi = min(B[i],count[i]);
            B[i] -= mi;
            count[i] -= mi;
        }
    }

    for (int i = 0; i < n; i++){

        if (count[i]==0) continue;
        
        vector<long long> dist(n,INF);
        dist[i] = 0;

        priority_queue<pair<long long, int>,vector<pair<long long, int>>,greater<pair<long long ,int>>> h;
        h.push(pair(0,i));

        while (!h.empty())
        {
            auto [d,now] = h.top(); h.pop();
            if (dist[now] != d) continue;

            for (auto [nex,nd]: e[now]){
                if (dist[nex] > d+nd){
                    dist[nex] = d+nd;
                    h.push(pair(d+nd,nex));
                }
            }
        }

        for (int j = 0; j < n; j++){
            if (B[j] == 0) continue;

            g.add_edge(i,n+j,count[i],dist[j]);
        }
   
    }
    int flow = 0;
    for (int i = 0; i < n; i++){
        if (B[i]){
            g.add_edge(n+i,t,B[i],0);
        }
        if (count[i]){
            g.add_edge(s,i,count[i],0);
            flow += count[i];
        }
    }

    auto result = g.flow(s,t,flow);
    cout << result.second << endl;

    return 0;

}
0