結果

問題 No.468 役に立つ競技プログラミング実践編
ユーザー uwiuwi
提出日時 2016-12-18 00:13:56
言語 Java21
(openjdk 21)
結果
AC  
実行時間 559 ms / 2,000 ms
コード長 6,330 bytes
コンパイル時間 4,192 ms
コンパイル使用メモリ 83,220 KB
実行使用メモリ 86,832 KB
最終ジャッジ日時 2024-05-08 04:19:18
合計ジャッジ時間 14,644 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 100 ms
51,640 KB
testcase_01 AC 94 ms
52,876 KB
testcase_02 AC 102 ms
51,808 KB
testcase_03 AC 104 ms
51,836 KB
testcase_04 AC 92 ms
52,652 KB
testcase_05 AC 94 ms
52,640 KB
testcase_06 AC 102 ms
52,884 KB
testcase_07 AC 104 ms
52,916 KB
testcase_08 AC 97 ms
52,924 KB
testcase_09 AC 107 ms
52,568 KB
testcase_10 AC 103 ms
52,848 KB
testcase_11 AC 95 ms
52,916 KB
testcase_12 AC 94 ms
52,804 KB
testcase_13 AC 102 ms
53,088 KB
testcase_14 AC 102 ms
53,244 KB
testcase_15 AC 99 ms
52,784 KB
testcase_16 AC 106 ms
53,200 KB
testcase_17 AC 106 ms
52,888 KB
testcase_18 AC 102 ms
53,160 KB
testcase_19 AC 104 ms
53,068 KB
testcase_20 AC 101 ms
52,980 KB
testcase_21 AC 101 ms
52,856 KB
testcase_22 AC 103 ms
52,948 KB
testcase_23 AC 104 ms
52,664 KB
testcase_24 AC 523 ms
83,964 KB
testcase_25 AC 527 ms
83,948 KB
testcase_26 AC 559 ms
86,832 KB
testcase_27 AC 538 ms
77,116 KB
testcase_28 AC 559 ms
77,016 KB
testcase_29 AC 522 ms
74,416 KB
testcase_30 AC 532 ms
77,008 KB
testcase_31 AC 541 ms
77,096 KB
testcase_32 AC 538 ms
76,908 KB
testcase_33 AC 536 ms
77,060 KB
testcase_34 AC 293 ms
57,272 KB
testcase_35 AC 106 ms
39,796 KB
testcase_36 AC 95 ms
38,796 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package adv2016;
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 N468_2 {
	InputStream is;
	PrintWriter out;
	String INPUT = "";
	
	void solve()
	{
		int n = ni(), m = 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();
			to[i] = ni();
			w[i] = ni();
		}
		int[][][] g = packWD(n, from, to, w);
		int[][][] ig = packWD(n, to, from, w);
		int[] indeg = new int[n];
		for(int i = 0;i < m;i++){
			indeg[to[i]]++;
		}
		TaggedSimpleMinHeapL h = new TaggedSimpleMinHeapL(n);
		h.add(0, 0L);
		long[] ends = new long[n];
		int[] ord = new int[n];
		int p = 0;
		while(h.size() > 0){
			int cur = h.mintag();
			h.poll();
			ord[p++] = cur;
			for(int[] e : g[cur]){
				ends[e[0]] = Math.max(ends[e[0]], ends[cur] + e[1]);
				indeg[e[0]]--;
				if(indeg[e[0]] == 0){
					h.add(e[0], ends[e[0]]);
				}
			}
		}
		out.print(ends[n-1] + " ");
		boolean[] cri = new boolean[n];
		cri[n-1] = true;
		for(int i = p-1;i >= 0;i--){
			if(!cri[ord[i]])continue;
			for(int[] e : ig[ord[i]]){
				if(ends[ord[i]] == ends[e[0]] + e[1]){
					cri[e[0]] = true;
				}
			}
		}
		int incri = 0;
		for(int i = 0;i < n;i++){
			if(!cri[i])incri++;
		}
		out.println(incri + "/" + n);
	}
	
	public static class TaggedSimpleMinHeapL {
		public long[] a;
		public int[] tags;
		public int n;
		public int pos;
		public static final long INF = Long.MAX_VALUE;
		
		public TaggedSimpleMinHeapL(int m)
		{
			n = m+1;
			a = new long[n];
			tags = new int[n];
			Arrays.fill(a, INF);
			Arrays.fill(tags, -1);
			pos = 1;
		}
		
		public TaggedSimpleMinHeapL(long[] in)
		{
			n = in.length+1;
			if((n&1)==1)n++;
			a = new long[n];
			tags = new int[n];
			pos = 1+in.length;
//			Arrays.fill(a, INF);
			a[0] = a[n-1] = a[n-2] = INF;
			System.arraycopy(in, 0, a, 1, in.length);
			for(int i = 0;i < in.length;i++){
				tags[1+i] = i;
			}
			for(int t = pos/2-1;t >= 1;t--){
				for(int c = t;2*c < pos;){
					int smaller = a[2*c] < a[2*c+1] ? 2*c : 2*c+1;
					if(a[smaller] < a[c]){
						{long d = a[c]; a[c] = a[smaller]; a[smaller] = d;}
						{int d = tags[c]; tags[c] = tags[smaller]; tags[smaller] = d;}
						c = smaller;
					}else{
						break;
					}
				}
			}
		}
		
		public void add(int tag, long x)
		{
			a[pos] = x; tags[pos] = tag; pos++;
			for(int c = pos-1, p = c>>>1;p >= 1 && a[c] < a[p];c>>>=1, p>>>=1){
				{long d = a[p]; a[p] = a[c]; a[c] = d;}
				{int d = tags[p]; tags[p] = tags[c]; tags[c] = d;}
			}
		}
		
		public long poll()
		{
			if(pos == 1)return INF;
			pos--;
			long ret = a[1];
			a[1] = a[pos];
			a[pos] = INF;
			tags[1] = tags[pos];
			tags[pos] = -1;
			for(int c = 1;2*c < pos;){
				int smaller = a[2*c] < a[2*c+1] ? 2*c : 2*c+1;
				if(a[smaller] < a[c]){
					{long d = a[c]; a[c] = a[smaller]; a[smaller] = d;}
					{int d = tags[c]; tags[c] = tags[smaller]; tags[smaller] = d;}
					c = smaller;
				}else{
					break;
				}
			}
			return ret;
		}
		
		public long min() { return a[1]; }
		public int mintag() { return tags[1]; }
		public int size() { return pos-1; }
	}
	
	public static int[][][] packWD(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 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];
		}
		return g;
	}

	
	void run() throws Exception
	{
		is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
		out = new PrintWriter(System.out);
		
		long s = System.currentTimeMillis();
		solve();
		out.flush();
		if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");
//		Thread t = new Thread(null, null, "~", Runtime.getRuntime().maxMemory()){
//			@Override
//			public void run() {
//				long s = System.currentTimeMillis();
//				solve();
//				out.flush();
//				if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");
//			}
//		};
//		t.start();
//		t.join();
	}
	
	public static void main(String[] args) throws Exception { new N468_2().run(); }
	
	private byte[] inbuf = new byte[1024];
	public int lenbuf = 0, ptrbuf = 0;
	
	private 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 boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
	private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
	
	private double nd() { return Double.parseDouble(ns()); }
	private char nc() { return (char)skip(); }
	
	private 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 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 int[] na(int n)
	{
		int[] a = new int[n];
		for(int i = 0;i < n;i++)a[i] = ni();
		return a;
	}
	
	private long[] nal(int n)
	{
		long[] a = new long[n];
		for(int i = 0;i < n;i++)a[i] = nl();
		return a;
	}
	
	private 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 int[][] nmi(int n, int m) {
		int[][] map = new int[n][];
		for(int i = 0;i < n;i++)map[i] = na(m);
		return map;
	}
	
	private int ni() { return (int)nl(); }
	
	private 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) { System.out.println(Arrays.deepToString(o)); }
}
0