This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define TASK "long"
#define fi first
#define se second
#define ll long long
#define pb push_back
#define ALL(x) (x).begin(), (x).end()
#define GETBIT(mask, i) ((mask) >> (i) & 1)
#define MASK(i) ((1LL) << (i))
#define SZ(x) ((int)(x).size())
#define mp make_pair
#define CNTBIT(mask) __builtin_popcount(mask)
template<class X, class Y> bool maximize(X &x, Y y){ if (x < y) {x = y; return true;} return false;};
template<class X, class Y> bool minimize(X &x, Y y){ if (x > y) {x = y; return true;} return false;};
typedef pair<int, int> ii;
const int N = 1e5 + 5;
const int M = 5e5 + 5;
const int inf = 1e9 + 7;
const long long INF = (long long)1e18 + 7;
const int mod = 1e9 + 7;
struct DSU {
int n;
vector<int> par;
vector<vector<int>> sz;
DSU(int _n = 0) {
n = _n;
par.assign(n + 5, -1);
sz.assign(n + 2, vector<int>());
for(int i = 1; i <= n; i++) sz[i].push_back(i);
}
int root(int v) {
return par[v] < 0 ? v : (par[v] = root(par[v]));
}
bool join(int x, int y) {
if (par[y] < par[x]) swap(x, y);
par[x] += par[y];
for(int p: sz[y]) sz[x].push_back(p);
sz[y].clear();
par[y] = x;
return true;
}
} dsu;
vector<ii> adj[N];
int n, m, q, numNpp;
vector<int> Npps, val;
int d[N];
pair<int, int> e[M];
vector<ii> queries[N];
void read() {
cin >> n >> m;
for(int i = 1; i <= m; i++) {
int u, v, c; cin >> u >> v >> c;
adj[u].push_back({v, c});
adj[v].push_back({u, c});
e[i] = {u, v};
}
cin >> numNpp;
Npps.assign(numNpp, 0);
for(int &i: Npps) cin >> i;
}
void calDistToNpp() {
priority_queue<ii, vector<ii>, greater<ii>> heap;
memset(d, 0x3f, sizeof(d));
for(int i: Npps) {
heap.push({0, i});
d[i] = 0;
}
while(!heap.empty()) {
ii tmp = heap.top(); heap.pop();
int u = tmp.se;
int du = tmp.fi;
if (du != d[u]) continue;
for(auto [v, c]: adj[u]) {
if (minimize(d[v], d[u] + c)) {
heap.push(mp(d[v], v));
}
}
}
}
int ans[N];
void solve() {
calDistToNpp();
cin >> q;
for(int i = 1; i <= q; i++) {
int u, v; cin >> u >> v;
queries[u].push_back({v, i});
queries[v].push_back({u, i});
}
sort(e + 1, e + m + 1, [&](const ii &x, const ii &y) {
return min(d[x.fi], d[x.se]) > min(d[y.fi], d[y.se]);
});
dsu = DSU(n);
for(int i = 1; i <= m; i++) {
int r1 = dsu.root(e[i].fi), r2 = dsu.root(e[i].se);
if (r1 == r2) continue;
int mn = min(d[e[i].fi], d[e[i].se]);
if (SZ(dsu.sz[r1]) > SZ(dsu.sz[r2])) swap(r1, r2);
for(int x: dsu.sz[r1]) {
for(auto [y, id]: queries[x]) if (ans[id] == 0) {
if (dsu.root(y) == r2) {
ans[id] = mn;
}
}
}
dsu.join(r1, r2);
}
for(int i = 1; i <= q; i++) cout << ans[i] << endl;
}
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if (fopen(TASK".inp", "r")) {
freopen(TASK".inp", "r", stdin);
freopen(TASK".out", "w", stdout);
}
int test = 1;
// cin >> test;
while(test--) {
read();
solve();
}
return 0;
}
// rmq - rmq2d
// hash
// fw - fw2d
// segtree
Compilation message (stderr)
net.cpp: In function 'int main()':
net.cpp:113:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
113 | freopen(TASK".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
net.cpp:114:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
114 | 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... |