結果

問題 No.748 yuki国のお財布事情
コンテスト
ユーザー 梧桐
提出日時 2025-12-23 22:57:17
言語 C++14
(gcc 13.3.0 + boost 1.89.0)
結果
AC  
実行時間 56 ms / 2,000 ms
コード長 1,141 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 775 ms
コンパイル使用メモリ 74,880 KB
実行使用メモリ 7,848 KB
最終ジャッジ日時 2025-12-23 22:57:21
合計ジャッジ時間 3,864 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
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) {
      |               ^
main.cpp: In function ‘int main()’:
main.cpp:41:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   41 |     scanf("%d%d%d", &n, &m, &kk);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~
main.cpp:44:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   44 |         scanf("%d%d%d", &vec[i].a, &vec[i].b, &vec[i].c);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:49:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   49 |         scanf("%d", &x);
      |         ~~~~~^~~~~~~~~~

ソースコード

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