結果

問題 No.1442 I-wate Shortest Path Problem
ユーザー 沙耶花沙耶花
提出日時 2021-03-26 22:28:57
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 367 ms / 3,000 ms
コード長 4,043 bytes
コンパイル時間 5,462 ms
コンパイル使用メモリ 286,664 KB
実行使用メモリ 34,664 KB
最終ジャッジ日時 2023-08-19 09:01:34
合計ジャッジ時間 11,517 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 5 ms
4,380 KB
testcase_03 AC 34 ms
4,380 KB
testcase_04 AC 5 ms
4,380 KB
testcase_05 AC 3 ms
4,384 KB
testcase_06 AC 34 ms
4,380 KB
testcase_07 AC 4 ms
4,380 KB
testcase_08 AC 29 ms
4,380 KB
testcase_09 AC 9 ms
4,576 KB
testcase_10 AC 36 ms
4,376 KB
testcase_11 AC 32 ms
4,376 KB
testcase_12 AC 228 ms
23,000 KB
testcase_13 AC 123 ms
19,136 KB
testcase_14 AC 166 ms
20,788 KB
testcase_15 AC 159 ms
21,052 KB
testcase_16 AC 224 ms
23,644 KB
testcase_17 AC 346 ms
27,944 KB
testcase_18 AC 364 ms
27,772 KB
testcase_19 AC 239 ms
24,300 KB
testcase_20 AC 353 ms
27,836 KB
testcase_21 AC 367 ms
27,596 KB
testcase_22 AC 118 ms
29,744 KB
testcase_23 AC 233 ms
34,664 KB
testcase_24 AC 92 ms
19,592 KB
testcase_25 AC 213 ms
23,988 KB
testcase_26 AC 97 ms
22,080 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace atcoder;
using mint = modint998244353;
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf 1000000000000000000
vector<vector<pair<int,long long>>> E;

vector<long long> get(vector<int> ind){
	priority_queue<pair<long long,int>,vector<pair<long long,int>>,greater<pair<long long,int>>> Q;
	vector<long long> dis(E.size(),Inf);
	rep(i,ind.size()){
		Q.emplace(0,ind[i]);
		dis[ind[i]] = 0;
	}
	while(Q.size()>0){
		long long D = Q.top().first;
		int u = Q.top().second;
		Q.pop();
		if(dis[u]!=D)continue;
		rep(i,E[u].size()){
			long long D2 = E[u][i].second;
			int v = E[u][i].first;
			if(dis[v]<=D+D2)continue;
			dis[v] = D+D2;
			Q.emplace(dis[v],v);	
		}
	}
	return dis;
}

struct HLD{
	vector<int> sz,parent,depth,root,pos;
	vector<int> arr;
	HLD(vector<vector<int>> &E){
		sz.resize(E.size(),1);
		parent.resize(E.size(),0);
		depth.resize(E.size(),0);
		root.resize(E.size(),0);
		pos.resize(E.size(),0);
		
		dfs(0,-1,E);
		dfs2(0,-1,E,0);
	}
	
	void dfs(int now,int p,vector<vector<int>> &E){
		parent[now] = p;
		if(p==-1){
			depth[now] = 0;
		}
		else{
			depth[now] = depth[p]+1;
		}
		for(int i=0;i<E[now].size();i++){
			int to = E[now][i];
			if(to==p)continue;
			dfs(to,now,E);
			sz[now] += sz[to];
		}
	}
	
	void dfs2(int now,int p,vector<vector<int>> &E,int r){
		pos[now] = arr.size();
		arr.push_back(now);
		root[now] = r;
		int maxi = 0;
		int ind = -1;
		for(int i=0;i<E[now].size();i++){
			int to = E[now][i];
			if(to==p)continue;
			if(maxi<sz[to]){
				maxi = sz[to];
				ind = to;
			}
		}
		if(ind==-1)return;
		dfs2(ind,now,E,r);
		for(int i=0;i<E[now].size();i++){
			int to = E[now][i];
			if(to==p||to==ind)continue;
			dfs2(to,now,E,to);
		}
	}
	
	vector<pair<int,int>> query(int u,int v){
		vector<pair<int,int>> ret;
		int t = 0;
		while(root[u]!=root[v]){
			if(depth[root[u]] <= depth[root[v]]){
				ret.insert(ret.begin()+t,{pos[root[v]], pos[v]});
				v = parent[root[v]];
			}
			else{
				ret.insert(ret.begin()+t,{pos[u],pos[root[u]]});
				u = parent[root[u]];
				t++;
			}
		}
		ret.insert(ret.begin()+t,{pos[u],pos[v]});
		return ret;
	}
	
	int lca(int u,int v){
		for(;;v=parent[root[v]]){
			if(pos[u]>pos[v])swap(u,v);
			if(root[u]==root[v])return u;
		}
	}
	
	int get_distance(int u,int v){
		return depth[u] + depth[v] - 2 * depth[lca(u,v)];
	}
	
};



int main(){
	
	int N,K;
	cin>>N>>K;
	vector<vector<int>> e(N);
	E.resize(N);
	rep(i,N-1){
		int a,b,c;
		scanf("%d %d %d",&a,&b,&c);
		a--;b--;
		E[a].emplace_back(b,c);
		E[b].emplace_back(a,c);
		e[a].push_back(b);
		e[b].push_back(a);
	}
	
	vector<int> M(K);
	vector<long long> P(K);
	vector<vector<int>> X(K);
	rep(i,K){
		scanf("%d %lld",&M[i],&P[i]);
		X[i].resize(M[i]);
		rep(j,M[i]){
			scanf("%d",&X[i][j]);
			X[i][j]--;
		}
	}
	//cout<<'a'<<endl;
	vector<vector<long long>> dis(K);
	rep(i,K){
		dis[i] = get(X[i]);
	}
	//cout<<'a'<<endl;
	vector d(K*2,vector<long long>(K*2,Inf));
	rep(i,K*2)d[i][i] = 0LL;
	//cout<<'a'<<endl;
	rep(i,K){
		d[i*2][i*2+1] = P[i];
	}
	//cout<<'a'<<endl;
	rep(i,K){
		rep(j,K){
			int x = i*2+1;
			int y = j*2;
			long long mini = Inf;
			rep(k,X[j].size()){
				mini = min(mini,dis[i][X[j][k]]);
			}
			d[x][y] = mini;
		}
	}
	//cout<<'a'<<endl;
	rep(i,K*2){
		rep(j,K*2){
			rep(k,K*2){
				d[j][k] = min(d[j][k],d[j][i]+d[i][k]);
			}
		}
	}
	//cout<<'a'<<endl;
	vector<long long> D(N,Inf);
	D[0] = 0;
	{
		queue<int> Q;
		Q.push(0);
		while(Q.size()>0){
			int u = Q.front();
			Q.pop();
			rep(i,E[u].size()){
				int v = E[u][i].first;
				long long dd = E[u][i].second;
				if(D[v]==Inf){
					D[v] = D[u]+dd;
					Q.push(v);
				}
			}
		}
	}
	//cout<<'a'<<endl;
	HLD H(e);
	//cout<<'a'<<endl;
	int Q;
	cin>>Q;
	
	rep(_,Q){
		int u,v;
		scanf("%d %d",&u,&v);
		u--;v--;
		long long ans = D[u] + D[v] - D[H.lca(u,v)]*2;
		
		rep(i,K){
			rep(j,K){
				ans = min(ans,dis[i][u] + d[i*2][j*2+1] + dis[j][v]);				
			}
		}
		printf("%lld\n",ans);
	}
	
	
	
    return 0;
}
0