結果

問題 No.1812 Uribo Road
ユーザー nishi5451nishi5451
提出日時 2022-01-28 01:04:28
言語 Java21
(openjdk 21)
結果
AC  
実行時間 548 ms / 5,000 ms
コード長 5,314 bytes
コンパイル時間 2,343 ms
コンパイル使用メモリ 82,644 KB
実行使用メモリ 73,116 KB
最終ジャッジ日時 2024-06-08 00:22:35
合計ジャッジ時間 10,796 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 54 ms
50,264 KB
testcase_01 AC 53 ms
50,136 KB
testcase_02 AC 55 ms
50,156 KB
testcase_03 AC 139 ms
56,084 KB
testcase_04 AC 54 ms
49,884 KB
testcase_05 AC 54 ms
50,260 KB
testcase_06 AC 53 ms
50,028 KB
testcase_07 AC 80 ms
51,036 KB
testcase_08 AC 88 ms
51,648 KB
testcase_09 AC 112 ms
51,480 KB
testcase_10 AC 99 ms
51,432 KB
testcase_11 AC 84 ms
51,484 KB
testcase_12 AC 392 ms
70,264 KB
testcase_13 AC 332 ms
70,788 KB
testcase_14 AC 318 ms
66,808 KB
testcase_15 AC 246 ms
58,936 KB
testcase_16 AC 297 ms
64,484 KB
testcase_17 AC 458 ms
64,752 KB
testcase_18 AC 255 ms
59,768 KB
testcase_19 AC 543 ms
71,676 KB
testcase_20 AC 538 ms
73,084 KB
testcase_21 AC 548 ms
73,116 KB
testcase_22 AC 452 ms
72,100 KB
testcase_23 AC 119 ms
55,696 KB
testcase_24 AC 54 ms
50,284 KB
testcase_25 AC 163 ms
55,284 KB
testcase_26 AC 89 ms
51,492 KB
testcase_27 AC 240 ms
60,940 KB
testcase_28 AC 224 ms
58,612 KB
testcase_29 AC 53 ms
49,996 KB
testcase_30 AC 107 ms
52,068 KB
testcase_31 AC 358 ms
63,632 KB
testcase_32 AC 79 ms
51,052 KB
testcase_33 AC 307 ms
60,100 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