結果

問題 No.1812 Uribo Road
ユーザー nishi5451nishi5451
提出日時 2022-01-28 01:04:28
言語 Java21
(openjdk 21)
結果
AC  
実行時間 648 ms / 5,000 ms
コード長 5,314 bytes
コンパイル時間 2,649 ms
コンパイル使用メモリ 79,072 KB
実行使用メモリ 74,820 KB
最終ジャッジ日時 2023-08-27 04:24:19
合計ジャッジ時間 13,167 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
49,712 KB
testcase_01 AC 44 ms
49,612 KB
testcase_02 AC 46 ms
49,756 KB
testcase_03 AC 125 ms
56,212 KB
testcase_04 AC 43 ms
49,492 KB
testcase_05 AC 45 ms
49,952 KB
testcase_06 AC 46 ms
49,572 KB
testcase_07 AC 60 ms
50,576 KB
testcase_08 AC 86 ms
52,264 KB
testcase_09 AC 97 ms
52,636 KB
testcase_10 AC 85 ms
51,588 KB
testcase_11 AC 78 ms
52,736 KB
testcase_12 AC 458 ms
72,416 KB
testcase_13 AC 341 ms
72,212 KB
testcase_14 AC 326 ms
68,108 KB
testcase_15 AC 259 ms
59,616 KB
testcase_16 AC 326 ms
63,504 KB
testcase_17 AC 512 ms
66,480 KB
testcase_18 AC 271 ms
60,124 KB
testcase_19 AC 648 ms
72,436 KB
testcase_20 AC 592 ms
74,460 KB
testcase_21 AC 585 ms
74,820 KB
testcase_22 AC 501 ms
73,896 KB
testcase_23 AC 115 ms
56,244 KB
testcase_24 AC 48 ms
49,632 KB
testcase_25 AC 175 ms
55,296 KB
testcase_26 AC 76 ms
52,252 KB
testcase_27 AC 249 ms
61,980 KB
testcase_28 AC 234 ms
59,188 KB
testcase_29 AC 45 ms
49,588 KB
testcase_30 AC 106 ms
53,100 KB
testcase_31 AC 416 ms
64,004 KB
testcase_32 AC 71 ms
51,116 KB
testcase_33 AC 329 ms
60,256 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.util.stream.*;
import java.io.*;

class Path {
	int to;
	long d;
	Path(int to ,long d){
		this.to = to;
		this.d = d;
	}
}

class Node implements Comparable<Node> {
	int v;
	long dist;
	Node(int v, long dist){
		this.v = v;
		this.dist = dist;
	}
	@Override
	public int compareTo(Node other){
		if(this.dist > other.dist) return 1;
		else if(this.dist < other.dist) return -1;
		else return 0;
	}
}

class Edge {
	int a, b;
	long c;
	Edge(int a, int b, long c){
		this.a = a;
		this.b = b;
		this.c = c;
	}
}

public class Main {
	static final int INF = 1<<30;
	static final long INFL = 1L<<59;
	static final int MOD = 1000000007;
	static final int MOD2 = 998244353;
	static List<List<Path>> g;
	static int n;

	static Long[] dijkstra(int from){
		Long[] ret = new Long[n];
		Arrays.fill(ret, INFL);
		ret[from] = 0L;
		Queue<Node> pq = new PriorityQueue<>();
		pq.add(new Node(from, 0));
		while(!pq.isEmpty()){
			Node node = pq.poll();
			if(node.dist > ret[node.v]) continue;
			for(Path path : g.get(node.v)){
				if(node.dist + path.d < ret[path.to]){
					ret[path.to] = node.dist + path.d;
					pq.add(new Node(path.to, ret[path.to]));
				}
			}
		}
		return ret;
	}
	
