結果

問題 No.1442 I-wate Shortest Path Problem
ユーザー 👑 kmjpkmjp
提出日時 2021-03-26 22:44:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,130 bytes
コンパイル時間 2,162 ms
コンパイル使用メモリ 212,552 KB
実行使用メモリ 34,948 KB
最終ジャッジ日時 2024-05-06 16:37:26
合計ジャッジ時間 10,834 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
8,192 KB
testcase_01 AC 5 ms
8,064 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 242 ms
13,416 KB
testcase_25 AC 418 ms
21,200 KB
testcase_26 AC 270 ms
15,456 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define FORR2(x,y,arr) for(auto& [x,y]:arr)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
template<class T> bool chmax(T &a, const T &b) { if(a<b){a=b;return 1;}return 0;}
template<class T> bool chmin(T &a, const T &b) { if(a>b){a=b;return 1;}return 0;}
//-------------------------------------------------------

int N,K,Q;
vector<pair<int,int>> E[202020];

int P[21][200005],D[200005];
ll SD[2000005];
ll TD[10][200005];

void dfs(int cur) {
	FORR(e,E[cur]) if(e.first!=P[0][cur]) {
		D[e.first]=D[cur]+1;
		P[0][e.first]=cur;
		SD[e.first]=SD[cur]+e.second;
		dfs(e.first);
	}
}
int lca(int a,int b) {
	int ret=0,i,aa=a,bb=b;
	if(D[aa]>D[bb]) swap(aa,bb);
	for(i=19;i>=0;i--) if(D[bb]-D[aa]>=1<<i) bb=P[i][bb];
	for(i=19;i>=0;i--) if(P[i][aa]!=P[i][bb]) aa=P[i][aa], bb=P[i][bb];
	return (aa==bb)?aa:P[0][aa];               // vertex
}


void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N>>K;
	FOR(i,N-1) {
		cin>>x>>y>>r;
		E[x-1].push_back({y-1,2*r});
		E[y-1].push_back({x-1,2*r});
	}
	dfs(0);
	FOR(i,K) {
		cin>>x>>y;
		FOR(j,x) {
			cin>>k;
			E[N+i].push_back({k-1,y});
			E[k-1].push_back({N+i,y});
		}
	}
	FOR(i,K) {
		FOR(j,N+K) TD[i][j]=1LL<<60;
		priority_queue<pair<ll,int>> Q;
		TD[i][N+i]=0;
		Q.push({0,N+i});
		while(Q.size()) {
			ll co=-Q.top().first;
			int cur=Q.top().second;
			Q.pop();
			if(TD[i][cur]!=co) continue;
			FORR2(e,c,E[cur]) if(co+c<TD[i][e]) {
				TD[i][e]=co+c;
				Q.push({-TD[i][e],e});
			}
		}
	}
	
	cin>>Q;
	while(Q--) {
		cin>>x>>y;
		x--,y--;
		ll ret=1LL<<60;
		FOR(i,K) ret=min(ret,TD[i][x]+TD[i][y]);
		int lc=lca(x,y);
		ret=min(ret,SD[x]+SD[y]-2*SD[lc]);
		cout<<ret/2<<endl;
	}
	
	
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0