Submission #616859


Source Code Expand

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(), s = ni(), t = ni();
		int[][] rails = new int[m][];
		int[][] ds = new int[m][];
		int[][] dcums = new int[m][];
		int ne = 0;
		for(int z = 0;z < m;z++){
			int L = ni();
			rails[z] = na(L);
			ds[z] = na(L-1);
			ne += L-1;
			dcums[z] = new int[L];
			for(int i = 0;i < L-1;i++){
				dcums[z][i+1] = dcums[z][i] + ds[z][i];
			}
		}
		
		int[] from = new int[ne*2];
		int[] to = new int[ne*2];
		int[] w = new int[ne*2];
		int[] rs = new int[ne*2];
		int[] rind = new int[ne*2];
		int[] dir = new int[ne*2];
		int p = 0;
		for(int i = 0;i < m;i++){
			for(int j = 0;j < rails[i].length-1;j++){
				from[p] = rails[i][j];  to[p] = rails[i][j+1]; w[p] = ds[i][j]; rs[p] = i; rind[p] = j; dir[p] = 1; p++;
				to[p] = rails[i][j];  from[p] = rails[i][j+1]; w[p] = ds[i][j]; rs[p] = i; rind[p] = j+1; dir[p] = -1; p++;
			}
		}
		int[][][] g = packWD(n, from, to, p, w, rs, rind, dir);
		long[] dt = dijkl(g, t);
		
		long low = 0, high = Integer.MAX_VALUE;
		while(high - low > 1){
			long h = high + low>>>1;
			if(go(h, g, s, t, dt, dcums, rails)){
				high = h;
			}else{
				low = h;
			}
		}
		out.println(high);
	}
	
	static boolean go(long h, int[][][] g, int from, int to, long[] dt, int[][] dcums, int[][] rails)
	{
		int n = g.length;
		long[] td = new long[n];
		
		long I = Long.MAX_VALUE / 2;
		Arrays.fill(td, Long.MAX_VALUE / 2);
		MinHeapL q = new MinHeapL(n);
		q.add(from, 0);
		td[from] = 0;
		
		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];
				long loss = nd;
				if(e[4] == 1){
					loss += dcums[e[2]][dcums[e[2]].length-1]-dcums[e[2]][e[3]+1];
					loss += dt[rails[e[2]][dcums[e[2]].length-1]];
				}else{
					loss += dcums[e[2]][e[3]-1];
					loss += dt[rails[e[2]][0]];
				}
				if(nd < td[next] && loss <= h){
					td[next] = nd;
					q.update(next, nd);
				}
			}
		}
		
		return td[to] < I;
	}
	
	public static long[] dijkl(int[][][] g, int from)
	{
		int n = g.length;
		long[] td = new long[n];
		
		Arrays.fill(td, Long.MAX_VALUE / 2);
		MinHeapL q = new MinHeapL(n);
		q.add(from, 0);
		td[from] = 0;
		
		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);
				}
			}
		}
		
		return td;
	}
	
	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[][][] packWD(int n, int[] from, int[] to, int sup, int[]... ws)
	{
		int[][][] g = new int[n][][];
		int[] p = new int[n];
		for(int i = 0;i < sup;i++)p[from[i]]++;
		for(int i = 0;i < n;i++)g[i] = new int[p[i]][ws.length+1];
		for(int i = 0;i < sup;i++){
			--p[from[i]];
			g[from[i]][p[from[i]]][0] = to[i];
			for(int j = 0;j < ws.length;j++)g[from[i]][p[from[i]]][j+1] = ws[j][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];
	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 boolean isSpaceChar(int c) { return !(c >= 32 && 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))){
			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)); }
}

Submission Info

Submission Time
Task C - メンテナンス明け
User uwi
Language Java (OpenJDK 1.7.0)
Score 100
Code Size 8396 Byte
Status AC
Exec Time 903 ms
Memory 44888 KB

Judge Result

