結果

問題 No.748 yuki国のお財布事情
コンテスト
ユーザー WutongDeath
提出日時 2025-12-21 23:59:10
言語 C++17
(gcc 13.3.0 + boost 1.89.0)
結果
AC  
実行時間 46 ms / 2,000 ms
コード長 1,216 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 842 ms
コンパイル使用メモリ 81,568 KB
実行使用メモリ 7,848 KB
最終ジャッジ日時 2025-12-21 23:59:13
合計ジャッジ時間 2,758 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 26
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:26:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   26 |     scanf("%d%d%d", &n, &m, &k);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~
main.cpp:30:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   30 |         scanf("%d%d%d", &edges[i].u, &edges[i].v, &edges[i].w);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:36:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   36 |         scanf("%d", &x); --x;
      |         ~~~~~^~~~~~~~~~

ソースコード

diff #
raw source code

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

typedef long long LL;

const int N = 100010;

struct Edge {
    int u, v, w;
    bool is_del;
};

int n, m, k, fa[N];
vector<Edge> edges;
LL sum, v;

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

int main() {
    // freopen("cost.in", "r", stdin);
    // freopen("cost.out", "w", stdout);

    scanf("%d%d%d", &n, &m, &k);
    edges.resize(m);
    sum = 0LL;
    for (int i = 0; i < m; ++i) {
        scanf("%d%d%d", &edges[i].u, &edges[i].v, &edges[i].w);
        sum += edges[i].w;
    }
    for (int i = 1; i <= n; ++i) fa[i] = i;
    v = 0LL;
    for (int i = 0, x, fu, fv; i < k; ++i) {
        scanf("%d", &x); --x;
        v += edges[x].w;
        edges[x].is_del = true;
        fu = Find(edges[x].u), fv = Find(edges[x].v);
        if (fu != fv) fa[fu] = fv;
    }
    sort(edges.begin(), edges.end(), [](Edge e1, Edge e2) {
        return e1.w < e2.w;
    });
    for (Edge e : edges) {
        if (e.is_del) continue;
        int fu = Find(e.u), fv = Find(e.v);
        if (fu != fv) {
            fa[fu] = fv;
            v += e.w;
        }
    }
    printf("%lld\n", sum - v);

    return 0;
}
0