結果

問題 No.1364 [Renaming] Road to Cherry from Zelkova
ユーザー 37zigen37zigen
提出日時 2021-01-21 23:52:52
言語 Java19
(openjdk 21)
結果
AC  
実行時間 491 ms / 2,500 ms
コード長 3,936 bytes
コンパイル時間 3,109 ms
コンパイル使用メモリ 76,964 KB
実行使用メモリ 128,572 KB
最終ジャッジ日時 2023-08-28 10:59:36
合計ジャッジ時間 17,759 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
49,068 KB
testcase_01 AC 43 ms
48,864 KB
testcase_02 AC 42 ms
48,956 KB
testcase_03 AC 42 ms
48,752 KB
testcase_04 AC 44 ms
49,380 KB
testcase_05 AC 42 ms
48,928 KB
testcase_06 AC 43 ms
49,252 KB
testcase_07 AC 43 ms
48,944 KB
testcase_08 AC 88 ms
53,972 KB
testcase_09 AC 81 ms
52,120 KB
testcase_10 AC 89 ms
53,972 KB
testcase_11 AC 84 ms
53,924 KB
testcase_12 AC 88 ms
54,052 KB
testcase_13 AC 354 ms
82,756 KB
testcase_14 AC 325 ms
83,052 KB
testcase_15 AC 384 ms
83,004 KB
testcase_16 AC 300 ms
74,752 KB
testcase_17 AC 206 ms
71,612 KB
testcase_18 AC 491 ms
92,168 KB
testcase_19 AC 466 ms
91,656 KB
testcase_20 AC 469 ms
91,960 KB
testcase_21 AC 469 ms
91,792 KB
testcase_22 AC 448 ms
92,328 KB
testcase_23 AC 155 ms
61,004 KB
testcase_24 AC 148 ms
56,384 KB
testcase_25 AC 282 ms
78,124 KB
testcase_26 AC 405 ms
87,320 KB
testcase_27 AC 286 ms
76,768 KB
testcase_28 AC 234 ms
72,976 KB
testcase_29 AC 281 ms
74,280 KB
testcase_30 AC 224 ms
72,772 KB
testcase_31 AC 188 ms
67,652 KB
testcase_32 AC 227 ms
66,992 KB
testcase_33 AC 359 ms
86,044 KB
testcase_34 AC 401 ms
87,036 KB
testcase_35 AC 463 ms
93,304 KB
testcase_36 AC 361 ms
82,944 KB
testcase_37 AC 242 ms
67,948 KB
testcase_38 AC 331 ms
78,592 KB
testcase_39 AC 344 ms
78,068 KB
testcase_40 AC 343 ms
78,880 KB
testcase_41 AC 344 ms
77,712 KB
testcase_42 AC 344 ms
79,232 KB
testcase_43 AC 345 ms
128,572 KB
testcase_44 AC 256 ms
82,180 KB
testcase_45 AC 336 ms
123,736 KB
testcase_46 AC 74 ms
57,108 KB
testcase_47 AC 42 ms
49,076 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// 制約の確認

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.NoSuchElementException;

public class Main {
	public static void main(String[] args) {
		new Main().run();
	}
	
	final long p=(long)1e9+7;
	
	class Edge {
		int src, dst;
		long dist, cnt;
		
		public Edge(int src, int dst, long dist, long cnt) {
			this.src = src;
			this.dst = dst;
			this.dist = dist;
			this.cnt = cnt;
		}
	}
	
