이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define N 100005
#define inf 2000000000
#define f first
#define s second
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
int n, m, k, q, P[N], id[N], dist[N], rev[N], pai[N], peso[N];
vector<pii> grafo[N];
list<int> l[N];
list<pii> hist[N];
void dijkstra()
{
priority_queue<pii, vector<pii>, greater<pii> > pq;
for(int i = 1; i <= n; i++) dist[i] = inf;
for(int i = 1; i <= k; i++) dist[ P[i] ] = 0, pq.push({0, P[i]});
while(!pq.empty())
{
int x = pq.top().s, d = pq.top().f;
pq.pop();
if(d > dist[x]) continue;
for(auto v: grafo[x])
{
if(dist[v.f] > dist[x] + v.s)
{
dist[v.f] = dist[x] + v.s;
pq.push({dist[v.f], v.f});
}
}
}
vector<pii> v;
for(int i = 1; i <= n; i++) v.push_back({dist[i], i});
sort(v.begin(), v.end());
for(int i = 0; i < v.size(); i++) id[ v[i].s ] = i + 1, rev[i + 1] = v[i].s;
}
void persistent_join(int a, int b, int t)
{
a = pai[a], b = pai[b];
if(a != b)
{
if(peso[a] > peso[b]) swap(a, b);
for(auto it = l[a].begin(); it != l[a].end(); it++)
{
hist[*it].push_back(pii(t, b));
pai[*it] = b;
}
peso[b] += peso[a];
l[b].splice(l[b].end(), l[a]);
}
}
int p_query(int a, int b)
{
int ans = 0;
for(auto x: hist[a])
for(auto y: hist[b])
if(x.s == y.s)
ans = max(ans, min(x.f, y.f));
return ans;
}
int main()
{
ios::sync_with_stdio(false); cin.tie(0);
cin>>n>>m;
for(int i = 1, a, b, c; i <= m; i++)
{
cin>>a>>b>>c;
grafo[a].push_back({b, c});
grafo[b].push_back({a, c});
}
cin>>k;
for(int i = 1; i <= k; i++) cin>>P[i];
dijkstra();
for(int i = 1; i <= n; i++)
{
l[i].push_back(i);
hist[i].push_back(pii(dist[i], i));
peso[i] = 1, pai[i] = i;
}
for(int x = n; x >= 1; x--)
{
int xi = rev[x];
for(auto v: grafo[xi])
if(id[v.f] > x) persistent_join(v.f, xi, dist[xi]);
}
cin>>q;
for(int t = 1, a, b; t <= q; t++)
{
cin>>a>>b;
cout<<p_query(a, b)<<"\n";
}
}
컴파일 시 표준 에러 (stderr) 메시지
plan.cpp: In function 'void dijkstra()':
plan.cpp:51:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < v.size(); i++) id[ v[i].s ] = i + 1, rev[i + 1] = v[i].s;
~~^~~~~~~~~~
# | 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... |