結果

提出番号 1766
提出者 uwi_
言語 Java
提出日時 2018-08-04 13:48:22
問題名 (73)観光計画
結果 MLE
点数 0%

テストケース

テストケース 結果 得点 実行時間 メモリ使用量
1 AC 100% 189ms 158800KB
2 AC 100% 210ms 192832KB
3 AC 100% 131ms 129328KB
4 AC 100% 281ms 212176KB
5 AC 100% 168ms 153856KB
6 AC 100% 221ms 192192KB
7 AC 100% 311ms 219872KB
8 AC 100% 323ms 217392KB
9 AC 100% 144ms 142544KB
10 MLE 0% 371ms 270736KB
11 AC 100% 149ms 133152KB
12 AC 100% 109ms 115776KB
13 AC 100% 144ms 140624KB
14 AC 100% 314ms 217824KB
15 AC 100% 235ms 178688KB
16 AC 100% 229ms 188992KB
17 AC 100% 264ms 183456KB
18 AC 100% 220ms 165584KB
19 AC 100% 143ms 118016KB
20 AC 100% 179ms 143520KB
21 AC 100% 105ms 118720KB
22 AC 100% 169ms 133792KB
23 AC 100% 158ms 139456KB
24 MLE 0% 429ms 277280KB
25 AC 100% 258ms 213040KB
26 MLE 0% 359ms 262544KB
27 AC 100% 121ms 119648KB
28 AC 100% 119ms 117056KB
29 AC 100% 189ms 165856KB
30 AC 100% 160ms 143088KB
31 MLE 0% 347ms 273712KB
32 AC 100% 224ms 186528KB
33 AC 100% 120ms 109936KB
34 AC 100% 250ms 205152KB
35 AC 100% 108ms 113680KB
36 AC 100% 287ms 215408KB
37 AC 100% 141ms 128224KB
38 AC 100% 106ms 105104KB
39 MLE 0% 375ms 277376KB
40 AC 100% 119ms 118160KB
41 MLE 0% 339ms 270256KB
42 AC 100% 109ms 117424KB
43 MLE 0% 374ms 266208KB
44 AC 100% 239ms 191248KB
45 AC 100% 201ms 169568KB
46 AC 100% 249ms 171536KB
47 MLE 0% 323ms 269024KB
48 MLE 0% 342ms 269664KB
49 AC 100% 190ms 180736KB
50 AC 100% 315ms 220480KB
51 AC 100% 116ms 115920KB
52 AC 100% 180ms 157664KB
53 AC 100% 134ms 123888KB
54 AC 100% 194ms 191552KB
55 AC 100% 135ms 118144KB
56 AC 100% 219ms 160192KB
57 AC 100% 142ms 138624KB
58 AC 100% 138ms 131744KB
59 AC 100% 137ms 121296KB
60 AC 100% 133ms 124544KB
61 AC 100% 159ms 131792KB
62 AC 100% 277ms 207456KB
63 AC 100% 326ms 217312KB
64 AC 100% 197ms 172304KB
65 AC 100% 277ms 210000KB
66 AC 100% 78ms 102944KB
67 AC 100% 191ms 152816KB
68 AC 100% 204ms 151856KB
69 MLE 0% 419ms 278592KB
70 AC 100% 260ms 197280KB

ソースコード

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;

public class Main {
	static InputStream is;
	static PrintWriter out;
	static String INPUT = "";

	static void solve() {
		int n = ni(), m = ni(), K = ni();
		int[] from = new int[m];
		int[] to = new int[m];
		int[] w = new int[m];
		for(int i = 0;i < m;i++){
			from[i] = ni()-1;
			to[i] = ni()-1;
			w[i] = ni();
		}
		int[][][] g = packWU(n, from, to, w);
		
		int[][] hs = new int[K][];
		for(int i = 0;i < K;i++){
			hs[i] = new int[]{ni()-1, ni()};
		}
		
		long[] td = new long[n];
		
		int O = 100005;
		Arrays.fill(td, Long.MAX_VALUE / 2);
		MinHeapL q = new MinHeapL(n);
		for(int[] h : hs){
			q.add(h[0], td[h[0]] = O - h[1]);
		}
		
		while(q.size() > 0){
			int cur = q.argmin();
			q.remove(cur);
			
			for(int[] e : g[cur]){
				int next = e[0];
				long nd = td[cur] + e[1];
				if(nd < td[next]){
					td[next] = nd;
					q.update(next, nd);
				}
			}
		}
		
		int ct = 0;
		for(int i = 0;i < n;i++){
			if(td[i] <= O){
				ct++;
			}
		}
		out.println(ct);
	}
	
	public static class MinHeapL {
		public long[] a;
		public int[] map;
		public int[] imap;
		public int n;
		public int pos;
		public static long INF = Long.MAX_VALUE;
		
		public MinHeapL(int m)
		{
			n = Integer.highestOneBit((m+1)<<1);
			a = new long[n];
			map = new int[n];
			imap = new int[n];
			Arrays.fill(a, INF);
			Arrays.fill(map, -1);
			Arrays.fill(imap, -1);
			pos = 1;
		}
		
		public long add(int ind, long x)
		{
			int ret = imap[ind];
			if(imap[ind] < 0){
				a[pos] = x; map[pos] = ind; imap[ind] = pos;
				pos++;
				up(pos-1);
			}
			return ret != -1 ? a[ret] : x;
		}
		
