This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// I_Love_LPL
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
const int N = 1e5 + 50;
long long mod = 1e9 + 7;
const int lim = 700;
const int lg = 18;
const int base = 311;
const long double eps = 1e-6;
vector<pair<ll, ll>> node[N];
struct viet{
ll tt, id, type;
bool operator <(const viet &o) const{
if (tt == o.tt) return type < o.type;
return tt > o.tt;
}
};
ll d[N], l[N], h[N], md[N], p[N], s[N], t[N], ans[N];
ll find_set(ll u){
if (p[u] < 0) return u;
return p[u] = find_set(p[u]);
}
void dsu(ll u, ll v){
u = find_set(u);
v = find_set(v);
if (u == v) return;
if (p[u] > p[v]) swap(u, v);
p[u] += p[v];
p[v] = u;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
if (fopen("tests.inp", "r")){
freopen("test.inp", "r", stdin);
freopen("test.out", "w", stdout);
}
ll n, m;
cin >> n >> m;
for (int i = 1; i <= m; i++){
ll u, v, c;
cin >> u >> v >> c;
node[u].push_back({v, c});
node[v].push_back({u, c});
}
ll k;
cin >> k;
for (int i = 1; i <= n; i++) d[i] = 1e18;
priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq;
for (int i = 1; i <= k; i++){
ll u;
cin >> u;
d[u] = 0;
pq.push({0, u});
}
while (pq.size()){
pair<ll, ll> x = pq.top();
pq.pop();
if (d[x.second] != x.first) continue;
for (auto j : node[x.second]){
if (d[j.first] > (j.second + x.first)){
d[j.first] = j.second + x.first;
pq.push({d[j.first], j.first});
}
}
}
ll q;
cin >> q;
for (int i = 1; i <= q; i++){
cin >> s[i] >> t[i];
l[i] = 0, h[i] = 1e9;
}
while (true){
bool flag = false;
vector<viet> Q;
for (int i = 1; i <= q; i++){
if (l[i] > h[i]) continue;
md[i] = (l[i] + h[i]) >> 1;
flag = true;
Q.push_back({md[i], i, 1});
}
if (!flag) break;
for (int i = 1; i <= n; i++){
Q.push_back({d[i], i, 0});
}
memset(p, -1, sizeof(p));
sort(Q.begin(), Q.end());
for (auto j : Q){
if (j.type == 0){
for (auto to : node[j.id]){
if (d[to.first] >= d[j.id]) dsu(to.first, j.id);
}
} else {
ll id = j.id;
if (find_set(s[id]) == find_set(t[id])) ans[id] = 1;
else ans[id] = 0;
}
}
for (int i = 1; i <= q; i++){
if (ans[i] == 1) l[i] = md[i] + 1;
else h[i] = md[i] - 1;
}
}
for (int i = 1; i <= q; i++) cout << h[i] << endl;
}
/*
9 12
1 9 4
1 2 5
2 3 7
2 4 3
4 3 6
3 6 4
8 7 10
6 7 5
5 8 1
9 5 7
5 4 12
6 8 2
2
4 7
5
1 6
5 3
4 8
5 8
1 5
Ans:
Out:
*/
Compilation message (stderr)
plan.cpp: In function 'int main()':
plan.cpp:37:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
37 | freopen("test.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
plan.cpp:38:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
38 | freopen("test.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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |