이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
//code
#define fl(i,x,y,z) for(int i=x;i<=y;i=i+z)
#define fn(i,x,y,z) for(int i=x;i>=y;i=i-z)
#define rep(i,x,y) for(int i=x;i<y;i=i+1)
#define all(v) v.begin(),v.end()
#define pb emplace_back
#define tle cout<<"tle"<<endl
#define edl cout<<"\n"
#define el "\n"
#define getbit(x,i) ((x>>i)&1)
#define bitcnt __builtin_popcount
//ham
//#define pii pair<int,int>
#define fi first
#define se second
#define ll long long
#define ld long double
using namespace std;
const int N = int(5e5) + 7;
const int inf = 1e9 + 1;
typedef pair<ll, ll> pii;
int n, m, k, d[N], q, u, v, w;
vector<pii> adj[N];
struct TEdges {
int u, v, w;
TEdges() {u = v = w = 0;}
TEdges(int u, int v, int w): u(u), v(v), w(w) {}
bool operator < (const TEdges& x)const& {
return w > x.w;
}
};
vector<TEdges> edges;
priority_queue<pii, vector<pii>, greater<pii>> pq;
int lab[N];
int findset(int x)
{
return lab[x] < 0? x: lab[x] = findset(lab[x]);
}
bool uni(int x, int y) {
x = findset(x), y = findset(y);
if(x == y) return 0;
if(lab[x] > lab[y]) swap(x, y);
lab[x] += lab[y], lab[y] = x;
return 1;
}
const int logn = 20;
int p[N][logn + 1], mn[N][logn + 1], h[N];
void dfs(int u) {
fl(i,1,logn,1) {
p[u][i] = p[p[u][i - 1]][i - 1];
mn[u][i] = min(mn[u][i - 1], mn[p[u][i - 1]][i - 1]);
}
for(auto& v: adj[u]) {
if(v.fi == p[u][0]) continue;
p[v.fi][0] = u;
mn[v.fi][0] = v.se;
h[v.fi] = h[u] + 1;
dfs(v.fi);
}
}
int get_min(int u, int v) {
if(h[u] < h[v]) swap(u, v);
int res = inf;
fn(i,logn,0,1) {
if(h[u] - (1 << i) >= h[v]) {
res = min(res, mn[u][i]);
u = p[u][i];
}
}
if(u == v) return res;
fn(i,logn,0,1) {
if(p[u][i] != p[v][i]) {
res = min({res, mn[u][i], mn[v][i]});
u = p[u][i], v = p[v][i];
}
}
res = min({res, mn[u][0], mn[v][0]});
return res;
}
void inp()
{
cin >> n >> m;
fl(i,1,m,1)
{
cin >> u >> v >> w;
adj[u].pb(v, w);
adj[v].pb(u, w);
edges.pb(u, v, w);
}
fill_n(&mn[0][0], N * (logn + 1), inf);
fill(lab, lab + n + 1, -1);
fill(d, d + n + 1, inf);
cin>>k;
fl(i,1,k,1)
{
cin >> u;
d[u] = 0, pq.emplace(d[u], u);
}
}
void sol()
{
pii top;
while(pq.size()) {
top = pq.top(); pq.pop();
if(top.fi != d[top.se]) continue;
for(auto& v: adj[top.se]) {
if(top.fi + v.se < d[v.fi]) {
d[v.fi] = top.fi + v.se;
pq.emplace(d[v.fi], v.fi);
}
}
}
for(auto& it: edges) it.w = min(d[it.u], d[it.v]);
sort(edges.begin(), edges.end());
fl(i,1,n,1) adj[i].clear();
for(auto &it: edges) {
if(uni(it.u, it.v)) {
adj[it.u].pb(it.v, it.w);
adj[it.v].pb(it.u, it.w);
}
}
dfs(1);
cin>>q;
while(q --) {
cin >> u >> v;
cout << get_min(u, v) << '\n';
}
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
inp();
sol();
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... |