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>
#include <ext/pb_ds/assoc_container.hpp>
#define lft(x) x * 2
#define rgt(x) x * 2 + 1
#define tm hui_pizda
#define ft first
#define sd second
#define pb push_back
#define pf push_front
#define sz size()
#define cnt continue
#define m_p make_pair
#define fr(i, l, r) for(int i = l; i <= r; ++ i)
#define rf(i, r, l) for(int i = r; i >= l; -- i)
#define all(x) x.begin(), x.end()
#pragma GCC optimize(-O3)
#pragma GCC optimize(Ofast)
#pragma GCC optimize("unroll-loops")
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using _set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long ll;
typedef long double ld;
typedef pair <ll, ll> pll;
typedef pair <int, int> pii;
typedef vector <int> vi;
typedef vector <ll> vl;
typedef vector <pii> vpi;
typedef vector <pll> vpl;
mt19937_64 rnd(time(NULL));
const ll N = 1e6;
const ll mtrxN = 10;
const ll oo = 1e18 + 10;
const ll B = 500;
const ll mod = 1e9 + 7;
struct mtrx{
ll m[mtrxN][mtrxN] = {};
};
mtrx mtrx_mult(mtrx a, mtrx b){
mtrx c;
fr(i, 0, mtrxN - 1){
fr(j, 0, mtrxN - 1){
ll sum = 0;
fr(x, 0, mtrxN - 1){
sum += a.m[i][x] * b.m[x][j];
sum %= mod;
}
c.m[i][j] = sum;
}
}
return c;
}
mtrx mtrx_pow(mtrx a, ll n){
mtrx res;
fr(i, 0, mtrxN - 1)fr(j, 0, mtrxN - 1)res.m[i][j] = a.m[i][j];
n --;
while(n){
if(n&1)res = mtrx_mult(res, a);
a = mtrx_mult(a, a);
n >>= 1;
}
return res;
}
ll _pow(ll a, ll n){
ll r = 1;
while(n){
if(n&1)r = r * a % mod;
a = a * a % mod;
n >>= 1;
}
return r;
}
ll div(ll x, ll y, ll md){
return x * _pow(y, md - 2) % md;
}
ll x[N], y[N], m[N], l[N], r[N], p[N], p2[N], usd[N], pp[N], dp[N];
ll n, Q;
vl cnm[N], gr[N];
vpl g[N];
bool cmp(ll x, ll y){
return m[x] > m[y];
}
bool cmp2(ll x, ll y){
return dp[x] > dp[y];
}
void add(ll x, ll y){
if(pp[x] != pp[y]){
ll xx = pp[x], yy = pp[y];
if(cnm[xx].sz > cnm[yy].sz)swap(xx, yy), swap(x, y);
for(auto i : cnm[xx]){
cnm[yy].pb(i);
pp[i] = yy;
}
cnm[xx].clear();
}
}
int main(){
ll M;
cin >> n >> M;
fr(i, 1, M){
ll x, y, z;
cin >> x >> y >> z;
g[x].pb({y, z});
g[y].pb({x, z});
gr[x].pb(y);
gr[y].pb(x);
}
ll k;
cin >> k;
set <pll> s;
fr(i, 1, n)dp[i] = oo;
fr(i, 1, k){
ll x;
cin >> x;
s.insert({0, x});
dp[x] = 0;
}
while(s.sz){
ll x = (*s.begin()).ft, y = (*s.begin()).sd;
s.erase(s.begin());
for(auto i : g[y]){
if(dp[i.ft] > x + i.sd){
s.erase({dp[i.ft], i.ft});
dp[i.ft] = x + i.sd;
s.insert({dp[i.ft], i.ft});
}
}
}
fr(i, 1, n)p2[i] = i;
sort(p2 + 1, p2 + 1 + n, cmp2);
cin >> Q;
fr(i, 1, Q)cin >> x[i] >> y[i], l[i] = 0, r[i] = 1e8;
fr(e, 0, 30){
fr(i, 1, n){
usd[i] = 0, pp[i] = i;
cnm[i].clear(), cnm[i].pb(i);
}
fr(i, 1, Q)
m[i] = (l[i] + r[i]) / 2, p[i] = i;
sort(p + 1, p + 1 + Q, cmp);
ll now = 1;
fr(i, 1, Q){
while(now <= n && dp[p2[now]] >= m[p[i]]){
for(auto j : gr[p2[now]])
if(usd[j])add(j, p2[now]);
usd[p2[now]] = 1;
now ++;
}
if(pp[x[p[i]]] != pp[y[p[i]]])r[p[i]] = m[p[i]] - 1;
else l[p[i]] = m[p[i]] + 1;
}
}
fr(i, 1, Q)cout << m[i] << "\n";
return 0;
}
/*
*/
Compilation message (stderr)
plan.cpp:18:22: warning: '#pragma GCC optimize' is not a string or number [-Wpragmas]
#pragma GCC optimize(-O3)
^
plan.cpp:19:22: warning: '#pragma GCC optimize' is not a string or number [-Wpragmas]
#pragma GCC optimize(Ofast)
^~~~~
# | 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... |