Submission #378734

#TimeUsernameProblemLanguageResultExecution timeMemory
378734rk42745417Evacuation plan (IZhO18_plan)C++17
41 / 100
4061 ms22496 KiB
#include <bits/stdc++.h>
using namespace std;

using ll = int64_t;
using ull = uint64_t;
using uint = uint32_t;
using ld = long double;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const ll LINF = 2e18;
const double EPS = 1e-9;
#define EMT ios::sync_with_stdio(0); cin.tie(0);

const int N = 1e5 + 25;
int dis[N];
bool vis[N];
vector<int> npp;
vector<pair<int, int>> edge[N];

struct DisjointSet {
	int pa[N], sz[N];
	void init(int n) {
		for(int i = 1; i <= n; i++)
			sz[i] = 1, pa[i] = i;
	}
	int fnd(int x) { return pa[x] == x ? pa[x] : pa[x] = fnd(pa[x]); }
	bool uni(int a, int b) {
		if((a = fnd(a)) == (b = fnd(b)))
			return false;
		if(sz[a] > sz[b])
			swap(a, b);
		sz[b] += sz[a];
		pa[a] = b;
		return true;
	}
} dsu;
void solve() {
	memset(dis, 0x3f, sizeof dis);
	priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
	for(int a : npp)
		dis[a] = 0, pq.push({dis[a], a});
	while(!pq.empty()) {
		auto [c, u] = pq.top();
		pq.pop();
		if(c > dis[u])
			continue;
		for(const auto &[v, d] : edge[u]) {
			if(dis[v] > c + d) {
				dis[v] = c + d;
				pq.push({dis[v], v});
			}
		}
	}
};

signed main() { EMT
	int n, m;
	cin >> n >> m;
	for(int i = 0; i < m; i++) {
		int u, v, w;
		cin >> u >> v >> w;
		edge[u].push_back({v, w});
		edge[v].push_back({u, w});
	}

	int k;
	cin >> k;
	npp.resize(k);
	for(int &a : npp)
		cin >> a;
	
	solve();
	dsu.init(n);
	
	int q;
	cin >> q;
	vector<pair<int, int>> que(q), arr(n);
	vector<int> ans(q);
	for(int i = 0; i < q; i++)
		cin >> que[i].first >> que[i].second;
	for(int i = 1; i <= n; i++)
		arr[i - 1] = {dis[i], i};
	sort(arr.begin(), arr.end(), greater<pair<int, int>>());
	for(const auto &[w, u] : arr) {
		vis[u] = 1;
		for(const auto &[v, d] : edge[u])
			if(vis[v])
				dsu.uni(u, v);
		for(int i = 0; i < q; i++)
			if(!ans[i] && dsu.fnd(que[i].first) == dsu.fnd(que[i].second))
				ans[i] = w;
	}
	for(int a : ans)
		cout << a << '\n';
}
/*
9 12
1 9 4
1 2 5
2 3 7
2 4 3
4 3 6
3 6 4
8 7 10
6 7 5
5 8 1
9 5 7
5 4 12
6 8 2
2
4 7
5
1 6
5 3
4 8
5 8
1 5

*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...