이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
*/
using namespace std;
#pragma GCC optimize ("unroll-loops,Ofast,O3")
#pragma GCC target("avx,avx2,fma")
#define F first
#define S second
#define sz(x) (int)x.size()
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define NFS ios_base::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) ;
#define file(s) if (fopen(s".in", "r")) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
//#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
typedef long long ll;
const int E = 1e6+777;
const long long inf = 1e18+777;
const int N = 1e5+777;
const int MOD = 1e9+7;
int n, m;
vector<pair<int,int>> g[N];
vector<int> d(N, INT_MAX);
int get(int _start, int _end){
vector<int> res(n+1,-INT_MAX);
res[_start] = d[_start];
set<pair<int,int>> st = {{-res[_start], _start}};
while(!st.empty()){
int v = st.begin() -> S;
st.erase(st.begin());
for(auto to : g[v]){
if(res[to.F] < min(res[v], d[to.F])){
st.erase({-res[to.F], to.F});
res[to.F] = min(res[v], d[to.F]);
st.insert({-res[to.F],to.F});
}
}
}
return res[_end];
}
set<pair<int,int>> st;
inline void go(){
while(!st.empty()){
int v = st.begin() -> S;
st.erase(st.begin());
for(auto to : g[v]){
if(d[to.F] > d[v] + to.S){
st.erase({d[to.F], to.F});
st.insert({d[to.F]=d[v]+to.S,to.F});
}
}
}
}
unordered_map<int,bool> used[N];
int main(){auto solve=[&](){
cin >> n >> m;
for(int i = 1; i <= m; ++i){
int x, y, w;
cin >> x >> y >> w;
g[x].pb({y, w});
g[y].pb({x, w});
used[x][y] = true;
used[y][x] = true;
}
int k; cin >> k;
for(int i = 1, v; i <= k; ++i){
cin >> v;
st.insert({d[v]=0,v});
}
go();
int q; cin >> q;
for(int s, e; q--;){
cin >> s >> e;
if(used[s].find(e) != used[s].end()){
cout << min(d[s], d[e]) << '\n';
}else{
cout << get(s, e) << '\n';
}
}
};NFS;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... |