結果

問題 No.748 yuki国のお財布事情
コンテスト
ユーザー 梧桐
提出日時 2025-12-23 22:57:17
言語 C++14
(gcc 15.2.0 + boost 1.90.0)
コンパイル:
g++-15 -O2 -lm -std=c++14 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 34 ms / 2,000 ms
+ 449µs
コード長 1,141 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 452 ms
コンパイル使用メモリ 95,788 KB
実行使用メモリ 5,888 KB
最終ジャッジ日時 2026-07-19 18:48:40
合計ジャッジ時間 3,179 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 26
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'void Kruskal()':
main.cpp:28:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
   28 |     for (auto [a, b, c] : vec) {
      |               ^

ソースコード

diff #
raw source code

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

using namespace std;

typedef long long LL;

const int N = 100010, M = 2 * N;

struct Line {
    int a, b, c;
};

LL ans, tot;
vector<Line> vec;
int n, m, kk, fa[N];

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

void Kruskal() {
    sort(vec.begin(), vec.end(), [](Line l1, Line l2) {
        return l1.c < l2.c;
    });
    for (auto [a, b, c] : vec) {
        int fx = Find(a), fy = Find(b);
        if (fx != fy) {
            fa[fx] = fy;
            ans += c;
        }
    }
}

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

    scanf("%d%d%d", &n, &m, &kk);
    vec.resize(m);
    for (int i = 0; i < m; ++i) {
        scanf("%d%d%d", &vec[i].a, &vec[i].b, &vec[i].c);
        tot += vec[i].c;
    }
    for (int i = 1; i <= n; ++i) fa[i] = i;
    for (int i = 1, x; i <= kk; ++i) {
        scanf("%d", &x);
        --x;
        int fx = Find(vec[x].a), fy = Find(vec[x].b);
        fa[fx] = fy;
        ans += vec[x].c;
    }

    Kruskal();

    printf("%lld\n", tot - ans);

    return 0;
}
0