이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define pb emplace_back
#define ll long long
#define fi first
#define se second
#define mp make_pair
//#define int int64_t
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 find(int x) {return lab[x] < 0? x: lab[x] = find(lab[x]);}
bool uni(int x, int y) {
x = find(x), y = find(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) {
for(int i = 1; i <= logN; ++i) {
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;
for(int i = logN; i >= 0; --i) {
if(h[u] - (1 << i) >= h[v]) {
res = min(res, mn[u][i]);
u = p[u][i];
}
}
if(u == v) return res;
for(int i = logN; i >= 0; --i) {
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;
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
#define Task "test"
if(fopen(Task".inp", "r")) {
freopen(Task".inp", "r", stdin);
freopen(Task".out", "w", stdout);
}
cin >> n >> m;
for(int i = 1; i <= m; ++i) {
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;
for(int i = 1; i <= k; ++i) {
cin >> u;
d[u] = 0, pq.emplace(d[u], u);
}
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& e: edges) e.w = min(d[e.u], d[e.v]);
sort(edges.begin(), edges.end());
for(int i = 1; i <= n; ++i) adj[i].clear();
for(auto &e: edges) {
if(uni(e.u, e.v)) {
adj[e.u].pb(e.v, e.w);
adj[e.v].pb(e.u, e.w);
}
}
dfs(1);
cin >> q;
while(q --) {
cin >> u >> v;
cout << get_min(u, v) << '\n';
}
}
컴파일 시 표준 에러 (stderr) 메시지
plan.cpp: In function 'int32_t main()':
plan.cpp:80:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen(Task".inp", "r", stdin);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
plan.cpp:81:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen(Task".out", "w", stdout);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |