結果

問題 No.1600 Many Shortest Path Problems
ユーザー e869120e869120
提出日時 2021-05-07 14:47:36
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 1,948 bytes
コンパイル時間 894 ms
コンパイル使用メモリ 78,888 KB
実行使用メモリ 24,032 KB
最終ジャッジ日時 2023-09-14 06:49:42
合計ジャッジ時間 13,201 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
9,700 KB
testcase_01 AC 6 ms
9,968 KB
testcase_02 AC 6 ms
9,680 KB
testcase_03 AC 6 ms
9,920 KB
testcase_04 RE -
testcase_05 RE -
testcase_06 AC 6 ms
9,960 KB
testcase_07 AC 6 ms
9,740 KB
testcase_08 AC 6 ms
9,684 KB
testcase_09 AC 7 ms
9,624 KB
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 AC 7 ms
9,824 KB
testcase_16 AC 7 ms
9,684 KB
testcase_17 RE -
testcase_18 RE -
testcase_19 AC 7 ms
9,892 KB
testcase_20 AC 7 ms
9,940 KB
testcase_21 RE -
testcase_22 AC 7 ms
9,824 KB
testcase_23 AC 7 ms
9,964 KB
testcase_24 RE -
testcase_25 AC 7 ms
9,620 KB
testcase_26 AC 7 ms
9,740 KB
testcase_27 AC 6 ms
9,892 KB
testcase_28 AC 6 ms
9,700 KB
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 AC 7 ms
9,672 KB
testcase_34 AC 6 ms
9,960 KB
testcase_35 RE -
testcase_36 RE -
testcase_37 AC 7 ms
9,820 KB
testcase_38 RE -
testcase_39 AC 7 ms
9,620 KB
testcase_40 RE -
testcase_41 RE -
testcase_42 RE -
testcase_43 RE -
testcase_44 RE -
testcase_45 RE -
testcase_46 RE -
testcase_47 RE -
testcase_48 RE -
testcase_49 AC 7 ms
9,820 KB
testcase_50 AC 7 ms
9,624 KB
testcase_51 AC 7 ms
9,696 KB
testcase_52 AC 7 ms
9,956 KB
testcase_53 AC 7 ms
9,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cassert>
#include <algorithm>
using namespace std;
#pragma warning (disable: 4996)

class UnionFind {
public:
	vector<int> par;

	void init(int sz) {
		par.resize(sz);
		for (int i = 0; i < sz; i++) par[i] = -1;
	}
	int root(int pos) {
		if (par[pos] == -1) return pos;
		par[pos] = root(par[pos]);
		return par[pos];
	}
	void unite(int u, int v) {
		u = root(u); v = root(v);
		if (u == v) return;
		par[u] = v;
	}
	bool same(int u, int v) {
		if (root(u) == root(v)) return true;
		return false;
	}
};

// 入力ほか
long long mod = 1000000007;
long long N, M, Q;
long long A[1 << 18], B[1 << 18], C[1 << 18];
long long X[1 << 18], Y[1 << 18], Z[1 << 18];
long long dist[1 << 18];
vector<pair<int, long long>> G[1 << 18];

void dfs(int pos, int pre, long long dep) {
	dist[pos] = dep;
	for (int i = 0; i < G[pos].size(); i++) {
		int to = G[pos][i].first;
		long long cost = G[pos][i].second;
		if (to == pre) continue;
		dfs(to, pos, (dep + cost) % mod);
	}
}

long long solve(int x, int y, int z) {
	for (int i = 1; i <= N; i++) G[i].clear();
	for (int i = 1; i <= N; i++) dist[i] = (1 << 30);

	UnionFind UF;
	UF.init(N + 2);
	for (int i = 1; i <= M; i++) {
		if (i == z) continue;
		if (UF.same(A[i], B[i]) == false) {
			UF.unite(A[i], B[i]);
			G[A[i]].push_back(make_pair(B[i], C[i]));
			G[B[i]].push_back(make_pair(A[i], C[i]));
		}
	}

	dfs(x, -1, 0);
	if (dist[y] == (1 << 30)) return -1;
	return dist[y];
}

int main() {
	// Step #1. 入力
	scanf("%lld%lld", &N, &M);
	for (int i = 1; i <= M; i++) scanf("%lld%lld", &A[i], &B[i]);
	scanf("%lld", &Q);
	for (int i = 1; i <= Q; i++) scanf("%lld%lld%lld", &X[i], &Y[i], &Z[i]);
	C[1] = 2LL;
	for (int i = 2; i <= M; i++) C[i] = (2LL * C[i - 1]) % mod;
	assert(N <= 2000 && M <= 2000);

	// Step #2. クエリに答える
	for (int i = 1; i <= Q; i++) {
		long long ans = solve(X[i], Y[i], Z[i]);
		printf("%lld\n", ans);
	}
	return 0;
}
0