이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<iostream>
#include<stdio.h>
#include<math.h>
#include<map>
#include<string>
#include<algorithm>
#include<vector>
#include<string.h>
#include<utility>
#include<set>
#include<cmath>
#include<queue>
#include<deque>
#include<functional>
#include<stack>
#include<limits.h>
#include<iomanip>
#include<unordered_map>
#include<numeric>
#include<tuple>
#include<bitset>
using namespace std;
#define MOD 1000000007
#define int long long
#define ss second
#define ff first
#define endl '\n'
typedef pair<int,int> pp;
signed main(){
// freopen("file.in", "r", stdin);
// freopen("file.out", "w", stdout);
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n,m; cin>>n>>m;
vector<pp> v[n+1];
for(int i=1; i<=m; i++){
int a,b,w; cin>>a>>b>>w;
v[a].push_back({b,w});
v[b].push_back({a,w});
}
int k; cin>>k;
vector<int> dp(n+1,1e9),mx(n+1);
priority_queue<pp,vector<pp>,greater<pp> > q;
for(int i=1; i<=k; i++){
int g; cin>>g;
q.push({0,g});
dp[g]=0;
}
vector<bool> vis(n+1);
while(!q.empty()){
int w=q.top().ff;
int c=q.top().ss;
q.pop();
vis[c]=1;
if(w<=dp[c]){
for(auto node: v[c]){
if(!vis[node.ff]&&w+node.ss<dp[node.ff]){
dp[node.ff]=w+node.ss;
q.push({w+node.ss,node.ff});
}
}
}
}
int qry; cin>>qry;
if(qry<=1000){
while(qry--){
int s,t; cin>>s>>t;
for(int i=1; i<=n; i++) vis[i]=0;
for(int i=1; i<=n; i++) mx[i]=-1;
priority_queue<pp> e;
e.push({dp[s],s});
mx[s]=dp[s];
while(!e.empty()){
int w=e.top().ff;
int c=e.top().ss;
if(c==t){
cout<<w<<endl;
break;
}
e.pop();
vis[c]=1;
if(w>=mx[c]){
for(auto node: v[c]){
if(!vis[node.ff]&&min(dp[node.ff],w)>mx[node.ff]){
mx[node.ff]=min(dp[node.ff],w);
e.push({min(dp[node.ff],w),node.ff});
}
}
}
}
}
}else{
while(qry--){
int s,t; cin>>s>>t;
cout<<min(dp[s],dp[t])<<endl;
}
}
return 0;
}
# | 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... |