結果

問題 No.1364 [Renaming] Road to Cherry from Zelkova
ユーザー Example0911Example0911
提出日時 2021-01-24 16:41:39
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 191 ms / 2,500 ms
コード長 1,833 bytes
コンパイル時間 4,031 ms
コンパイル使用メモリ 218,672 KB
実行使用メモリ 29,256 KB
最終ジャッジ日時 2023-08-30 23:51:24
合計ジャッジ時間 15,632 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
10,400 KB
testcase_01 AC 4 ms
10,412 KB
testcase_02 AC 4 ms
10,544 KB
testcase_03 AC 4 ms
10,416 KB
testcase_04 AC 4 ms
10,452 KB
testcase_05 AC 4 ms
10,420 KB
testcase_06 AC 4 ms
10,556 KB
testcase_07 AC 4 ms
10,372 KB
testcase_08 AC 10 ms
11,428 KB
testcase_09 AC 7 ms
10,964 KB
testcase_10 AC 10 ms
11,468 KB
testcase_11 AC 9 ms
11,272 KB
testcase_12 AC 11 ms
11,496 KB
testcase_13 AC 103 ms
21,656 KB
testcase_14 AC 151 ms
26,344 KB
testcase_15 AC 149 ms
25,436 KB
testcase_16 AC 104 ms
21,828 KB
testcase_17 AC 48 ms
16,052 KB
testcase_18 AC 183 ms
27,068 KB
testcase_19 AC 179 ms
27,252 KB
testcase_20 AC 182 ms
27,308 KB
testcase_21 AC 182 ms
27,356 KB
testcase_22 AC 183 ms
27,452 KB
testcase_23 AC 38 ms
14,116 KB
testcase_24 AC 35 ms
15,356 KB
testcase_25 AC 121 ms
22,056 KB
testcase_26 AC 191 ms
29,256 KB
testcase_27 AC 114 ms
27,172 KB
testcase_28 AC 73 ms
18,564 KB
testcase_29 AC 104 ms
26,996 KB
testcase_30 AC 74 ms
18,956 KB
testcase_31 AC 50 ms
15,476 KB
testcase_32 AC 85 ms
25,128 KB
testcase_33 AC 164 ms
28,552 KB
testcase_34 AC 155 ms
25,224 KB
testcase_35 AC 133 ms
22,796 KB
testcase_36 AC 124 ms
22,656 KB
testcase_37 AC 76 ms
22,720 KB
testcase_38 AC 107 ms
26,612 KB
testcase_39 AC 105 ms
26,644 KB
testcase_40 AC 104 ms
26,532 KB
testcase_41 AC 105 ms
26,544 KB
testcase_42 AC 105 ms
26,744 KB
testcase_43 AC 64 ms
22,692 KB
testcase_44 AC 44 ms
19,036 KB
testcase_45 AC 59 ms
20,268 KB
testcase_46 AC 8 ms
12,856 KB
testcase_47 AC 5 ms
10,644 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;

using ll = long long;
using P = pair<ll, ll>;
const ll INF = (1LL << 61);
ll mod = (ll)1e9 + 7;
ll N, M;
vector<pair<ll, P>>E1[100010], E2[100010], G[100010];
ll dp[100010], dp2[100010];

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> N >> M;
	for (int i = 0; i < M; i++) {
		ll u, v, l, a; cin >> u >> v >> l >> a;
		E1[u].push_back({ v, {l, a} });
		E2[v].push_back({ u, {l, a} });
	}
	vector<bool>used(N+1), used2(N+1);
	used[0] = true;
	queue<int>q; q.push(0);
	while (!q.empty()) {
		int v = q.front(); q.pop();
		for (auto e : E1[v]) {
			if (!used[e.first]) {
				used[e.first] = true;
				q.push(e.first);
			}
		}
	}
	used2[N] = true;
	queue<int>q2; q2.push(N);
	while (!q2.empty()) {
		int v = q2.front(); q2.pop();
		for (auto e : E2[v]) {
			if (!used2[e.first]) {
				used2[e.first] = true;
				q2.push(e.first);
			}
		}
	}
	for (int i = 0; i <= N; i++) {
		if (used[i] && used2[i]) {
			for (auto e : E1[i]) {
				if (used[e.first] && used2[e.first]) {
					G[i].push_back(e);
				}
			}
		}
	}
	vector<ll>ans;
	vector<ll>ind(N + 1);
	for (int i = 0; i <= N; i++) {
		for (auto e : G[i]) {
			ind[e.first]++;
		}
	}
	queue<ll>que;
	for (int i = 0; i <= N; i++) {
		if (ind[i] == 0)que.push(i);
	}
	while (!que.empty()) {
		ll now = que.front(); ans.push_back(now); que.pop();
		for (auto e : G[now]) {
			ind[e.first]--;
			if (ind[e.first] == 0) {
				que.push(e.first);
			}
		}
	}
	if (ans.size() != N + 1) {
		cout << "INF" << endl; return 0;
	}
	dp[0] = 1;
	dp2[0] = 0;
	for (auto v : ans) {
		for (auto e : G[v]) {
			dp[e.first] += dp[v] * e.second.second;
			dp[e.first] %= mod;
			dp2[e.first] += dp2[v] * e.second.second + (dp[v] * e.second.first % mod) * e.second.second;
			dp2[e.first] %= mod;
		}
	}
	cout << dp2[N] << endl;
	return 0;
}
0