結果

問題 No.416 旅行会社
ユーザー GrenacheGrenache
提出日時 2016-08-28 16:01:47
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,673 ms / 4,000 ms
コード長 4,923 bytes
コンパイル時間 4,866 ms
コンパイル使用メモリ 81,336 KB
実行使用メモリ 93,932 KB
最終ジャッジ日時 2024-05-08 15:18:00
合計ジャッジ時間 21,446 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 774 ms
75,224 KB
testcase_01 AC 59 ms
37,088 KB
testcase_02 AC 60 ms
36,776 KB
testcase_03 AC 58 ms
37,040 KB
testcase_04 AC 58 ms
37,072 KB
testcase_05 AC 58 ms
37,304 KB
testcase_06 AC 59 ms
37,368 KB
testcase_07 AC 91 ms
38,268 KB
testcase_08 AC 138 ms
40,720 KB
testcase_09 AC 221 ms
43,532 KB
testcase_10 AC 928 ms
77,276 KB
testcase_11 AC 878 ms
76,572 KB
testcase_12 AC 903 ms
78,728 KB
testcase_13 AC 758 ms
77,124 KB
testcase_14 AC 1,445 ms
90,916 KB
testcase_15 AC 1,396 ms
93,932 KB
testcase_16 AC 1,673 ms
91,752 KB
testcase_17 AC 1,581 ms
91,492 KB
testcase_18 AC 1,512 ms
90,880 KB
testcase_19 AC 1,110 ms
78,696 KB
testcase_20 AC 1,066 ms
80,268 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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


public class Main_yukicoder416 {

    public static void main(String[] args) {
    	Scanner sc = new Scanner(System.in);
    	Printer pr = new Printer(System.out);

		int n = sc.nextInt();
		int m = sc.nextInt();
		int q = sc.nextInt();

		Set<Long> hs = new HashSet<>();
		for (int i = 0; i < m; i++) {
			int a = sc.nextInt() - 1;
			int b = sc.nextInt() - 1;

			hs.add((long)a << 32 | b);
		}

		Dijkstra di = new Dijkstra(n);
		for (int i = 0; i < q; i++) {
			int c = sc.nextInt() - 1;
			int d = sc.nextInt() - 1;
			di.addEdge(c, d, q - i);
			di.addEdge(d, c, q - i);

			hs.remove((long)c << 32 | d);
		}

		for (long e : hs) {
			int a = (int)(e >> 32);
			int b = (int)(e & 0xffff_ffff);

			di.addEdge(a, b, 0);
			di.addEdge(b, a, 0);
		}

		for (int i = 1; i < n; i++) {
			long d = di.getShortestPath(0, i);

			if (d == 0) {
				pr.println(-1);
			} else if (d == di.INF) {
				pr.println(0);
			} else {
				pr.println(q - d + 1);
			}
		}

    	pr.close();
        sc.close();
    }

	private static class Dijkstra {
		long[] d;
		List<Edge>[] edges;
		PriorityQueue<Vertex> pq;
		int n;
		int s;

		final long INF = Long.MAX_VALUE;

		@SuppressWarnings("unchecked")
		Dijkstra(int n) {
			this.n = n;

			edges = new List[n];
			for (int ii = 0; ii < n; ii++) {
				edges[ii] = new ArrayList<Edge>();
			}

			s = - 1;
		}

		// i, j:0-indexed
		public void addEdge(int i, int j, int c) {
			edges[i].add(new Edge(i, j, c));
		}

		public long getShortestPath(int i, int j) {
			if (s != i) {
				d = new long[n];
				Arrays.fill(d, INF);
				d[i] = 0;
				pq = new PriorityQueue<Vertex>();
				pq.add(new Vertex(i, d[i]));

				while (!pq.isEmpty()) {
					Vertex u = pq.poll();
					if (d[u.me] < u.d) {
						continue;  // skip old vertex
					}

					for (int ii = 0; ii < edges[u.me].size(); ii++) {
						Edge v = edges[u.me].get(ii);
						if (d[u.me] != INF && d[v.v] > Math.max(d[u.me], v.w)) {
							d[v.v] = Math.max(d[u.me], v.w);
							pq.add(new Vertex(v.v, d[v.v]));
						}
					}
				}

				s = i;
			}

			return d[j];
		}

		private static class Edge {
//			int u; // from
			int v; // to
			int w; // cost

			Edge(int u, int v, int w) {
//				this.u = u;
				this.v = v;
				this.w = w;
			}
		}

		private static class Vertex implements Comparable<Vertex> {
			int me; // me
			long d; // cost

			Vertex(int u, long w) {
				this.me = u;
				this.d = w;
			}

			@Override
			public int compareTo(Vertex o) {
//				return Long.compare(this.d, o.d);
				return this.d > o.d ? 1 : this.d < o.d ? -1 : 0;
			}
		}
	}

	@SuppressWarnings("unused")
	private static class Scanner {
		BufferedReader br;

		Scanner (InputStream in) {
			br = new BufferedReader(new InputStreamReader(in));
		}

		private boolean isPrintable(int ch) {
			return ch >= '!' && ch <= '~';
		}

		private boolean isCRLF(int ch) {
			return ch == '\n' || ch == '\r';
		}

		private int nextPrintable() {
			try {
				int ch;
				while ((ch = br.read()) != -1 && !isPrintable(ch)) {
				}
				return ch;
			} catch (IOException e) {
				throw new IllegalStateException();
			}
		}

		String next() {
			try {
				int ch = nextPrintable();
				StringBuilder sb = new StringBuilder();
				while (isPrintable(ch)) {
					sb.appendCodePoint(ch);
					ch = br.read();
				}

				return sb.toString();
			} catch (IOException e) {
				throw new IllegalStateException();
			}
		}

		int nextInt() throws RuntimeException {
			try {
				// parseInt
				boolean negative = false;
				int res = 0;

				int fc = nextPrintable();
				if (fc < '0') {
					if (fc == '-') {
						negative = true;
					} else if (fc != '+') {
						throw new NumberFormatException();
					}
					fc = br.read();
				}

				int ch = fc;
				do {
					if (ch < '0' || ch > '9') {
						throw new NumberFormatException();
					}

					res *= 10;
					res += ch - '0';
				} while ((ch = br.read()) != -1 && isPrintable(ch));

				// parseInt

				return negative ? -res : res;
			} catch (IOException e) {
				throw new IllegalStateException();
			}
		}

		long nextLong() throws RuntimeException {
			return Long.parseLong(next());
		}

		float nextFloat() throws RuntimeException {
			return Float.parseFloat(next());
		}

		double nextDouble() throws RuntimeException {
			return Double.parseDouble(next());
		}

		String nextLine() throws RuntimeException {
			try {
				int ch;
				while ((ch = br.read()) != -1 && !isCRLF(ch)) {
				}
				StringBuilder sb = new StringBuilder();
				while (!isCRLF(ch)) {
					sb.appendCodePoint(ch);
					ch = br.read();
				}

				return sb.toString();
			} catch (IOException e) {
				throw new IllegalStateException();
			}
		}

		void close() {
			try {
				br.close();
			} catch (IOException e) {
//				throw new IllegalStateException();
			}
		}
	}

	private static class Printer extends PrintWriter {
		Printer(PrintStream out) {
			super(out);
		}
	}
}
0