結果

問題 No.748 yuki国のお財布事情
ユーザー roarisroaris
提出日時 2019-11-21 19:41:42
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,942 bytes
コンパイル時間 1,809 ms
コンパイル使用メモリ 178,312 KB
実行使用メモリ 7,080 KB
最終ジャッジ日時 2023-08-01 06:27:17
合計ジャッジ時間 3,933 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 WA -
testcase_26 AC 43 ms
7,080 KB
testcase_27 WA -
testcase_28 AC 27 ms
4,736 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: 関数 ‘int main()’ 内:
main.cpp:84:16: 警告: structured bindings only available with ‘-std=c++17’ or ‘-std=gnu++17’ [-Wc++17-extensions]
   84 |     for (auto& [ai, bi, ci] : edges) {
      |                ^

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef tuple<int, int, int> T;

struct Unionfind {
    vector<int> par, rank;
    
    Unionfind(int n) {
        par.resize(n);
        fill(par.begin(), par.end(), -1);
        rank.resize(n);
        fill(rank.begin(), rank.end(), 0);
    }
    
    int root(int x) {
        if (par[x]<0) return x;
        par[x] = root(par[x]);
        return par[x];
    }
    
    void unite(int x, int y) {
        int rx = root(x), ry = root(y);
        if (rx==ry) return;
        
        if (rank[rx]>=rank[ry]) {
            par[rx] += par[ry];
            par[ry] = rx;
            
            if (rank[rx]==rank[ry]) {
                rank[rx]++;
            }
        }
        else {
            par[ry] += par[rx];
            par[rx] = ry;
        }
    }
    
    int is_same(int x, int y) {
        return root(x)==root(y);
    }
    
    int count(int x) {
        return -par[root(x)];
    }
};

int main() {
    cin.tie(0); ios::sync_with_stdio(false);
    
    int N, M, K; cin >> N >> M >> K;
    int a[M], b[M], c[M];
    int cost_sum = 0;
    
    for (int i=0; i<M; i++) {
        cin >> a[i] >> b[i] >> c[i];
        cost_sum += c[i];
    }
    
    int flag[M];
    fill(flag, flag+M, 0);
    
    for (int i=0; i<K; i++) {
        int ei; cin >> ei;
        flag[ei-1] = 1;
    }
    
    vector<T> edges;
    Unionfind uf = Unionfind(N);
    int cost = 0;
    
    for (int i=0; i<M; i++) {
        if (flag[i]) {
            uf.unite(a[i]-1, b[i]-1);
            cost += c[i];
        }
        else {
            edges.push_back(make_tuple(a[i]-1, b[i]-1, c[i]));
        }
    }
    
    sort(edges.begin(), edges.end(), [](T const t1, T const t2) {return get<2>(t1)<get<2>(t2);});
    
    for (auto& [ai, bi, ci] : edges) {
        if (!(uf.is_same(ai, bi))) {
            uf.unite(ai, bi);
            cost += ci;
        }
    }
    
    cout << cost_sum-cost << endl;
}
0