이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair < int, int > pii;
typedef pair < long long, long long > pll;
const int N = 1e5 + 7, inf = 1e9 + 7;
const ll INF = 1e18 + 7;
const int X[] = {1, -1, 0, 0};
const int Y[] = {0, 0, 1, -1};
int n, m, d[N], k, s, t, dist[N], p[N];
bool us[N];
int u[N], v[N], tin[N], tout[N], timer, up[N][27], w[N][27];
int dep[N];
vector < pii > g[N];
vector < pair < int, pii > > g1;
vector < pii > g2[N];
int get(int v) {
if (v == p[v])
return v;
return p[v] = get(p[v]);
}
bool unite(int v, int u) {
v = get(v), u = get(u);
if (v != u) {
p[v] = u;
return 1;
}
return 0;
}
bool upper(int a, int b) {
return tin[a] <= tin[b] && tout[a] >= tout[b];
}
void dfs(int v, int p = 0) {
us[v] = 1;
tin[v] = ++timer;
up[v][0] = p;
w[v][0] = min(d[v], d[p]);
dep[v] = dep[p] + 1;
for (int i = 1; i <= 20; i++) {
up[v][i] = up[up[v][i - 1]][i - 1];
w[v][i] = min(w[up[v][i - 1]][i - 1], w[v][i - 1]);
}
for (int i = 0; i < g2[v].size(); i++) {
pii to = g2[v][i];
if (to.first == p) {
continue;
}
if (!us[to.first]) {
dfs(to.first, v);
}
}
tout[v] = ++timer;
}
int lca(int a, int b) {
if (upper(a, b)) {
return a;
}
if (upper(b, a)) {
return b;
}
for (int i = 20; i >= 0; i--) {
if (!upper(up[a][i], b)) {
a = up[a][i] ;
}
}
return up[a][0];
}
int ans(int a, int b) {
if (a == b) {
return inf;
}
int cur = w[a][0];
for (int i = 20; i >= 0; i--) {
if (dep[up[a][i]] >= dep[b]) {
cur = min(cur, w[a][i]);
a = up[a][i];
}
}
return cur;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
d[i] = inf;
}
for (int i = 1, x; i <= m; i++) {
cin >> u[i] >> v[i] >> x;
g[u[i]].push_back(make_pair(v[i], x));
g[v[i]].push_back(make_pair(u[i], x));
}
set < pair <int, int> > st;
cin >> k;
for (int i = 1, x; i <= k; i++) {
cin >> x;
d[x] = 0;
st.insert(make_pair(0, x));
}
while(!st.empty()) {
int x = st.begin() -> second;
st.erase(st.begin());
for (int i = 0; i < g[x].size(); ++i) {
int to = g[x][i].first, len = g[x][i].second;
if (d[x] + len < d[to]) {
st.erase(make_pair(d[to], to));
d[to] = d[x] + len;
st.insert(make_pair(d[to], to));
}
}
}
for (int i = 1; i <= m; i++) {
g1.push_back(make_pair(min(d[u[i]], d[v[i]]), make_pair(u[i], v[i])));
}
for (int i = 1; i <= n; ++i) {
p[i] = i;
}
sort(g1.begin(), g1.end());
reverse(g1.begin(), g1.end());
for (int i = 0; i < m; i++) {
int u = g1[i].second.first, v = g1[i].second.second;
if (unite(u, v)) {
g2[u].push_back(make_pair(v, g1[i].first));
g2[v].push_back(make_pair(u, g1[i].first));
}
}
d[0] = inf;
dfs(1);
int q;
cin >> q;
while(q--) {
cin >> s >> t;
int pr = lca(s, t);
cout << min(ans(s, pr), ans(t, pr)) << '\n';
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
plan.cpp: In function 'void dfs(int, int)':
plan.cpp:53:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < g2[v].size(); i++) {
~~^~~~~~~~~~~~~~
plan.cpp: In function 'int main()':
plan.cpp:116:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < g[x].size(); ++i) {
~~^~~~~~~~~~~~~
# | 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... |