This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//Make CSP great again
//Vengeance
#include <bits/stdc++.h>
#define TASK "TESTCODE"
#define Log2(x) 31 - __builtin_clz(x)
using namespace std;
const int N = 1e5;
priority_queue< pair<int, int>, deque<pair<int, int> > , greater<pair<int, int> > > PQ;
vector<pair<int, int> > Adj[N + 1];
int n, m, d[N + 1], res[N + 1], lab[N + 1], q;
set<int> dp[N + 1];
vector<int> id = {0};
int cnt[N + 1];
void read()
{
cin >> n >> m;
for (int i = 1; i <= m; ++ i)
{
int u, v, w;
cin >> u >> v >> w;
Adj[u].push_back({v, w});
Adj[v].push_back({u, w});
}
memset(d, 0x3f, sizeof(d));
memset(lab, -1, sizeof(lab));
int k;
cin >> k;
for (int i = 1; i <= k; ++ i)
{
int u;
cin >> u;
d[u] = 0;
PQ.push({0, u});
}
cin >> q;
for (int i = 1; i <= q; ++ i)
{
int u, v;
cin >> u >> v;
dp[u].insert(i);
dp[v].insert(i);
}
}
int FindSet(int u)
{
return lab[u] < 0 ? u : lab[u] = FindSet(lab[u]);
}
void Unite(int r, int s, int w)
{
r = FindSet(r);
s = FindSet(s);
if (r == s)
{
return ;
}
if (lab[r] < lab[s])
{
swap(r, s);
}
lab[r] += lab[s];
lab[s] = r;
if (dp[r].size() < dp[s].size())
{
swap(dp[r], dp[s]);
}
for (int i : dp[s])
{
if (dp[r].find(i) != dp[r].end())
{
dp[r].erase(i);
res[i] = w;
}
else
{
dp[r].insert(i);
}
}
}
void Dijkstra()
{
while(!PQ.empty())
{
pair<int, int> p = PQ.top();
PQ.pop();
if (d[p.second] != p.first)
{
continue;
}
id.push_back(p.second);
int u = p.second;
for (auto p : Adj[u])
{
int v = p.first, w = p.second;
if (d[v] > d[u] + w)
{
d[v] = d[u] + w;
PQ.push({d[v], v});
}
}
}
}
void solve()
{
Dijkstra();
for (int i = n; i >= 1; -- i)
{
int u = id[i];
for (auto p : Adj[u])
{
int v = p.first;
if (d[v] >= d[u])
{
Unite(u, v, d[u]);
}
}
}
for (int i = 1; i <= q; ++ i)
{
cout << res[i] << '\n';
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
if (fopen(TASK".INP", "r"))
{
freopen(TASK".INP", "r", stdin);
//freopen(TASK".OUT", "w", stdout);
}
int t = 1;
bool typetest = false;
if (typetest)
{
cin >> t;
}
for (int __ = 1; __ <= t; ++ __)
{
//cout << "Case " << __ << ": ";
read();
solve();
}
}
Compilation message (stderr)
plan.cpp: In function 'int main()':
plan.cpp:128:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
128 | freopen(TASK".INP", "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... |