Submission #1098251

#TimeUsernameProblemLanguageResultExecution timeMemory
1098251vjudge1Evacuation plan (IZhO18_plan)C++17
10 / 100
4034 ms27468 KiB
//UNSTOPPABLE
#include "bits/stdc++.h"
#include <ext/pb_ds/assoc_container.hpp>
#define ll long long
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define int long long
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
#define pii pair<int,int>
#define sigma signed
#define tpii pair <pair <int,int> , int>
#define bruh cout << "NO\n"
using namespace std;
using namespace __gnu_pbds;
const int N = 5e5 + 5;
int mod = 1e9 + 7;
const int INF = 1e18;
int n,m,k,q,a[N],dist[N],used[N];
vector <pii> g[N];
void Goldik(){
	cin >> n >> m;
	for(int i = 1 ; i <= m ; i++){
		int u,v,w;
		cin >> u >> v >> w;
		g[u].pb({v , w});
		g[v].pb({u , w});
	}
	cin >> k;
	for(int i = 1 ; i <= k ; i++){
		cin >> a[i];
	}
	cin >> q;
	for(int i = 1 ; i <= q ; ++i){
		int u,v;
		cin >> u >> v;
		for(int i = 1 ; i <= n ; i++){
			dist[i] = INF;
			used[i] = 0;
		}
		set <pii> st;
		st.insert({0 , u});
		dist[u] = 0;
		used[u] = 1;
		while(st.size()){
			int x = (*st.begin()).S;
			st.erase(*st.begin());
			used[x] = 1;
			for(auto to : g[x]){
				if(used[to.F]) continue;
				if(dist[to.F] > dist[x] + to.S){
					dist[to.F] = dist[x] + to.S;
					st.insert({dist[to.F] , to.F});
				}
			}
		} 
		int mn = INF;
		for(int i = 1 ; i <= k ; i++){
			mn = min(mn , dist[a[i]]);
		}
		for(int i = 1 ; i <= n ; i++){
			dist[i] = INF;
			used[i] = 0;
		}
		st.clear();
		st.insert({0 , v});
		dist[v] = 0;
		used[v] = 1;
		while(st.size()){
			int x = (*st.begin()).S;
			st.erase(*st.begin());
			used[x] = 1;
			for(auto to : g[x]){
				if(used[to.F]) continue;
				if(dist[to.F] > dist[x] + to.S){
					dist[to.F] = dist[x] + to.S;
					st.insert({dist[to.F] , to.F});
				}
			}
		} 
		for(int i = 1 ; i <= k ; i++){
			mn = min(mn , dist[a[i]]);
		}
		cout << mn << '\n';
	}
}
//rewai mnogo zadach vozmozhno odna iz nih gde to popadetsya
//returning winter prime?
//chem prowe tem luchshe
sigma main(/*AZ AZDAN UZDIKSIZ*/){
	//freopen("txt.in","r",stdin);
	//freopen("txt.out","w",stdout);
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	srand(time(0));
	int TT = 1;
	// cin >> TT;
	for(int i = 1 ; i <= TT ; i++){
		//cout << "Case " << i << ": ";
		Goldik();
	}
}
#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...