이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#ifdef DEBUG
#include <time.h>
#endif
#define all(a) (a).begin(), (a).end()
#define rev(a) (a).rbegin(), (a).rend()
#define F first
#define S second
int recur_depth = 0;
#ifdef DEBUG
#define dbg(x) \
{ \
++recur_depth; \
auto x_ = x; \
--recur_depth; \
cerr << string(recur_depth, '\t') << "\e[91m" << __func__ << ":" \
<< __LINE__ << "\t" << #x << " = " << x_ << "\e[39m" << endl; \
}
#else
#define dbg(x)
#endif
using namespace std;
using namespace __gnu_pbds;
typedef pair<int, int> ii;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> llll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pair<int, int>> vii;
typedef vector<vii> vvii;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<pair<ll, ll>> vll;
typedef vector<vll> vvll;
typedef vector<bool> vb;
template <class type1>
using ordered_set = tree<type1, null_type, less<type1>, rb_tree_tag,
tree_order_statistics_node_update>;
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p) {
return os << '(' << p.first << ", " << p.second << ')';
}
template <typename T_container, typename T = typename enable_if<
!is_same<T_container, string>::value,
typename T_container::value_type>::type>
ostream &operator<<(ostream &os, const T_container &v) {
os << '{';
string sep;
for (const T &x : v)
os << sep << x, sep = ", ";
return os << '}';
}
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const int INF = 1e9;
const ld EPS = 1e-9;
struct DSU {
int n;
vi par, sz;
DSU(int n) : n(n) {
par = vi(n, -1);
sz = vi(n, 1);
}
int find(int x) {
if (par[x] == -1)
return x;
return par[x] = find(par[x]);
}
void merge(int u, int v) {
u = find(u), v = find(v);
if (u == v)
return;
if (sz[u] < sz[v])
swap(u, v);
sz[u] += sz[v];
par[v] = u;
}
bool connected(int u, int v) { return find(u) == find(v); }
};
void solve() {
int n, m;
cin >> n >> m;
vvii adj(n);
vvi ed(m);
for (int i = 0; i < m; i++) {
int u, v, w;
cin >> u >> v >> w;
u--, v--;
adj[u].push_back({v, w}), adj[v].push_back({u, w});
ed[i] = {u, v, w};
}
int k;
cin >> k;
vi nuc(k);
for (int &x : nuc)
cin >> x, x--;
vi dist(n, INF);
vb vis(n);
priority_queue<ii, vii, greater<ii>> pq;
for (int &x : nuc)
pq.push({0, x}), dist[x] = 0;
while (!pq.empty()) {
auto [d, u] = pq.top();
pq.pop();
if (vis[u])
continue;
vis[u] = 1;
for (auto [v, w] : adj[u])
if (!vis[v] && dist[v] > w + d) {
dist[v] = d + w;
pq.push({d + w, v});
}
}
sort(all(ed), [&](vi a, vi b) {
return min(dist[a[0]], dist[a[1]]) > min(dist[b[0]], dist[b[1]]);
});
vi ed_idx;
DSU d(n);
for (int i = 0; i < m; i++) {
int u = ed[i][0], v = ed[i][1];
if (d.connected(u, v))
continue;
d.merge(u, v);
ed_idx.push_back(i);
}
vvi nadj(n);
for (int &idx : ed_idx) {
int u = ed[idx][0], v = ed[idx][1];
nadj[u].push_back(v);
nadj[v].push_back(u);
}
const int MAXLG = 22;
vvi par(MAXLG, vi(n, -1));
vi depth(n);
function<void(int, int, int)> f = [&](int u, int p, int d) {
par[0][u] = p;
depth[u] = d;
for (int &v : nadj[u])
if (v != p) {
f(v, u, d + 1);
}
};
f(0, -1, 0);
for (int j = 1; j < MAXLG; j++)
for (int i = 0; i < n; i++)
if (par[j - 1][i] != -1)
par[j][i] = par[j - 1][par[j - 1][i]];
vvi cost(MAXLG, vi(n));
for (int i = 0; i < n; i++)
cost[0][i] = min(dist[i], par[0][i] == -1 ? INF : dist[par[0][i]]);
for (int j = 1; j < MAXLG; j++)
for (int i = 0; i < n; i++)
cost[j][i] = min(cost[j - 1][i],
par[j - 1][i] == -1 ? INF : cost[j - 1][par[j - 1][i]]);
auto kth = [&](int u, int k) {
if (depth[u] < k)
return -1;
for (int i = MAXLG - 1; i >= 0; i--)
if (k & (1 << i))
u = par[i][u];
return u;
};
auto lca = [&](int u, int v) {
if (depth[u] < depth[v])
swap(u, v);
u = kth(u, depth[u] - depth[v]);
if (u == v)
return u;
for (int i = MAXLG - 1; i >= 0; i--)
if (par[i][u] != par[i][v]) {
u = par[i][u];
v = par[i][v];
}
return par[0][u];
};
auto ans = [&](int u, int v) {
int l = lca(u, v);
int res = INF;
int k1 = depth[u] - depth[l];
for (int i = MAXLG - 1; i >= 0; i--)
if (k1 & (1 << i)) {
res = min(res, cost[i][u]);
u = par[i][u];
}
int k2 = depth[v] - depth[l];
for (int i = MAXLG - 1; i >= 0; i--)
if (k2 & (1 << i)) {
res = min(res, cost[i][v]);
v = par[i][v];
}
return res;
};
int q;
cin >> q;
while (q--) {
int s, t;
cin >> s >> t;
s--, t--;
cout << ans(s, t) << "\n";
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
clock_t start = clock();
int test_cases = 1;
// cin >> test_cases;
while (test_cases--)
solve();
#ifdef DEBUG
cerr << fixed << setprecision(10)
<< "\nTime Taken: " << (double)(clock() - start) / CLOCKS_PER_SEC
<< "s\n";
#endif
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
plan.cpp: In function 'int main()':
plan.cpp:266:11: warning: unused variable 'start' [-Wunused-variable]
266 | clock_t start = clock();
| ^~~~~
# | 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... |