	void run() {
		FastScanner sc=new FastScanner();
		int N=sc.nextInt()+1;
		
		assert(1<=N-1&&N-1<=1e5);
		
		
		int M=sc.nextInt();
		
		
		assert(1<=M&&M<=2e5);

		
		ArrayList<Edge>[] g=new ArrayList[N];
		ArrayList<Edge>[] grev=new ArrayList[N];

		for (int i=0;i<g.length;++i) g[i]=new ArrayList<>();
		for (int i=0;i<g.length;++i) grev[i]=new ArrayList<>();

		for (int i=0;i<M;++i) {
			int u=sc.nextInt();
			int v=sc.nextInt();
			int l=sc.nextInt();
			int a=sc.nextInt();
			g[u].add(new Edge(u, v, l, a));
			grev[v].add(new Edge(v, u, l, a));
			
			
			assert(0<=u&&u<=N-1);
			assert(0<=v&&v<=N-1);
			assert(u!=v);
			assert(1<=l&&l<=1e9);
			assert(1<=a&&a<=1e9);
			
		}
		long[] ans=new long[N];
		long[] cnt=new long[N];
		cnt[N-1]=1;
		int[] state=new int[N];
		boolean[] a=new boolean[N];
		boolean[] b=new boolean[N];
		
		dfs0(0,g,a);
		dfs0(N-1,grev,b);
		for (int i=0;i<N;++i) a[i] &= b[i];
		
		Arrays.fill(state, 0);
		dfs(0, g, ans, state, cnt, a);
		System.out.println(ans[0]);
	}
	
	void dfs0(int cur, ArrayList<Edge>[] g, boolean[] valid) {
		valid[cur]=true;
		for (Edge e:g[cur]) {
			if (!valid[e.dst]) dfs0(e.dst,g,valid);
		}
	}
	
	void dfs(int cur, ArrayList<Edge>[] g, long[] ans, int[] state, long[] cnt, boolean[] ok) {
		state[cur]=1;
		for (Edge e:g[cur]) if (ok[e.dst]) {
			if (state[e.dst]==1) {
				System.out.println("INF");
				System.exit(0);
			}
			if (state[e.dst]==0) dfs(e.dst,g,ans,state,cnt,ok);
		}
		for (Edge e:g[cur]) if (ok[e.dst]){
			cnt[cur]+=cnt[e.dst]*e.cnt%p;
			cnt[cur]%=p;
			ans[cur]+=ans[e.dst]*e.cnt%p+cnt[e.dst]*e.cnt%p*e.dist%p;
			ans[cur]%=p;
		}
		state[cur]=2;
	}
	
	
	
	void tr(Object...objects) {
		System.out.println(Arrays.deepToString(objects));
	}
}

class FastScanner {
	private final InputStream in = System.in;
	private final byte[] buffer = new byte[1024];
	private int ptr = 0;
	private int buflen = 0;

	private boolean hasNextByte() {
		if (ptr < buflen) {
			return true;
		} else {
			ptr = 0;
			try {
				buflen = in.read(buffer);
			} catch (IOException e) {
				e.printStackTrace();
			}
			if (buflen <= 0) {
				return false;
			}
		}
		return true;
	}

	private int readByte() {
		if (hasNextByte())
			return buffer[ptr++];
		else
			return -1;
	}

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

	public boolean hasNext() {
		while (hasNextByte() && !isPrintableChar(buffer[ptr]))
			ptr++;
		return hasNextByte();
	}

	public String next() {
		if (!hasNext())
			throw new NoSuchElementException();
		StringBuilder sb = new StringBuilder();
		int b = readByte();
		while (isPrintableChar(b)) {
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}

	public long nextLong() {
		if (!hasNext())
			throw new NoSuchElementException();
		long n = 0;
		boolean minus = false;
		int b = readByte();
		if (b == '-') {
			minus = true;
			b = readByte();
		}
		if (b < '0' || '9' < b) {
			throw new NumberFormatException();
		}
		while (true) {
			if ('0' <= b && b <= '9') {
				n *= 10;
				n += b - '0';
			} else if (b == -1 || !isPrintableChar(b)) {
				return minus ? -n : n;
			} else {
				throw new NumberFormatException();
			}
			b = readByte();
		}
	}

	public int nextInt() {
		long nl = nextLong();
		if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE)
			throw new NumberFormatException();
		return (int) nl;
	}

	public double nextDouble() {
		return Double.parseDouble(next());
	}
}
0