이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define N 100002
#define ff first
#define ss second
#define ll long long
int d[N], n, m;
vector<pair<int, int> > adj[N];
priority_queue<pair<int, int> > pq;
vector<int> dis;
vector<pair<int, int> > plist[N];
bool in[N];
int p[N];
vector<int> group[N];
int find(int v){
return v == p[v] ? v : p[v] = find(p[v]);
}
void merge(int v, int u, int dre){
v = find(v);
u = find(u);
if(v == u) return;
if(group[v].size() > group[u].size()) swap(v, u);
p[v] = u;
for(int& x : group[v]){
while(plist[x].back().ff == dre)
plist[x].pop_back();
plist[x].push_back({dre, u});
group[u].push_back(x);
}
group[v].clear();
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
memset(d, 0x3f, sizeof d);
cin>>n>>m;
for(int i = 1; i <= n; i++){
plist[i].push_back({0x3f3f3f3f, i});
p[i] = i;
group[i].push_back(i);
}
while(m--){
int v, u, w;
cin>>v>>u>>w;
adj[v].push_back({u, w});
adj[u].push_back({v, w});
}
cin>>m;
while(m--){
int v;
cin>>v;
d[v] = 0;
pq.push({0, v});
}
while(!pq.empty()){
int v = pq.top().ss;
int dd = pq.top().ff;
pq.pop();
if(dd != -d[v]) continue;
for(auto& x : adj[v]){
if(d[x.ff] > x.ss - dd){
d[x.ff] = x.ss - dd;
pq.push({-d[x.ff], x.ff});
}
}
}
for(int i = 1; i <= n; i++)
dis.push_back(i);
sort(dis.begin(), dis.end(), [](const int& lhs, const int& rhs){
return d[lhs] > d[rhs];
});
for(auto& x : dis){
in[x] = 1;
for(auto& y : adj[x])
if(in[y.ff]) merge(x, y.ff, d[x]);
}
cin>>m;
while(m--){
int v, u;
cin>>v>>u;
int i1 = plist[v].size() - 1, i2 = plist[u].size() - 1;
while(plist[v][i1].ss == plist[u][i2].ss){
i1--; i2--;
}
i1++;
i2++;
cout<<min(plist[v][i1].ff, plist[u][i2].ff)<<'\n';
}
}
# | 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... |