結果

提出番号 1789
提出者 uwi_
言語 Java
提出日時 2018-08-04 13:53:10
問題名 (73)観光計画
結果 AC
点数 100%

テストケース

テストケース 結果 得点 実行時間 メモリ使用量
1 AC 100% 161ms 129696KB
2 AC 100% 180ms 145024KB
3 AC 100% 127ms 112208KB
4 AC 100% 194ms 165984KB
5 AC 100% 138ms 122992KB
6 AC 100% 167ms 138448KB
7 AC 100% 239ms 177600KB
8 AC 100% 232ms 185904KB
9 AC 100% 119ms 122496KB
10 AC 100% 247ms 179536KB
11 AC 100% 136ms 113552KB
12 AC 100% 126ms 109104KB
13 AC 100% 142ms 125472KB
14 AC 100% 226ms 173520KB
15 AC 100% 181ms 149424KB
16 AC 100% 187ms 162336KB
17 AC 100% 240ms 177632KB
18 AC 100% 170ms 142224KB
19 AC 100% 121ms 110368KB
20 AC 100% 144ms 120912KB
21 AC 100% 106ms 108096KB
22 AC 100% 133ms 114688KB
23 AC 100% 130ms 116752KB
24 AC 100% 287ms 192656KB
25 AC 100% 210ms 174032KB
26 AC 100% 231ms 175584KB
27 AC 100% 133ms 111232KB
28 AC 100% 120ms 108080KB
29 AC 100% 153ms 135216KB
30 AC 100% 144ms 124192KB
31 AC 100% 261ms 178944KB
32 AC 100% 148ms 137136KB
33 AC 100% 122ms 105072KB
34 AC 100% 176ms 147840KB
35 AC 100% 105ms 108384KB
36 AC 100% 163ms 151024KB
37 AC 100% 117ms 113200KB
38 AC 100% 86ms 100768KB
39 AC 100% 286ms 197136KB
40 AC 100% 95ms 108064KB
41 AC 100% 213ms 151728KB
42 AC 100% 120ms 110096KB
43 AC 100% 223ms 178176KB
44 AC 100% 163ms 141424KB
45 AC 100% 159ms 139456KB
46 AC 100% 177ms 145616KB
47 AC 100% 250ms 176128KB
48 AC 100% 235ms 175856KB
49 AC 100% 139ms 133568KB
50 AC 100% 204ms 173936KB
51 AC 100% 97ms 106848KB
52 AC 100% 156ms 141104KB
53 AC 100% 143ms 111376KB
54 AC 100% 161ms 141536KB
55 AC 100% 105ms 108928KB
56 AC 100% 159ms 139920KB
57 AC 100% 143ms 122512KB
58 AC 100% 139ms 118512KB
59 AC 100% 111ms 114080KB
60 AC 100% 117ms 111264KB
61 AC 100% 124ms 112720KB
62 AC 100% 187ms 149744KB
63 AC 100% 244ms 175600KB
64 AC 100% 230ms 172304KB
65 AC 100% 212ms 170544KB
66 AC 100% 77ms 102928KB
67 AC 100% 177ms 129120KB
68 AC 100% 161ms 127760KB
69 AC 100% 264ms 188480KB
70 AC 100% 176ms 163344KB

ソースコード

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();
		long[] ft = new long[m];
		int[] w = new int[m];
		for(int i = 0;i < m;i++){
			ft[i] = (long)ni()-1<<32|ni()-1;
			w[i] = ni();
		}
		long[][] g = packWU(n, ft, w);
		
		int[] h = new int[K];
		int[] d = new int[K];
		for(int i = 0;i < K;i++){
			h[i] = ni()-1;
			d[i] = ni();
		}
		
		int[] td = new int[n];
		
		int O = 100005;
		Arrays.fill(td, Integer.MAX_VALUE / 2);
		MinHeap q = new MinHeap(n);
		for(int i = 0;i < K;i++){
			q.add(h[i], td[h[i]] = O - d[i]);
		}
		
		int ct = 0;
		while(q.size() > 0){
			int cur = q.argmin();
			q.remove(cur);
			
			if(td[cur] <= O){
				ct++;
				for(long e : g[cur]){
					int next = (int)(e>>>32);
					int nd = td[cur] + (int)e;
					if(nd < td[next]){
						td[next] = nd;
						q.update(next, nd);
					}
				}
			}
		}
		out.println(ct);
	}
	
	public static long[][] packWU(int n, long[] ft, int[] w){ return packWU(n, ft, w, ft.length); }
	public static long[][] packWU(int n, long[] ft, int[] w, int sup)
	{
		long[][] g = new long[n][];
		int[] p = new int[n];
		for(int i = 0;i < sup;i++)p[(int)(ft[i]>>>32)]++;
		for(int i = 0;i < sup;i++)p[(int)ft[i]]++;
		for(int i = 0;i < n;i++)g[i] = new long[p[i]];
		for(int i = 0;i < sup;i++){
			int f = (int)(ft[i]>>>32);
			int t = (int)ft[i];
			--p[f];
			g[f][p[f]] = (long)t<<32|w[i];
			--p[t];
			g[t][p[t]] = (long)f<<32|w[i];
		}
		return g;
	}

	
	public static class MinHeap {
		public int[] a;
		public int[] map;
		public int[] imap;
		public int n;
		public int pos;
		public static int INF = Integer.MAX_VALUE;
		
		public MinHeap(int m)
		{
			n = m+2;
			a = new int[n];
			map = new int[n];
			imap = new int[n];
			Arrays.fill(a, INF);
			Arrays.fill(map, -1);
			Arrays.fill(imap, -1);
			pos = 1;
		}
		
		public int add(int ind, int 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 int update(int ind, int x)
		{
			int ret = imap[ind];
			if(imap[ind] < 0){
				a[pos] = x; map[pos] = ind; imap[ind] = pos;
				pos++;
				up(pos-1);
			}else{
				int o = a[ret];
				a[ret] = x;
				up(ret);
				down(ret);
//				if(a[ret] > o){
//					up(ret);
//				}else{
//					down(ret);
//				}
			}
			return x;
		}
		
		public int remove(int ind)
		{
			if(pos == 1)return INF;
			if(imap[ind] == -1)return INF;
			
			pos--;
			int rem = imap[ind];
			int 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 int 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){
				int 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]){
					int 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));
	}
}