結果

問題 No.1308 ジャンプビーコン
ユーザー fgwiebfaoishfgwiebfaoish
提出日時 2020-12-05 03:09:11
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 1,039 ms / 4,000 ms
コード長 4,753 bytes
コンパイル時間 2,090 ms
コンパイル使用メモリ 113,116 KB
実行使用メモリ 101,680 KB
最終ジャッジ日時 2023-10-13 13:29:29
合計ジャッジ時間 13,754 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 52 ms
24,244 KB
testcase_01 AC 53 ms
22,180 KB
testcase_02 AC 53 ms
24,300 KB
testcase_03 AC 54 ms
22,184 KB
testcase_04 AC 53 ms
22,100 KB
testcase_05 AC 53 ms
22,188 KB
testcase_06 AC 54 ms
22,212 KB
testcase_07 AC 53 ms
22,252 KB
testcase_08 AC 60 ms
24,276 KB
testcase_09 AC 61 ms
24,336 KB
testcase_10 AC 61 ms
22,448 KB
testcase_11 AC 60 ms
22,524 KB
testcase_12 AC 61 ms
22,296 KB
testcase_13 AC 70 ms
26,792 KB
testcase_14 AC 70 ms
26,792 KB
testcase_15 AC 71 ms
29,012 KB
testcase_16 AC 72 ms
26,956 KB
testcase_17 AC 70 ms
26,788 KB
testcase_18 AC 395 ms
99,592 KB
testcase_19 AC 400 ms
99,608 KB
testcase_20 AC 397 ms
99,516 KB
testcase_21 AC 424 ms
97,536 KB
testcase_22 AC 399 ms
101,516 KB
testcase_23 AC 55 ms
22,244 KB
testcase_24 AC 61 ms
24,688 KB
testcase_25 AC 60 ms
22,580 KB
testcase_26 AC 394 ms
101,680 KB
testcase_27 AC 390 ms
99,572 KB
testcase_28 AC 396 ms
99,776 KB
testcase_29 AC 655 ms
97,888 KB
testcase_30 AC 655 ms
99,864 KB
testcase_31 AC 1,039 ms
99,756 KB
testcase_32 AC 603 ms
99,880 KB
testcase_33 AC 632 ms
97,772 KB
testcase_34 AC 389 ms
95,560 KB
testcase_35 AC 394 ms
97,632 KB
testcase_36 AC 388 ms
97,516 KB
testcase_37 AC 390 ms
95,424 KB
testcase_38 AC 399 ms
97,436 KB
testcase_39 AC 410 ms
99,536 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.Collections.Generic;
using System.Collections;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using static System.Math;
using System.Numerics;
using System.Threading;
using System.Runtime.CompilerServices;
static class Program{
	const int mod=(int)1e9+7;
	const long inf=long.MaxValue>>1;
	static List<ValueTuple<int,int>>[] li;
	static bool[] b;
	static void Main(){
		Sc sc=new Sc();
		var s=sc.Ia;
		int n=s[0],m=n-1;
		li=new List<ValueTuple<int,int>>[n+1];
		b=new bool[n+1];
		var c=new long[n+1][];
		for(int i=1;i<=n;i++){
			li[i]=new List<ValueTuple<int,int>>();
			c[i]=Enumerable.Repeat(inf,s[1]).ToArray();
		}
		for(int i=0;i<m;i++){
			var e=sc.Ia;
			li[e[0]].Add(ValueTuple.Create(e[1],e[2]));
			li[e[1]].Add(ValueTuple.Create(e[0],e[2]));
		}
		var q=sc.Ia;
		var al=new Alca(li,true,1);
		c[q[0]][0]=0;
		var u=new int[n];
		long f=0;
		var ba=new int[n+1];
		for(int i = 1;i<s[1];i++) {
			var d=al.Di(q[i-1],q[i]);
			var p=al.Lca(q[i-1],q[i]);
			var e=q[i-1];
			long z=f;
			f=inf;
			int v=0;
			while(e!=p){
				Fu(e);
				e=al.oy[e];
			}
			e=q[i];
			Fu(p);
			int k=0;
			while(e!=p){
				u[k++]=e;
				e=al.oy[e];
			}
			for(int j = k-1;j>=0;j--) {Fu(u[j]);}
			void Fu(int g) {
				z=Min(z+(v==0?0:al.Di(v,g)),c[g][i-1]+s[2]);
				c[g][i]=z+al.Di(g,q[i]);
				f=Min(f,c[g][i]);
				ba[g]=i;
				v=g;
			}
			for(int j = 1;j<=n;j++) {
				if(ba[j]!=i){c[j][i]=c[j][i-1]+d;}
			}
		}
		Console.WriteLine("{0}",f);
	}
	static void Fu(int a,int g){
		b[a]=true;
		for(int i=0;i<li[a].Count;i++){
			if(!b[li[a][i].Item1]){Fu(li[a][i].Item1,g);}
		}
	}
}
public class Alca{
	private List<ValueTuple<int,int>>[] li;
	private readonly int[] at,ix,l;
	public readonly int[] oy;
	private readonly long[] de;
	private readonly int[][] g;
	private int f=0;
	public Alca(List<ValueTuple<int,int>>[] li,bool bo,int r){
		int n=li.Length,z=(n<<1)-3,m=z;
		this.li=li;
		at=new int[z];
		ix=new int[n];
		oy=new int[n];
		de=new long[n];
		if(bo){Fu1(r,1);}
		else{
			for(int i = 1;i<n;i++) {
				if(de[i]==0){Fu1(i,1);f++;}
			}
		}
		m=(int)Log(m,2)+1;
		g=new int[m][];
		g[0]=new int[z];
		l=new int[z+1];
		for(int i=0;i<z;i++){
			g[0][i]=i;
			l[i+1]=l[i>>1]+1;
		}
		for(int i=1,k=1;i<m;i++,k<<=1){
			int p=z+1-(k<<1);
			g[i]=new int[p];
			for(int j=0;j<p;j++){g[i][j]=de[at[g[i-1][j]]]<de[at[g[i-1][j+k]]]?g[i-1][j]:g[i-1][j+k];}
		}
	}
	public int Lca(int a,int b){return Get(ix[a],ix[b]);}
	public long Di(int a,int b){
		int p=Get(ix[a],ix[b]);
		return p!=0?de[a]+de[b]-(de[p]<<1):long.MinValue;
	}
	private int Get(int a,int b){
		if(a>b){(a,b)=(b,a);}
		int p=l[b-a];
		return at[de[at[g[p][a]]]<de[at[g[p][b+1-(1<<p)]]]?g[p][a]:g[p][b+1-(1<<p)]];
	}
	private void Fu1(int a,long g){
		at[f]=a;
		de[a]=g;
		ix[a]=f;
		f++;
		for(int i=0;i<li[a].Count;i++){
			if(de[li[a][i].Item1]==0){
				Fu1(li[a][i].Item1,g+li[a][i].Item2);
				oy[li[a][i].Item1]=a;
				at[f]=a;
				f++;
			}
		}
	}
}
public class Sc{
	public int I{get{return int.Parse(Console.ReadLine());}}
	public long L{get{return long.Parse(Console.ReadLine());}}
	public double D{get{return double.Parse(Console.ReadLine());}}
	public string S{get{return Console.ReadLine();}}
	public int[] Ia{get{return Array.ConvertAll(Console.ReadLine().Split(),int.Parse);}}
	public long[] La{get{return Array.ConvertAll(Console.ReadLine().Split(),long.Parse);}}
	public double[] Da{get{return Array.ConvertAll(Console.ReadLine().Split(),double.Parse);}}
	public string[] Sa{get{return Console.ReadLine().Split();}}
	public object[] Oa{get{return Console.ReadLine().Split();}}
	public int[] Ia2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),int.Parse);}}
	public int[] Ia3(string a,string b){return Array.ConvertAll((a+Console.ReadLine()+b).Split(),int.Parse);}
	public long[] La2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),long.Parse);}}
	public long[] La3(string a,string b){return Array.ConvertAll((a+Console.ReadLine()+b).Split(),long.Parse);}
	public double[] Da2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),double.Parse);}}
	public double[] Da3(string a,string b){return Array.ConvertAll((a+Console.ReadLine()+b).Split(),double.Parse);}
	public T[] Arr<T>(int n,Func<T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f();}return a;}
	public T[] Arr<T>(int n,Func<int,T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i);}return a;}
	public T[] Arr<T>(int n,Func<string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(Console.ReadLine().Split());}return a;}
	public T[] Arr<T>(int n,Func<int,string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i,Console.ReadLine().Split());}return a;}
}
0