結果
問題 | No.1442 I-wate Shortest Path Problem |
ユーザー | kotatsugame |
提出日時 | 2021-03-26 22:54:03 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,255 bytes |
コンパイル時間 | 982 ms |
コンパイル使用メモリ | 86,008 KB |
実行使用メモリ | 24,040 KB |
最終ジャッジ日時 | 2024-05-06 16:48:54 |
合計ジャッジ時間 | 11,958 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 3 ms
6,656 KB |
testcase_01 | AC | 4 ms
6,400 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 | 316 ms
13,028 KB |
testcase_25 | AC | 524 ms
20,496 KB |
testcase_26 | AC | 359 ms
14,760 KB |
コンパイルメッセージ
main.cpp:52:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type] 52 | main() | ^~~~
ソースコード
#include<iostream> #include<algorithm> #include<vector> #include<queue> using namespace std; int N,K; int A[1<<17],B[1<<17],C[1<<17]; int M[10],Ps[10]; vector<int>X[10]; int Q; vector<pair<int,int> >G[1<<17]; int dep[1<<17],pr[17][1<<17]; long dst[1<<17]; void dfs(int u,int p,int d,long ds) { dep[u]=d; pr[0][u]=p; dst[u]=ds; for(pair<int,int>e:G[u]) { int v=e.first; if(v!=p)dfs(v,u,d+1,ds+e.second); } } int lca(int u,int v) { if(dep[u]>dep[v]) { int t=u; u=v; v=t; } for(int k=0;k<17;k++)if(dep[v]-dep[u]>>k&1)v=pr[k][v]; if(u==v)return u; for(int k=17;k--;) { if(pr[k][u]!=pr[k][v]) { u=pr[k][u]; v=pr[k][v]; } } return pr[0][u]; } long dist(int u,int v) { int w=lca(u,v); return dst[u]+dst[v]-2*dst[w]; } long d2K[10][1<<17]; long K2K[10][10]; main() { cin>>N>>K; for(int i=0;i<N-1;i++) { cin>>A[i]>>B[i]>>C[i]; A[i]--,B[i]--; G[A[i]].push_back(make_pair(B[i],C[i])); G[B[i]].push_back(make_pair(A[i],C[i])); } dfs(0,-1,0,0); for(int i=0;i<K;i++) { cin>>M[i]>>Ps[i]; X[i].assign(M[i],0); for(int j=0;j<M[i];j++) { cin>>X[i][j]; X[i][j]--; } for(int j=0;j<N;j++)d2K[i][j]=9e18; priority_queue<pair<long,int> >P; for(int u:X[i]) { d2K[i][u]=0; P.push(make_pair(0,u)); } while(!P.empty()) { int u=P.top().second; long cost=-P.top().first; P.pop(); if(d2K[i][u]<cost)continue; for(pair<int,int>e:G[u]) { int v=e.first; long nxt=cost+e.second; if(d2K[i][v]>nxt) { d2K[i][v]=nxt; P.push(make_pair(-nxt,v)); } } } } for(int i=0;i<K;i++)for(int j=i+1;j<K;j++) { K2K[i][j]=9e18; for(int u:X[i])K2K[i][j]=min(K2K[i][j],d2K[j][u]); K2K[j][i]=K2K[i][j]; } cin>>Q; for(;Q--;) { int u,v;cin>>u>>v;u--,v--; long ans=dist(u,v); long D[10]; priority_queue<pair<long,int> >P; for(int i=0;i<K;i++) { D[i]=d2K[i][u]+Ps[i]; P.push(make_pair(-D[i],i)); } while(!P.empty()) { int u=P.top().second; long cost=-P.top().first; P.pop(); if(D[u]<cost)continue; for(int v=0;v<K;v++) { long nxt=cost+K2K[u][v]+Ps[v]; if(D[v]>nxt) { D[v]=nxt; P.push(make_pair(-nxt,v)); } } } for(int i=0;i<K;i++)ans=min(ans,D[i]+d2K[i][v]); cout<<ans<<"\n"; } }