이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//IZHO 2018 P2
#include <bits/stdc++.h>
#define INF INT_MAX
#define MOD 100000007
#define MAX_N 100000
#define MAX_M 100000
#define MAX_Q 100000
using namespace std;
struct edge{
int d;
int w;
};
struct dj{
int v;
int p;
public:
bool operator<(const dj& rhs) const{
return p>rhs.p;//Jer priroty queue vraca obrnuto
}
};
int N, M;
vector<edge> adj[MAX_N];
int height[MAX_N];
int query[MAX_N][2];
int K, Q;
bool npp[MAX_N];
void input(){
cin>>N>>M;
for(int i=0; i<M; ++i){
int a, b, w;
cin>>a>>b>>w;
a--;
b--;
edge ea, eb;
ea.d=b;
eb.d=a;
ea.w=eb.w=w;
adj[a].push_back(ea);
adj[b].push_back(eb);
}
cin>>K;
memset(npp, 0, sizeof(npp));
for(int i=0; i<K; ++i){
int g;
cin>>g;
g--;
npp[g]=1;
}
cin>>Q;
for(int i=0; i<Q; ++i){
int s, t;
cin>>s>>t;
query[i][0]=s-1;
query[i][1]=t-1;
}
}
priority_queue<dj> pq;
void relax(int v){
int d=adj[v].size();
for(int i=0; i<d; ++i){
int u=adj[v][i].d;
int l=adj[v][i].w;
if(height[u]>height[v]+l){
height[u]=height[v]+l;
dj un;
un.p=height[u];
un.v=u;
pq.push(un);
}
}
}
void find_height(){
memset(height, INF, sizeof(height));
for(int i=0; i<N; ++i){
if(npp[i]){
height[i]=0;
}
else height[i]=INF;
}
for(int i=0; i<N; ++i){
if(npp[i])relax(i);
}
while(!pq.empty()){
int v=pq.top().v;
pq.pop();
if(npp[v])continue;
relax(v);
npp[v]=1;
}
}
int main(){
ios::sync_with_stdio(false);
input();
find_height();
for(int i=0; i<Q; ++i){
cout<<min(height[query[i][0]], height[query[i][1]])<<endl;
}
return 0;
}
/*
9 12
1 9 4
1 2 5
2 3 7
2 4 3
4 3 6
3 6 4
8 7 10
6 7 5
5 8 1
9 5 7
5 4 12
6 8 2
2
4 7
5
1 6
5 3
4 8
5 8
1 5
*/
# | 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... |