	public static void main(String[] args) {
		FastScanner fs = new FastScanner();
		PrintWriter pw = new PrintWriter(System.out);
		n = fs.nextInt();
		int m = fs.nextInt();
		int k = fs.nextInt();
		int[] r = new int[k];
		for(int i = 0; i < k; i++) r[i] = fs.nextInt() - 1;
		g = new ArrayList<>();
		for(int i = 0; i < n; i++) g.add(new ArrayList<>());
		Edge[] edge = new Edge[m];
		for(int i = 0; i < m; i++){
			int a = fs.nextInt() - 1;
			int b = fs.nextInt() - 1;
			long d = fs.nextLong();
			g.get(a).add(new Path(b, d));
			g.get(b).add(new Path(a, d));
			edge[i] = new Edge(a, b, d);
		}

		Set<Integer> set = new HashSet<>();
		set.add(0);
		set.add(n-1);
		for(int i = 0; i < k; i++){
			set.add(edge[r[i]].a);
			set.add(edge[r[i]].b);
		}

		Map<Integer, Long[]> map = new HashMap<>();
		for(int v : set){
			Long[] t = dijkstra(v);
			map.put(v, t);
		}

		long[][][] dp = new long[1<<k][k][2];
		for(int i = 0; i < 1<<k; i++){
			for(int j = 0; j < k; j++){
				dp[i][j][0] = INFL;
				dp[i][j][1] = INFL;
			}
		}
		for(int i = 0; i < k; i++){
			dp[1<<i][i][0] = map.get(0)[edge[r[i]].b] + edge[r[i]].c;
			dp[1<<i][i][1] = map.get(0)[edge[r[i]].a] + edge[r[i]].c;
		}
		for(int s = 0; s < 1<<k; s++){
			for(int i = 0; i < k; i++){
				if(s != 0 && (s & 1<<i) == 0) continue;
				int a = edge[r[i]].a;
				int b = edge[r[i]].b;
				for(int j = 0; j < k; j++){
					if((s & 1<<j) != 0) continue;
					int na = edge[r[j]].a;
					int nb = edge[r[j]].b;
					long nc = edge[r[j]].c;
					dp[s|1<<j][j][0] = Math.min(dp[s|1<<j][j][0], dp[s][i][0] + map.get(a)[nb] + nc);
					dp[s|1<<j][j][0] = Math.min(dp[s|1<<j][j][0], dp[s][i][1] + map.get(b)[nb] + nc);
					dp[s|1<<j][j][1] = Math.min(dp[s|1<<j][j][1], dp[s][i][0] + map.get(a)[na] + nc);
					dp[s|1<<j][j][1] = Math.min(dp[s|1<<j][j][1], dp[s][i][1] + map.get(b)[na] + nc);
				}
			}
		}
		long ans = INFL;
		for(int i = 0; i < k; i++){
			int a = edge[r[i]].a;
			int b = edge[r[i]].b;
			ans = Math.min(ans, dp[(1<<k)-1][i][0] + map.get(a)[n-1]);
			ans = Math.min(ans, dp[(1<<k)-1][i][1] + map.get(b)[n-1]);
		}
		pw.println(ans);
	
		pw.close();
	}
}

class FastScanner {
	private InputStream in = System.in;
	private byte[] buffer = new byte[1024];
	private int length = 0, p = 0;
	private boolean hasNextByte() {
		if (p < length) return true;
		else{
			p = 0;
			try{
				length = in.read(buffer);
			}catch(Exception e){
				e.printStackTrace();
			}
			if(length == 0) return false;
		}
		return true;
	}
	private int readByte() {
		if (hasNextByte() == true) return buffer[p++];
		return -1;
	}
	private static boolean isPrintable(int n) {
		return 33 <= n && n <= 126;
	}
	private void skip() {
		while (hasNextByte() && !isPrintable(buffer[p])) p++;
	}
	public boolean hasNext() {
		skip();
		return hasNextByte();
	}
	public String next() {
		if(!hasNext()) throw new NoSuchElementException();
		StringBuilder sb = new StringBuilder();
		int temp = readByte();
		while (isPrintable(temp)) {
			sb.appendCodePoint(temp);
			temp = readByte();
		}
		return sb.toString();
	}
	public int nextInt() {
		return Math.toIntExact(nextLong());
	}
	public int[] nextInts(int n) {
		int[] ar = new int[n];
		for (int i = 0; i < n; i++) ar[i] = nextInt();
		return ar;
	}
	public long[] nextLongs(int n) {
		long[] ar = new long[n];
		for (int i = 0; i < n; i++) ar[i] = nextLong();
		return ar;
	}
	public long nextLong() {
		if(!hasNext()) throw new NoSuchElementException();
		boolean minus = false;
		int temp = readByte();
		if (temp == '-') {
			minus = true;
			temp = readByte();
		}
		if (temp < '0' || '9' < temp) throw new NumberFormatException();
		long n = 0;
		while (isPrintable(temp)) {
			if ('0' <= temp && temp <= '9') {
				n *= 10;
				n += temp - '0';
			}
			temp = readByte();
		}
		return minus ? -n : n;
	}
	public char[] nextChars() {
		String s = next();
		return s.toCharArray();
	}
	public char[][] nextCharMat(int h, int w) {
		char[][] ar = new char[h][w];
		for(int i = 0; i < h; i++){
			String s = next();
			for(int j = 0; j < w; j++){
				ar[i][j] = s.charAt(j);
			}
		}
		return ar;
	}
}
0