結果

問題 No.748 yuki国のお財布事情
ユーザー math957963math957963
提出日時 2018-10-19 21:58:06
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 51 ms / 2,000 ms
コード長 1,484 bytes
コンパイル時間 842 ms
コンパイル使用メモリ 67,540 KB
実行使用メモリ 9,688 KB
最終ジャッジ日時 2024-04-29 15:44:55
合計ジャッジ時間 2,335 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
7,808 KB
testcase_01 AC 5 ms
7,936 KB
testcase_02 AC 5 ms
7,808 KB
testcase_03 AC 5 ms
7,936 KB
testcase_04 AC 5 ms
7,808 KB
testcase_05 AC 5 ms
7,808 KB
testcase_06 AC 5 ms
7,936 KB
testcase_07 AC 5 ms
7,936 KB
testcase_08 AC 5 ms
7,936 KB
testcase_09 AC 5 ms
7,808 KB
testcase_10 AC 5 ms
7,808 KB
testcase_11 AC 5 ms
7,808 KB
testcase_12 AC 5 ms
7,936 KB
testcase_13 AC 11 ms
8,064 KB
testcase_14 AC 14 ms
8,144 KB
testcase_15 AC 12 ms
8,144 KB
testcase_16 AC 22 ms
8,016 KB
testcase_17 AC 45 ms
9,688 KB
testcase_18 AC 48 ms
8,660 KB
testcase_19 AC 50 ms
8,276 KB
testcase_20 AC 44 ms
8,192 KB
testcase_21 AC 47 ms
8,788 KB
testcase_22 AC 5 ms
7,808 KB
testcase_23 AC 5 ms
7,808 KB
testcase_24 AC 5 ms
7,808 KB
testcase_25 AC 44 ms
9,688 KB
testcase_26 AC 51 ms
9,688 KB
testcase_27 AC 50 ms
9,684 KB
testcase_28 AC 33 ms
7,808 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:68:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   68 |         scanf("%d %d %d", &n, &m, &k);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:70:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   70 |                 scanf("%d %d %lld", &a[i], &b[i], &c[i]);
      |                 ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:76:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   76 |                 scanf("%d", &x);
      |                 ~~~~~^~~~~~~~~~

ソースコード

diff #

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<functional>
#include<vector>
#include<queue>
#include<stack>
#include<set>
using namespace std;
#define MOD 1000000007
#define f(i,n) for(int i=0;i<int(n);i++)
#define N 200000

int p[N];
int d[N];

void init(void){
	f(i, N){
		p[i] = i;
		d[i] = 1;
	}
	return;
}

int pa(int x){
	if (p[x] == x)return x;
	else return p[x] = pa(p[x]);
}


void uni(int x, int y){
	int px = pa(x);
	int py = pa(y);
	if (px == py)return;
	if (d[px] < d[py]){
		p[px] = py;
		d[py] = max(d[py], d[px] + 1);
	}
	else{
		p[py] = px;
		d[px] = max(d[px], d[py] + 1);
	}
	return;
}

bool jud(int x, int y){
	int px = pa(x);
	int py = pa(y);
	if (px == py)return true;
	else return false;
}


int main(){
	int n,m,k;
	int x, y, z;
	int a[N];
	int b[N];
	long long c[N];
	bool used[N];
	long long s, ans;
	bool v = true;
	vector < pair<long long, pair<int, int> > >pe;
	ans = 0;
	init();
	scanf("%d %d %d", &n, &m, &k);
	f(i, m){
		scanf("%d %d %lld", &a[i], &b[i], &c[i]);
		a[i]--;
		b[i]--;
		used[i] = false;
	}
	f(i, k){
		scanf("%d", &x);
		x--;
		used[x] = true;
		uni(a[x], b[x]);
	}
	f(i, m){
		if (!used[i]){
			pe.push_back(make_pair(c[i], make_pair(a[i], b[i])));
		}
	}
	sort(pe.begin(), pe.end());
	f(i, pe.size()){
		if (jud(pe[i].second.first, pe[i].second.second)){
			ans += pe[i].first;
		}
		else{
			uni(pe[i].second.first, pe[i].second.second);
		}
	}
	printf("%lld\n", ans);


	return 0;
}
0