結果

問題 No.748 yuki国のお財布事情
ユーザー puni_kyopropuni_kyopro
提出日時 2018-12-02 16:03:37
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 3,736 bytes
コンパイル時間 738 ms
コンパイル使用メモリ 77,040 KB
実行使用メモリ 16,008 KB
最終ジャッジ日時 2023-09-13 20:50:44
合計ジャッジ時間 3,560 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 3 ms
4,500 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 3 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,384 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 3 ms
4,376 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 3 ms
4,376 KB
testcase_23 AC 3 ms
4,376 KB
testcase_24 AC 3 ms
4,376 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<algorithm>
#include<cctype>
#include<utility>
#include<string>
#include<cmath>
#include <numeric>
#include<set>


#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define FORR(i, m, n) for(int i = m;i >= n;i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF 999999999
using namespace std;

typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;
typedef long long int ll;
#define ARRAY_MAX 100005

vector<int> par(ARRAY_MAX);//親
vector<int> ran(ARRAY_MAX);//木の深さ
bool used[ARRAY_MAX];

typedef struct edge{

    ll num;
    ll u;
    ll v;
    ll cost;

}EDGE;

typedef struct where{

    ll num;
    ll x;
    ll y;

}WHERE;


bool comp(const EDGE& e1,const EDGE& e2){
    return e1.cost < e2.cost;
}

//n要素で初期化
void init(ll n){
    for(ll i = 0;i < n;i++){
        par[i] = i;/*各ノードに番号を振る,この時par[x]=xとなる時は木の根となる*/
        ran[i] = 0;/*各ノード自体の高さは0*/
    }
}
//木の根を求める
int find(ll x){
    if(par[x] == x){
        return x;/*根ならそのノードの番号を返す*/
    }else{
        return par[x] = find(par[x]);/*根でないならさらにノードの根を探す*/
    }
}

//xとyの属する集合を併合する
void unite(ll x,ll y){
    x = find(x);//xの根の番号を探す
    y = find(y);//yの根の番号を探す
    if(x == y){//一致すればつながっている
        return;
    }
    if(ran[x] < ran[y]){//xの高さが低いなら高いほうにつなぐ、そして高さは大きい方(y)になる
        par[x] = y;
    }else{
        par[y] = x;//yの高さが低いなら高いほうにつなぐ、そして高さは大きいほう(x)になる
        if(ran[x] == ran[y]){//高さが一致しているなら併合の高さは1増える
            ran[x]++;
        }
    }
}

//xとyが同じ集合に属するか判定
bool same(ll x,ll y){
    return find(x) == find(y);//同じ集合なら1、違うなら0が返る
}

int kruskal(ll n,ll count,vector<EDGE>& es,ll ans){

    sort(es.begin(),es.end(),comp);
    ll res = ans;
    for(ll i = 0;i < count;i++){
        EDGE e = es[i];
        if(used[e.num] == true){
            //cout << e.num << endl;
            continue;
        }
        if(!same(e.u,e.v)){
            unite(e.u,e.v);
            used[e.num] = true;
            res += e.cost;
        }
        //cout << "a " << res << endl;
    }
    return res;
}


/*最小全域木をクラシカル法で実装*/
/*ABC065 AC解法*/

int main(){

    ll n,m,k;
    cin >> n >> m >> k;
    vector<EDGE> es;
    vector<EDGE> es2;
    vector<WHERE> map(n);
    ll count = 0;
    ll a,b,c;
    ll sum = 0;//全経路の合計コスト
    ll use[k];
    ll ans = 0;

    init(n);
    
    for(ll i = 0;i < m;i++){
        cin >> a >> b >> c;
        a--;
        b--;
        sum += c;
        EDGE e;
        e.num = i;
        e.u = a;
        e.v = b;
        e.cost = c;/*昇順ソート済み*/
        es.push_back(e);
        es2.push_back(e);
    }
    //mapの作成

    REP(i,ARRAY_MAX){
        used[i] = false;
    }


    //閉鎖できない経路
    for(ll i = 0;i < k;i++){
        cin >> use[i];
        use[i]--;
        unite(es[use[i]].u,es[use[i]].v);
        ans += es[use[i]].cost;
        used[use[i]] = true;
    }

    ans = kruskal(n,m,es,ans);//最小全域木作成

    //cout << sum << endl;
    //cout << ans << endl;



    cout << sum - ans << endl;



    return 0;
}
0