이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define ll long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define fi first
#define se second
#define f(i, a, b) for (int i = a; i <= b; i++)
using namespace std;
/*
░░░░░░██╗███████╗██████╗░░░░█████╗░░█████╗░██████╗░███████╗██████╗░░
░░░░░░██║██╔════╝██╔══██╗░░██╔══██╗██╔══██╗██╔══██╗██╔════╝██╔══██╗░
░░░░░░██║█████╗░░██████╦╝░░██║░░╚═╝██║░░██║██║░░██║█████╗░░██████╔╝░
░██╗░░██║██╔══╝░░██╔══██╗░░██║░░██╗██║░░██║██║░░██║██╔══╝░░██╔══██╗░
░╚█████╔╝███████╗██████╦╝░░╚█████╔╝╚█████╔╝██████╔╝███████╗██║░░██║░
░░╚════╝░╚══════╝╚═════╝░░░░╚════╝░░╚════╝░╚═════╝░╚══════╝╚═╝░░╚═╝░
// == [[ << - >> ]] == //
// Author: Jeb Coder //
// Language: C++ //
*/
const int NMax = 1e5 + 5;
ll n, m, x, y, w, k, q;
ll a[NMax], p[NMax], parent[NMax], s[NMax], id[NMax], dist[NMax];
vector<pll> adj[NMax];
bool mark[NMax];
set<ll>sz[NMax];
bool COMP (ll a, ll b)
{
return dist[a] > dist[b];
}
void Dijsktra()
{
for (int i = 1; i <= n; i++)
dist[i] = LLONG_MAX;
priority_queue<pll,vector<pll>, greater<pll> >q;
for (int i = 1; i <= k; i++) {
dist[a[i]] = 0;
q.push ({0, a[i]});
}
while (!q.empty()) {
ll u = q.top().se; q.pop();
if(mark[u]) continue;
mark[u] = true;
for (auto v : adj[u]) {
ll it = v.fi;
ll w = v.se;
if (dist[it] > dist[u] + w) {
dist[it] = dist[u] + w;
q.push ({dist[it], it});
}
}
}
}
ll Find_Set(ll u)
{
if (u == parent[u]) return u;
return parent[u] = Find_Set (parent[u]);
}
void Union_Set (ll u, ll v)
{
u = Find_Set(u); v = Find_Set(v);
if (u == v) return;
if (s[u] < s[v]) swap(u, v);
parent[v] = u;
s[u] += s[v];
for (auto it : sz[v]) {
if (sz[u].find (it) != sz[u].end()) {
sz[u].erase (it);
p[it] = w;
}
else sz[u].insert (it);
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
// freopen(".INP", "r", stdin);
// freopen(".OUT", "w", stdout);
cin >> n >> m;
while (m--) {
cin >> x >> y >> w;
adj[x].push_back ({y, w});
adj[y].push_back ({x, w});
}
cin >> k;
for (int i = 1; i <= k; i++) cin >> a[i];
Dijsktra();
cin >> q;
for (int i = 1; i <= q; i++) {
cin >> x >> y;
sz[x].insert (i);
sz[y].insert (i);
}
for (int i = 1; i <= n; i++) {
id[i] = i;
parent[i] = i;
s[i] = i;
}
sort (id + 1, id + n + 1, COMP);
memset(mark,0,sizeof(mark));
for (int i = 1; i <= n; i++) {
x = id[i];
mark[x] = true;
w = dist[x];
for (auto it : adj[x]) {
y = it.fi;
if (mark[y]) Union_Set (x,y);
}
}
for (int i = 1; i <= q; i++)
cout << p[i] << '\n';
return 0;
}
/*
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |