이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define FASTIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define pii pair<int, int>
#define ff first
#define ss second
#define PI acos(-1)
#define ld long double
const int mod = 1e9+7, N = 1e5+5;
int msb(int val){return sizeof(int)*8-__builtin_clzll(val);}
int a[N], n, m, k;
vector<pii> edges[N];
vector<int> dist(N, mod*mod);
set<int> s[N];
int ans[N];
bool used[N];
struct des{
vector<int> p;
des(){
p.resize(n, 0);
for(int i=0;i<n;i++)p[i] = i;
}
int par(int i){
if(i==p[i])return i;
return p[i] = par(p[i]);
}
void merge(int u, int v, int di){
u=par(u), v=par(v);
if(u == v)return;
if(s[u].size()<s[v].size())swap(s[u], s[v]);
for(auto x : s[v]){
if(s[u].count(x)){
ans[x] = di;
}else {
s[u].insert(x);
}
}p[v] = u;
}
};
void solve(int test_case){
int i, j;
cin >> n >> m;
for(i=0;i<m;i++){
int u, v, c;
cin >> u >> v >> c;
u--,v--;
edges[u].pb({v,c});
edges[v].pb({u,c});
}
priority_queue<pii, vector<pii>, greater<pii>> q;
cin >> k;
for(i=0;i<k;i++){
int u;
cin >> u;u--;
dist[u] = 0;
q.push({0, u});
}
while(!q.empty()){
auto [d, u] = q.top(); q.pop();
if(d > dist[u])continue;
assert(d == dist[u]);
for(auto [v, c] : edges[u]){
if(dist[v] > d+c){
dist[v] = d+c;
q.push({dist[v], v});
}
}
}
des ds;
int Q;
cin >> Q;
for(j=0;j<Q;j++){
int u, v;
cin >> u >> v;u--,v--;
s[u].insert(j);
s[v].insert(j);
}
vector<pii> v;
for(i=0;i<n;i++)v.pb({dist[i], i});
sort(all(v));reverse(all(v));
for(auto [dis, u] : v){
used[u] = true;
for(auto [V, noneed] : edges[u]){
if(used[V])ds.merge(u, V, dis);
}
}
for(i=0;i<Q;i++)cout << ans[i] << '\n';
return;
}
signed main(){
FASTIO;
#define MULTITEST 0
#if MULTITEST
int ___T;
cin >> ___T;
for(int T_CASE = 1; T_CASE <= ___T; T_CASE++)
solve(T_CASE);
#else
solve(1);
#endif
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... |