結果
問題 | No.1442 I-wate Shortest Path Problem |
ユーザー | kotatsugame |
提出日時 | 2021-03-26 23:01:24 |
言語 | C++14 (gcc 13.2.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 910 ms / 3,000 ms |
コード長 | 2,387 bytes |
コンパイル時間 | 936 ms |
コンパイル使用メモリ | 85,280 KB |
実行使用メモリ | 34,800 KB |
最終ジャッジ日時 | 2023-08-19 09:44:19 |
合計ジャッジ時間 | 14,184 ms |
ジャッジサーバーID (参考情報) |
judge13 / judge15 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 5 ms
17,692 KB |
testcase_01 | AC | 5 ms
17,696 KB |
testcase_02 | AC | 16 ms
20,052 KB |
testcase_03 | AC | 244 ms
20,080 KB |
testcase_04 | AC | 18 ms
20,040 KB |
testcase_05 | AC | 13 ms
17,916 KB |
testcase_06 | AC | 245 ms
19,936 KB |
testcase_07 | AC | 15 ms
19,960 KB |
testcase_08 | AC | 219 ms
17,888 KB |
testcase_09 | AC | 21 ms
20,256 KB |
testcase_10 | AC | 255 ms
22,328 KB |
testcase_11 | AC | 226 ms
18,052 KB |
testcase_12 | AC | 579 ms
26,736 KB |
testcase_13 | AC | 373 ms
21,816 KB |
testcase_14 | AC | 492 ms
24,808 KB |
testcase_15 | AC | 473 ms
23,528 KB |
testcase_16 | AC | 618 ms
27,668 KB |
testcase_17 | AC | 910 ms
32,124 KB |
testcase_18 | AC | 885 ms
32,324 KB |
testcase_19 | AC | 651 ms
28,140 KB |
testcase_20 | AC | 888 ms
32,112 KB |
testcase_21 | AC | 883 ms
31,932 KB |
testcase_22 | AC | 426 ms
27,548 KB |
testcase_23 | AC | 657 ms
34,800 KB |
testcase_24 | AC | 359 ms
21,740 KB |
testcase_25 | AC | 597 ms
30,856 KB |
testcase_26 | AC | 377 ms
23,240 KB |
コンパイルメッセージ
main.cpp:52:1: 警告: ISO C++ では型の無い ‘main’ の宣言を禁止しています [-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 k=1;k<17;k++) { for(int i=0;i<N;i++) { if(pr[k-1][i]==-1)pr[k][i]=-1; else pr[k][i]=pr[k-1][pr[k-1][i]]; } } 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"; } }