結果

問題 No.748 yuki国のお財布事情
ユーザー bal4ubal4u
提出日時 2019-08-19 21:32:02
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 35 ms / 2,000 ms
コード長 1,723 bytes
コンパイル時間 331 ms
コンパイル使用メモリ 32,512 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-15 10:25:03
合計ジャッジ時間 2,057 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 1 ms
6,944 KB
testcase_10 AC 1 ms
6,944 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 1 ms
6,940 KB
testcase_13 AC 5 ms
6,944 KB
testcase_14 AC 6 ms
6,944 KB
testcase_15 AC 6 ms
6,944 KB
testcase_16 AC 9 ms
6,944 KB
testcase_17 AC 28 ms
6,944 KB
testcase_18 AC 25 ms
6,944 KB
testcase_19 AC 17 ms
6,944 KB
testcase_20 AC 14 ms
6,944 KB
testcase_21 AC 26 ms
6,940 KB
testcase_22 AC 1 ms
6,940 KB
testcase_23 AC 1 ms
6,940 KB
testcase_24 AC 1 ms
6,940 KB
testcase_25 AC 32 ms
6,940 KB
testcase_26 AC 35 ms
6,944 KB
testcase_27 AC 34 ms
6,940 KB
testcase_28 AC 10 ms
6,940 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: In function 'in':
main.c:11:14: warning: implicit declaration of function 'getchar_unlocked' [-Wimplicit-function-declaration]
   11 | #define gc() getchar_unlocked()
      |              ^~~~~~~~~~~~~~~~
main.c:17:24: note: in expansion of macro 'gc'
   17 |         int n = 0, c = gc();
      |                        ^~

ソースコード

diff #

// yukicoder: No.748 yuki国のお財布事情
// bal4u 2019.8.19

#include <stdio.h>
#include <stdlib.h>

typedef long long ll;

//// 入出力関係
#if 1
#define gc() getchar_unlocked()
#else
#define gc() getchar()
#endif

int in() { // 非負整数の入力
	int n = 0, c = gc();
	do n = 10 * n + (c & 0xf); while ((c = gc()) >= '0');
	return n;
}

#define MAX 100005

/* UNION-FIND library */
int id[MAX], size[MAX];
void init(int n) { int i; for (i = 0; i < n; i++) id[i] = i, size[i] = 1; }
int root(int i) { while (i != id[i]) id[i] = id[id[i]], i = id[i]; return i; }
int connected(int p, int q) { return root(p) == root(q); }
void unite(int p, int q) {
    int i = root(p), j = root(q); if (i == j) return;
    if (size[i] < size[j]) id[i] = j, size[j] += size[i]; else id[j] = i, size[i] += size[j];
}

typedef struct { int a, b, c; } EDGE; // 道路の両端町、維持費
EDGE edge[MAX]; int sz;
int a[MAX], b[MAX], c[MAX]; char f[MAX];

int cmp(const void *u, const void *v) { return ((EDGE *)u)->c - ((EDGE *)v)->c; }

ll kruskal(int E) {
	int i; ll ans = 0;

	qsort(edge, E, sizeof(EDGE), cmp);
	for (i = 0; i < E; i++) {
		if (!connected(edge[i].a, edge[i].b)) { 
			unite(edge[i].a, edge[i].b);
			ans += edge[i].c;
		}
	}
	return ans;
}

int main()
{
	int i, N, M, K; ll cost, iden;

	N = in(), M = in(), K = in();
	cost = 0; for (i = 1; i <= M; i++) {
		a[i] = in(), b[i] = in(), c[i] = in();
		cost += c[i];
	}
	while (K--) f[in()] = 1;

	init(N+1);  // UNION-FIND 初期化
	iden = 0, sz = 0;
	for (i = 1; i <= M; i++) {
		if (f[i]) {
			iden += c[i];
			unite(a[i], b[i]);
		} else edge[sz].a = a[i], edge[sz].b = b[i], edge[sz++].c = c[i];
	}
	printf("%lld\n", cost-iden-kruskal(sz));
	return 0;
}
0