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>
#define INF 1e18
#define fi first
#define se second
#define FOR(i, k, n) for(ll i = k; i <= n; i++)
#define FOR1(i, k, n) for(ll i = k; i >= n; i--)
#define pb push_back
#define fastio ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define vi vector<int>
#define pii pair<int, int>
#define vii vector<pii>
#define ll long long
#define vll vector<ll>
#define pll pair<ll, ll>
#define re return 0
#define mii map<int, int>
#define input "BAI1.inp"
#define output "BAI1.out"
#define rf freopen(input, "r", stdin); freopen(output, "w", stdout)
using namespace std;
const int maxn = 1e5 + 5;
const int mod = 1e9 + 7;
ll dp[maxn];
int n, m, par[maxn], sz[maxn], L[maxn], R[maxn], mid[maxn], kq[maxn];
bool vis[maxn];
vii adj[maxn];
vi event[maxn];
pii a[maxn];
void make_set()
{
FOR(i, 1, n)
{
sz[i] = 1;
par[i] = i;
}
}
int find(int u)
{
if(u == par[u])
return u;
return par[u] = find(par[u]);
}
void Union(int u, int v)
{
u = find(u);
v = find(v);
if(sz[u] > sz[v])
swap(u, v);
par[u] = v;
sz[v] += sz[u];
}
int main()
{
fastio;
cin >> n >> m;
FOR(i, 1, m)
{
int u, v, w;
cin >> u >> v >> w;
adj[u].pb({v, w});
adj[v].pb({u, w});
}
priority_queue<pll, vector<pll>, greater<pll>> Q;
FOR(i, 1, n)
dp[i] = INF;
int k;
cin >> k;
FOR(i, 1, k)
{
int u;
cin >> u;
dp[u] = 0;
Q.push({0, u});
}
while(!Q.empty())
{
int u = Q.top().se;
Q.pop();
if(vis[u])
continue;
vis[u] = 1;
for(auto x : adj[u])
{
int v = x.fi;
int w = x.se;
if(dp[v] > dp[u] + w)
{
dp[v] = dp[u] + w;
Q.push({dp[v], v});
}
}
}
int q;
cin >> q;
FOR(i, 1, q)
cin >> a[i].fi >> a[i].se;
vii vv;
FOR(i, 1, n)
vv.pb({dp[i], i});
sort(vv.begin(), vv.end());
FOR(i, 1, q)
{
L[i] = 0;
R[i] = n - 1;
}
while(1)
{
make_set();
bool kt = 0;
FOR(i, 1, n)
event[i].clear();
FOR(i, 1, q)
{
if(L[i] > R[i])
continue;
kt = 1;
mid[i] = (L[i] + R[i]) / 2;
event[mid[i]].pb(i);
}
if(kt == 0)
break;
FOR1(i, vv.size() - 1, 0)
{
int u = vv[i].se;
for(auto x : adj[u])
{
int v = x.fi;
if(dp[v] < dp[u])
continue;
if(find(u) != find(v))
Union(u, v);
}
for(auto id : event[i])
{
if(find(a[id].fi) == find(a[id].se))
{
kq[id] = i;
L[id] = mid[id] + 1;
}
else
R[id] = mid[id] - 1;
}
}
}
FOR(i, 1, q)
{
cout << vv[kq[i]].fi << "\n";
}
re;
}
# | 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... |