Set Name Subtask1 Subtask2
Score / Max Score 50 / 50 50 / 50
Status
AC × 52
AC × 6
Set Name Test Cases
Subtask1 small/00_sample00, small/00_sample01, small/00_sample02, small/10_small-0000, small/10_small-0001, small/10_small-0002, small/10_small-0003, small/10_small-0004, small/10_small-0005, small/10_small-0006, small/10_small-0007, small/10_small-0008, small/10_small-0009, small/10_small-0010, small/10_small-0011, small/10_small-0012, small/10_small-0013, small/10_small-0014, small/10_small-0015, small/10_small-0016, small/10_small-0017, small/10_small-0018, small/10_small-0019, small/30_max_small, small/40_simple_0000, small/40_simple_0001, small/40_simple_0002, small/40_simple_0003, small/40_simple_0004, small/40_simple_0005, small/40_simple_0006, small/40_simple_0007, small/40_simple_0008, small/40_simple_0009, small/40_simple_0010, small/40_simple_0011, small/40_simple_0012, small/40_simple_0013, small/40_simple_0014, small/40_simple_0015, small/40_simple_0016, small/40_simple_0017, small/40_simple_0018, small/40_simple_0019, small/90_dijkstra_killer_00, small/90_dijkstra_killer_01, small/91_tayama_killer_00, small/91_tayama_killer_01, small/91_tayama_killer_02, small/91_tayama_killer_03, small/91_tayama_killer_04, small/91_tayama_killer_05
Subtask2 large/20_large-00, large/20_large-01, large/20_large-02, large/20_large-03, large/20_large-04, large/31_max_large
Case Name Status Exec Time Memory
large/20_large-00 AC 872 ms 44132 KB
large/20_large-01 AC 865 ms 44888 KB
large/20_large-02 AC 889 ms 44360 KB
large/20_large-03 AC 889 ms 44456 KB
large/20_large-04 AC 903 ms 44320 KB
large/31_max_large AC 587 ms 39644 KB
small/00_sample00 AC 308 ms 22796 KB
small/00_sample01 AC 305 ms 22800 KB
small/00_sample02 AC 307 ms 22796 KB
small/10_small-0000 AC 363 ms 25896 KB
small/10_small-0001 AC 372 ms 26444 KB
small/10_small-0002 AC 359 ms 25936 KB
small/10_small-0003 AC 366 ms 26148 KB
small/10_small-0004 AC 370 ms 26232 KB
small/10_small-0005 AC 361 ms 25748 KB
small/10_small-0006 AC 359 ms 26356 KB
small/10_small-0007 AC 364 ms 26184 KB
small/10_small-0008 AC 359 ms 25948 KB
small/10_small-0009 AC 357 ms 25624 KB
small/10_small-0010 AC 359 ms 26028 KB
small/10_small-0011 AC 357 ms 25716 KB
small/10_small-0012 AC 380 ms 26712 KB
small/10_small-0013 AC 368 ms 26176 KB
small/10_small-0014 AC 358 ms 25880 KB
small/10_small-0015 AC 375 ms 26332 KB
small/10_small-0016 AC 364 ms 26200 KB
small/10_small-0017 AC 383 ms 26384 KB
small/10_small-0018 AC 371 ms 26340 KB
small/10_small-0019 AC 360 ms 25720 KB
small/30_max_small AC 335 ms 23904 KB
small/40_simple_0000 AC 308 ms 22804 KB
small/40_simple_0001 AC 304 ms 22756 KB
small/40_simple_0002 AC 342 ms 22752 KB
small/40_simple_0003 AC 307 ms 22752 KB
small/40_simple_0004 AC 306 ms 22852 KB
small/40_simple_0005 AC 307 ms 22800 KB
small/40_simple_0006 AC 304 ms 22748 KB
small/40_simple_0007 AC 303 ms 22760 KB
small/40_simple_0008 AC 300 ms 22736 KB
small/40_simple_0009 AC 309 ms 22836 KB
small/40_simple_0010 AC 309 ms 22804 KB
small/40_simple_0011 AC 303 ms 22744 KB
small/40_simple_0012 AC 303 ms 22800 KB
small/40_simple_0013 AC 419 ms 22784 KB
small/40_simple_0014 AC 310 ms 22796 KB
small/40_simple_0015 AC 310 ms 22804 KB
small/40_simple_0016 AC 326 ms 22788 KB
small/40_simple_0017 AC 309 ms 22820 KB
small/40_simple_0018 AC 311 ms 22816 KB
small/40_simple_0019 AC 299 ms 22800 KB
small/90_dijkstra_killer_00 AC 316 ms 22856 KB
small/90_dijkstra_killer_01 AC 470 ms 22860 KB
small/91_tayama_killer_00 AC 307 ms 22776 KB
small/91_tayama_killer_01 AC 299 ms 22780 KB
small/91_tayama_killer_02 AC 300 ms 22828 KB
small/91_tayama_killer_03 AC 301 ms 22852 KB
small/91_tayama_killer_04 AC 308 ms 22800 KB
small/91_tayama_killer_05 AC 310 ms 22872 KB