		public long update(int ind, long x)
		{
			int ret = imap[ind];
			if(imap[ind] < 0){
				a[pos] = x; map[pos] = ind; imap[ind] = pos;
				pos++;
				up(pos-1);
			}else{
				a[ret] = x;
				up(ret);
				down(ret);
			}
			return x;
		}
		
		public long remove(int ind)
		{
			if(pos == 1)return INF;
			if(imap[ind] == -1)return INF;
			
			pos--;
			int rem = imap[ind];
			long ret = a[rem];
			map[rem] = map[pos];
			imap[map[pos]] = rem;
			imap[ind] = -1;
			a[rem] = a[pos];
			a[pos] = INF;
			map[pos] = -1;
			
			up(rem);
			down(rem);
			return ret;
		}
		
		public long min() { return a[1]; }
		public int argmin() { return map[1]; }
		public int size() {	return pos-1; }
		
		private void up(int cur)
		{
			for(int c = cur, p = c>>>1;p >= 1 && a[p] > a[c];c>>>=1, p>>>=1){
				long d = a[p]; a[p] = a[c]; a[c] = d;
				int e = imap[map[p]]; imap[map[p]] = imap[map[c]]; imap[map[c]] = e;
				e = map[p]; map[p] = map[c]; map[c] = e;
			}
		}
		
		private void down(int cur)
		{
			for(int c = cur;2*c < pos;){
				int b = a[2*c] < a[2*c+1] ? 2*c : 2*c+1;
				if(a[b] < a[c]){
					long d = a[c]; a[c] = a[b]; a[b] = d;
					int e = imap[map[c]]; imap[map[c]] = imap[map[b]]; imap[map[b]] = e;
					e = map[c]; map[c] = map[b]; map[b] = e;
					c = b;
				}else{
					break;
				}
			}
		}
	}

	
	public static int[][][] packWU(int n, int[] from, int[] to, int[] w) {
		int[][][] g = new int[n][][];
		int[] p = new int[n];
		for (int f : from)
			p[f]++;
		for (int t : to)
			p[t]++;
		for (int i = 0; i < n; i++)
			g[i] = new int[p[i]][2];
		for (int i = 0; i < from.length; i++) {
			--p[from[i]];
			g[from[i]][p[from[i]]][0] = to[i];
			g[from[i]][p[from[i]]][1] = w[i];
			--p[to[i]];
			g[to[i]][p[to[i]]][0] = from[i];
			g[to[i]][p[to[i]]][1] = w[i];
		}
		return g;
	}


	public static void main(String[] args) throws Exception {
		long S = System.currentTimeMillis();
		is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
		out = new PrintWriter(System.out);

		solve();
		out.flush();
		long G = System.currentTimeMillis();
		tr(G - S + "ms");
	}

	private static boolean eof() {
		if (lenbuf == -1)
			return true;
		int lptr = ptrbuf;
		while (lptr < lenbuf)
			if (!isSpaceChar(inbuf[lptr++]))
				return false;

		try {
			is.mark(1000);
			while (true) {
				int b = is.read();
				if (b == -1) {
					is.reset();
					return true;
				} else if (!isSpaceChar(b)) {
					is.reset();
					return false;
				}
			}
		} catch (IOException e) {
			return true;
		}
	}

	private static byte[] inbuf = new byte[1024];
	public static int lenbuf = 0, ptrbuf = 0;

	private static int readByte() {
		if (lenbuf == -1)
			throw new InputMismatchException();
		if (ptrbuf >= lenbuf) {
			ptrbuf = 0;
			try {
				lenbuf = is.read(inbuf);
			} catch (IOException e) {
				throw new InputMismatchException();
			}
			if (lenbuf <= 0)
				return -1;
		}
		return inbuf[ptrbuf++];
	}

	private static boolean isSpaceChar(int c) {
		return !(c >= 33 && c <= 126);
	}

	private static int skip() {
		int b;
		while ((b = readByte()) != -1 && isSpaceChar(b))
			;
		return b;
	}

	private static double nd() {
		return Double.parseDouble(ns());
	}

	private static char nc() {
		return (char) skip();
	}

	private static String ns() {
		int b = skip();
		StringBuilder sb = new StringBuilder();
		while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != '
									// ')
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}

	private static char[] ns(int n) {
		char[] buf = new char[n];
		int b = skip(), p = 0;
		while (p < n && !(isSpaceChar(b))) {
			buf[p++] = (char) b;
			b = readByte();
		}
		return n == p ? buf : Arrays.copyOf(buf, p);
	}

	private static char[][] nm(int n, int m) {
		char[][] map = new char[n][];
		for (int i = 0; i < n; i++)
			map[i] = ns(m);
		return map;
	}

	private static int[] na(int n) {
		int[] a = new int[n];
		for (int i = 0; i < n; i++)
			a[i] = ni();
		return a;
	}

	private static int ni() {
		int num = 0, b;
		boolean minus = false;
		while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
			;
		if (b == '-') {
			minus = true;
			b = readByte();
		}

		while (true) {
			if (b >= '0' && b <= '9') {
				num = num * 10 + (b - '0');
			} else {
				return minus ? -num : num;
			}
			b = readByte();
		}
	}

	private static long nl() {
		long num = 0;
		int b;
		boolean minus = false;
		while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
			;
		if (b == '-') {
			minus = true;
			b = readByte();
		}

		while (true) {
			if (b >= '0' && b <= '9') {
				num = num * 10 + (b - '0');
			} else {
				return minus ? -num : num;
			}
			b = readByte();
		}
	}

	private static void tr(Object... o) {
		if (INPUT.length() != 0)
			System.out.println(Arrays.deepToString(o));
	}
}