This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
// is short or still long ???
hollwo_pelw's template(short)
// Note : -Dhollwo_pelw_local
*/
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/trie_policy.hpp>
// #include <ext/rope>
using namespace std;
// using namespace __gnu_pbds;
// using namespace __gnu_cxx;
void FAST_IO(string filein = "", string fileout = "", string fileerr = ""){
if (fopen(filein.c_str(), "r")){
freopen(fileout.c_str(), "w", stdout);
freopen(filein.c_str(), "r", stdin);
#ifdef hollwo_pelw_local
freopen(fileerr.c_str(), "w", stderr);
#endif
}
cin.tie(0), cout.tie(0) -> sync_with_stdio(0);
}
void Hollwo_Pelw();
signed main(){
#ifdef hollwo_pelw_local
FAST_IO("input.inp", "output.out", "error.err");
auto start = chrono::steady_clock::now();
#else
FAST_IO("hollwo_pelw.inp", "hollwo_pelw.out");
#endif
int testcases = 1;
// cin >> testcases;
for (int test = 1; test <= testcases; test++){
// cout << "Case #" << test << ": ";
Hollwo_Pelw();
}
#ifdef hollwo_pelw_local
auto end = chrono::steady_clock::now();
cout << endl;
cout << "Excution time : " << chrono::duration_cast<chrono::milliseconds> (end - start).count() << "[ms]" << endl;
#endif
return 0;
}
const int N = 1e5 + 5, inf = 2e9;
vector<pair<int, int>> adj[N];
int n, m, q, dis[N], res[N];
struct data_t {
int d, u;
data_t (int _d = 0, int _u = 0)
: d(_d), u(_u) {}
bool operator < (const data_t &o) const {
return d > o.d;
}
};
priority_queue<data_t> pq;
int par[N], vis[N];
set<int> query[N];
inline int find(int u) {
return par[u] == u ? u : par[u] = find(par[u]);
}
inline void merge(int u, int v, int w) {
if ((u = find(u)) == (v = find(v)))
return ;
if (query[u].size() < query[v].size())
swap(u, v);
for (auto x : query[v]) {
if (query[u].count(x)) {
res[x] = w;
query[u].erase(x);
} else {
query[u].insert(x);
}
}
par[v] = u;
}
void Hollwo_Pelw() {
cin >> n >> m;
for (int u, v, w; m --; ) {
cin >> u >> v >> w;
adj[u].emplace_back(v, w);
adj[v].emplace_back(u, w);
}
fill(dis + 1, dis + n + 1, inf);
cin >> m;
for (int u; m --; )
cin >> u, pq.emplace(dis[u] = 0, u);
while (!pq.empty()) {
int u = pq.top().u, d = pq.top().d; pq.pop();
if (dis[u] != d) continue ;
for (auto ed : adj[u]) {
int v = ed.first, w = ed.second;
if (dis[v] > d + w)
pq.emplace(dis[v] = d + w, v);
}
}
cin >> q;
for (int i = 1, s, t; i <= q; i++) {
cin >> s >> t;
query[s].insert(i);
query[t].insert(i);
}
iota(par + 1, par + n + 1, 1);
vector<pair<int, int>> order;
for (int i = 1; i <= n; i++)
order.emplace_back(dis[i], i);
sort(order.rbegin(), order.rend());
for (auto it : order) {
int u = it.second;
vis[u] = 1;
for (auto ed : adj[u]) {
int v = ed.first;
if (vis[v]) merge(u, v, it.first);
}
}
for (int i = 1; i <= q; i++)
cout << res[i] << '\n';
}
Compilation message (stderr)
plan.cpp: In function 'void FAST_IO(std::string, std::string, std::string)':
plan.cpp:18:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
18 | freopen(fileout.c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
plan.cpp:19:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
19 | freopen(filein.c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |