結果

問題 No.1308 ジャンプビーコン
ユーザー tails
提出日時 2020-12-06 01:05:49
言語 cLay
(20241019-1)
結果
AC  
実行時間 161 ms / 4,000 ms
コード長 1,029 bytes
コンパイル時間 3,438 ms
コンパイル使用メモリ 178,332 KB
実行使用メモリ 70,400 KB
最終ジャッジ日時 2024-07-05 14:48:44
合計ジャッジ時間 5,264 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

diff #

int u[3d3],v[3d3],l[3d3],x[3d3];


int costs[1d7];
int visited[3d3];
ll saveds[3d3];
int steps[1d7],fs[1d7];
int stepn;

ll totalcost;
ll totalsaved;

wgraph<int> g;
HLD h;

{
	int @n,@q,@c;
	rd((u--,v--,l)(n-1));
	rd((x--)(q));
	g.setEdge(n,n-1,u,v,l);
	h.init(g.g);
	rep(i,n-1){
		costs[u[i]+3000*v[i]]=l[i];
		costs[v[i]+3000*u[i]]=l[i];
	}
	{
		int p=x[0];
		steps[stepn++]=p;
		rep(k,q){
			int y=x[k];
			int a=h.lca(p,y);
			while(p!=a){
				p=h.up(p);
				steps[stepn++]=p;
			}
			stepn+=h.depth(y)-h.depth(a);
			int n=stepn;
			while(y!=a){
				steps[--n]=y;
				y=h.up(y);
			}
			p=x[k];
			fs[stepn-1]=1;
		}
	}
	
	{
		int p=steps[0];
		visited[p]=1;
		ll saved=0;
		rep(stepi,1,stepn){
			int y=steps[stepi];
			int localcost=costs[p+3000*y];
			saved+=localcost;
			totalcost+=localcost;
			if(visited[y]){
				if(totalsaved<saveds[y]+saved-c){
					totalsaved=saveds[y]+saved-c;
				}
			}
			visited[y]=1;
			if(fs[stepi]){
				saved=0;
			}
			saveds[y]=totalsaved;
			p=y;
		}
	}
	wt(totalcost-totalsaved);
}
0