Submission #1155748

#TimeUsernameProblemLanguageResultExecution timeMemory
1155748mertbbmEvacuation plan (IZhO18_plan)C++20
100 / 100
517 ms99012 KiB
#include <bits/stdc++.h>
using namespace std;
 
#define int long long 
#define ld long double
#define show(x,y) cout << y << " " << #x << endl;
#define show2(x,y,i,j) cout << y << " " << #x << "  " << j << " " << #i << endl;
#define show3(x,y,i,j,p,q) cout << y << " " << #x << "  " << j << " " << #i << "  " << q << " " << #p << endl;
#define show4(x,y) for(auto it:y) cout << it << " "; cout << #x << endl;
typedef pair<int,int>pii;
typedef pair<pii,int>pi2;
mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());
	
struct DSU{
	vector<int>e;
	void init(int n){
		e=vector<int>(n,-1);
	}
	
	int get(int x){
		return e[x]<0?x:e[x]=get(e[x]);
	}
	
	bool unite(int x, int y){
		x=get(x); y=get(y);
		if(x==y) return 0;
		if(e[x]>e[y]) swap(x,y);
		e[x]+=e[y];
		e[y]=x;
		return 1;
	}
};

void solve(){
	int n,m;
	cin >> n >> m;
	
	vector<pii>adj[n+5];
	int temp,temp2,temp3;
	
	for(int x=0;x<m;x++){
		cin >> temp >> temp2 >> temp3;
		adj[temp].push_back({temp2,temp3});
		adj[temp2].push_back({temp,temp3});
	}
	
	int k;
	cin >> k;
	
	int dist[n+5];
	memset(dist,-1,sizeof(dist));
	priority_queue<pii,vector<pii>,greater<pii>>pq;
	
	for(int x=0;x<k;x++){
		cin >> temp;
		dist[temp]=0;
		pq.push({0,temp});
	}
	
	while(!pq.empty()){
		pii cur=pq.top();
		pq.pop();
		
		int index=cur.second;
		int d=cur.first;
		if(dist[index]!=d) continue;
		
		for(auto it:adj[index]){
			int nx=it.first;
			int nd=d+it.second;
			if(dist[nx]!=-1&&dist[nx]<=nd) continue;
			dist[nx]=nd;
			pq.push({nd,nx});
		}
	}
	
	vector<int>v;
	vector<pii>v2;
	
	for(int x=1;x<=n;x++){
		v.push_back(dist[x]);
		v2.push_back({dist[x],x});
		//cout << x << " " << dist[x] << " *\n";
	}
	
	sort(v.begin(),v.end());
	sort(v2.begin(),v2.end());
	v.resize(unique(v.begin(),v.end())-v.begin());
	
	//show4(v,v);
	
	int sz=v.size();
	
	vector<array<int,5>>storage[sz][2];
	
	int q;
	cin >> q;
	
	int ans[q];
	
	for(int x=0;x<q;x++){
		cin >> temp >> temp2;
		storage[sz/2][0].push_back({0,sz-1,temp,temp2,x});
		ans[x]=0;
	}
	
	//show(bruh,v[sz/2]);
	
	for(int x=0;x<22;x++){
		DSU dsu;
		dsu.init(n+5);
		bool visited[n+5];
		memset(visited,0,sizeof(visited));
		
		int ptr=v2.size()-1;
		for(int y=sz-1;y>=0;y--){
			//show(v[y],v[y]);
			while(ptr>=0&&v2[ptr].first>=v[y]){
				int index=v2[ptr].second;
				visited[index]=true;
				//show(index,index);
				for(auto it:adj[index]){
					if(visited[it.first]){
						dsu.unite(it.first,index);
						//show2(it.first,it.first,index,index);
					}
				}
				ptr--;
			}
		
			for(auto it:storage[y][x%2]){
				int l=it[0];
				int r=it[1];
				//cout << it[2] << " " << it[3] << " " << v[y] << " ";
				if(dsu.get(it[2])==dsu.get(it[3])){
					//cout << "ok\n"; 
					ans[it[4]]=v[y];
					l=y+1;
					if(l<=r) storage[(l+r)/2][(x+1)%2].push_back({l,r,it[2],it[3],it[4]});
				}
				else{
					//cout << "no\n";
					r=y-1;
					if(l<=r) storage[(l+r)/2][(x+1)%2].push_back({l,r,it[2],it[3],it[4]});
				}
			}
			storage[y][x%2].clear();
		}
	}
	
	for(int x=0;x<q;x++){
		cout << ans[x] << "\n";
	}
}

int32_t main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	//freopen("in.txt","r",stdin);
	//freopen("in.txt","w",stdout);
	int t=1;	        
	//cin >> t;	
	while(t--){
		solve(); 
	}
}
#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...