이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ff first
#define ss second
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define int long long
template <class F, class _S>
bool chmin(F &u, const _S &v){
bool flag = false;
if ( u > v ){
u = v; flag |= true;
}
return flag;
}
template <class F, class _S>
bool chmax(F &u, const _S &v){
bool flag = false;
if ( u < v ){
u = v; flag |= true;
}
return flag;
}
const int N = 1e6+50, inf = 1e18+200;
//int mod = 998244353;
//int mod = 1000000007;
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//#define rnd(l, r) uniform_int_distribution <int> (l, r)(rng)
void solve(){
int n, m;
cin >> n >> m;
vector<vector<array<int, 2>> > g(n+1);
for (int i = 0; i < m; i++){
int a, b, w;
cin >> a >> b >> w;
g[a].pb({w, b});
g[b].pb({w, a});
}
vector<int> d(n+1, inf);
auto rere = [&]()->void{
int k;
cin >> k;
priority_queue<array<int, 2>> q;
for(int i = 0; i < k; i++){
int x;cin >> x;
q.push({0, x});
d[x] = 0;
}
while(q.size()){
auto [dis, ps] = q.top();
q.pop();
dis = -dis;
if(d[ps] < dis)continue;
for(auto [w, to]:g[ps])
if(chmin(d[to], d[ps] + w))
q.push({-d[to], to});
}
};rere();
auto dfs = [&](int s, int t) -> int{
priority_queue<array<int,2>> q;
q.push({d[s], s});
vector<int> dp(n+1, 0);
dp[s] = d[s];
while(q.size()){
auto [mx, ps] = q.top();
q.pop();
//cout << ps << ' ' << mx << '\n';
if(ps == t)return dp[ps];
if(mx < dp[ps])continue;
for(auto [w, to]:g[ps])
if(chmax(dp[to], min(d[to], dp[ps])))
q.push({dp[to], to});
}
return 0;
};
int q;
cin >> q;
while(q--){
int s, t;
cin >> s >> t;
int ans = dfs(s, t);
cout << ans << '\n';
}
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int tt = 1;
//cin >> tt;
while(tt--){
solve();
}
}
# | 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... |