Submission #99904

#TimeUsernameProblemLanguageResultExecution timeMemory
99904aleksamEvacuation plan (IZhO18_plan)C++14
23 / 100
1415 ms19620 KiB
//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<N; ++i){
		//printf("height[%d]=%d\n", i, height[i]);
	}
	for(int i=0; i<Q; ++i){
		int s=height[query[i][0]];
		int t=height[query[i][1]];
		if(s>t)cout<<t<<endl;
		else cout<<s<<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

5 5
1 2 2
5 1 1
4 5 1
3 2 3
3 4 4
1
5
5
5 1
1 2
2 3
4 3
5 4
*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...