이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#define wiwihorz
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma loop-opt(on)
#define rep(i, a, b) for(int i = a; i <= b; i++)
#define rrep(i, a, b) for(int i = b; i >= a; i--)
#define ceil(a, b) ((a + b - 1) / (b))
#define all(x) x.begin(), x.end()
#define INF 1000000000000000000
#define MOD 1000000007
#define eps (1e-9)
#define MAXN 1000005
#define int long long int
#define lld long double
#define pii pair<int, int>
#define random mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count())
using namespace std;
#ifdef wiwihorz
#define print(a...) kout("[" + string(#a) + "] = ", a)
void vprint(auto L, auto R) {
while(L < R) cerr << *L << " \n"[next(L) == R], ++ L;
}
void kout() {cerr << endl;}
template<class T1, class ... T2> void kout(T1 a, T2 ... e) {
cerr << a << " ", kout(e...);
}
#else
#define print(...) 0
#define vprint(...) 0
#endif
vector<int> g, ans, dis;
vector<pii> qq;
int n, m, k, q;
struct edge {
int to, cost;
};
vector<vector<edge>> mp;
struct DSU {
int n;
vector<int> par, sz;
vector<unordered_set<int>> s;
void init_(int _n) {
n = _n;
par.assign(n + 1, 0);
sz.assign(n + 1, 1);
s.assign(n + 1, unordered_set<int>());
rep(i, 1, n) par[i] = i;
return;
}
int find_par(int a) {
while(a != par[a]) a = par[a];
return a;
}
void unite(int a, int b, int cur) {
int aa = find_par(a);
int bb = find_par(b);
if(aa == bb) return;
if(sz[aa] < sz[bb]) swap(aa, bb);
par[bb] = aa, sz[aa] += sz[bb];
for(auto i : s[bb]) {
if(s[aa].find(i) != s[aa].end()) {
ans[i] = cur;
s[aa].erase(s[aa].find(i));
}
else s[aa].insert(i);
}
s[bb].clear();
return;
}
void add(int x, int id) {
int a = find_par(x);
s[a].insert(id);
}
} dsu;
void build() {
dis.assign(n + 1, INF);
priority_queue<pii, vector<pii>, greater<pii>> pq;
rep(i, 1, k) {
dis[g[i]] = 0;
pq.push({0, g[i]});
}
while(pq.size()) {
pii p = pq.top(); pq.pop();
if(dis[p.second] != p.first) continue;
int cur = p.second;
for(auto i : mp[cur]) {
int v = i.cost + dis[cur];
if(dis[i.to] > v) {
dis[i.to] = v;
pq.push({v, i.to});
}
}
}
return;
}
bool cmp(int a, int b) {
return dis[a] > dis[b];
}
void solve() {
vector<int> a;
rep(i, 1, n) a.push_back(i);
sort(all(a), cmp);
for(auto i : a) {
for(auto j : mp[i]) {
if(dis[j.to] >= dis[i]) {
dsu.unite(i, j.to, dis[i]);
}
}
}
return;
}
signed main() {
ios::sync_with_stdio(false), cin.tie(0);
cin >> n >> m;
mp.assign(n + 1, vector<edge>());
rep(i, 1, m) {
int a, b, c;
cin >> a >> b >> c;
mp[a].push_back({b, c});
mp[b].push_back({a, c});
}
cin >> k;
g.assign(k + 1, 0);
rep(i, 1, k) cin >> g[i];
build();
cin >> q;
dsu.init_(n);
ans.assign(q + 1, 0);
qq.assign(q + 1, {0 ,0});
rep(i, 1, q) {
int s, t; cin >> s >> t;
qq[i] = {s, t};
dsu.add(s, i);
dsu.add(t, i);
}
solve();
rep(i, 1, q) {
cout << ans[i] << "\n";
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
plan.cpp:4: warning: ignoring #pragma loop [-Wunknown-pragmas]
4 | #pragma loop-opt(on)
|
plan.cpp:24:13: warning: use of 'auto' in parameter declaration only available with '-fconcepts'
24 | void vprint(auto L, auto R) {
| ^~~~
plan.cpp:24:21: warning: use of 'auto' in parameter declaration only available with '-fconcepts'
24 | void vprint(auto L, auto R) {
| ^~~~
# | 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... |