結果

問題 No.748 yuki国のお財布事情
コンテスト
ユーザー vjudge1
提出日時 2025-12-26 22:29:31
言語 C++17
(gcc 15.2.0 + boost 1.89.0)
結果
WA  
実行時間 -
コード長 2,076 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,877 ms
コンパイル使用メモリ 201,136 KB
実行使用メモリ 7,852 KB
最終ジャッジ日時 2025-12-26 22:29:36
合計ジャッジ時間 3,635 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1 WA * 2
other AC * 4 WA * 22
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

struct Edge {
    int u, v, w;
    bool operator<(const Edge& R) const {
        return w < R.w;
    }
};

const int MAXN = 100010;
int n, m, k;
Edge e[MAXN];
int fa[MAXN];

void init(int n) {
    for (int i = 1; i <= n; ++i) {
        fa[i] = i;
    }
}

int find(int x) {
    if (fa[x] == x) return x;
    return fa[x] = find(fa[x]);
}

bool unite(int u, int v) {
    u = find(u);
    v = find(v);
    if (u == v) return false;
    fa[u] = v;
    return true;
}

long long kur() {
    sort(e + 1, e + m + 1); // ?1????
    init(n);
    
    // ????????K??
    int edgeCount = 0;
    long long mstWeight = 0;
    
    // ??????????????
    for (int i = 1; i <= m; ++i) {
        if (e[i].w == 0) { // ????????????0?
            if (unite(e[i].u, e[i].v)) {
                edgeCount++;
                // ?????sumw????????????
            }
        }
    }
    
    // ??????????????
    for (int i = 1; i <= m; ++i) {
        if (e[i].w != 0) { // ????????
            if (unite(e[i].u, e[i].v)) {
                mstWeight += e[i].w;
                edgeCount++;
            }
        }
    }
    
    // ?????????
    return (edgeCount == n - 1) ? mstWeight : -1;
}

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    
    cin >> n >> m >> k;
    
    // ?????
    for (int i = 1; i <= m; ++i) {
        cin >> e[i].u >> e[i].v >> e[i].w;
    }
    
    // ????????
    vector<bool> required(m + 1, false);
    for (int i = 1; i <= k; ++i) {
        int x;
        cin >> x;
        required[x] = true;
    }
    
    // ??????????????????0
    long long totalWeight = 0;
    for (int i = 1; i <= m; ++i) {
        totalWeight += e[i].w;
        if (required[i]) {
            e[i].w = 0; // ???????????0
        }
    }
    
    long long mstWeight = kur();
    
    if (mstWeight == -1) {
        // ?????????????????
        cout << 0 << endl;
    } else {
        // ????????? = ??? - ???????
        cout << totalWeight - mstWeight << endl;
    }
    
    return 0;
}
0