結果

問題 No.1364 [Renaming] Road to Cherry from Zelkova
ユーザー nikkukunnikkukun
提出日時 2021-01-23 15:02:33
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 169 ms / 2,500 ms
コード長 1,678 bytes
コンパイル時間 1,965 ms
コンパイル使用メモリ 172,488 KB
実行使用メモリ 19,796 KB
最終ジャッジ日時 2023-08-29 01:55:15
合計ジャッジ時間 11,193 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
8,036 KB
testcase_01 AC 3 ms
8,116 KB
testcase_02 AC 4 ms
8,052 KB
testcase_03 AC 4 ms
8,052 KB
testcase_04 AC 4 ms
8,292 KB
testcase_05 AC 3 ms
8,032 KB
testcase_06 AC 4 ms
8,052 KB
testcase_07 AC 3 ms
8,036 KB
testcase_08 AC 9 ms
8,600 KB
testcase_09 AC 6 ms
8,436 KB
testcase_10 AC 9 ms
8,676 KB
testcase_11 AC 8 ms
8,508 KB
testcase_12 AC 10 ms
8,636 KB
testcase_13 AC 93 ms
14,212 KB
testcase_14 AC 137 ms
16,836 KB
testcase_15 AC 133 ms
16,040 KB
testcase_16 AC 91 ms
14,248 KB
testcase_17 AC 44 ms
11,488 KB
testcase_18 AC 169 ms
17,156 KB
testcase_19 AC 169 ms
17,012 KB
testcase_20 AC 164 ms
17,440 KB
testcase_21 AC 160 ms
17,284 KB
testcase_22 AC 166 ms
17,560 KB
testcase_23 AC 31 ms
10,420 KB
testcase_24 AC 31 ms
9,792 KB
testcase_25 AC 82 ms
13,600 KB
testcase_26 AC 130 ms
16,688 KB
testcase_27 AC 89 ms
14,836 KB
testcase_28 AC 58 ms
12,116 KB
testcase_29 AC 86 ms
14,428 KB
testcase_30 AC 60 ms
12,280 KB
testcase_31 AC 41 ms
11,124 KB
testcase_32 AC 68 ms
13,544 KB
testcase_33 AC 120 ms
16,020 KB
testcase_34 AC 121 ms
15,460 KB
testcase_35 AC 117 ms
14,824 KB
testcase_36 AC 105 ms
14,292 KB
testcase_37 AC 66 ms
12,824 KB
testcase_38 AC 83 ms
14,112 KB
testcase_39 AC 82 ms
13,968 KB
testcase_40 AC 88 ms
14,228 KB
testcase_41 AC 85 ms
13,936 KB
testcase_42 AC 86 ms
14,024 KB
testcase_43 AC 58 ms
19,796 KB
testcase_44 AC 43 ms
14,636 KB
testcase_45 AC 54 ms
18,028 KB
testcase_46 AC 4 ms
8,056 KB
testcase_47 AC 3 ms
8,188 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define lch (o << 1)
#define rch (o << 1 | 1)

typedef double db;
typedef long long ll;
typedef unsigned int ui;
typedef pair<int, int> pint;
typedef tuple<int, int, int> tint;

const int N = 1e5 + 5;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const ll INF_LL = 0x3f3f3f3f3f3f3f3f;

struct Adj {
	int v, w, s;
};

int ind[N];
vector<Adj> a[N], ia[N];
bool okSt[N], okEd[N];

void dfs(int u, int isSt) {
	(isSt ? okSt[u] : okEd[u]) = 1;
	for (auto e: (isSt ? a[u] : ia[u])) {
		if (isSt ? okSt[e.v] : okEd[e.v]) continue;
		dfs(e.v, isSt);
	}
}

ll sumLS[N], sumS[N];

int main() {
	ios::sync_with_stdio(0);

	int n, m;
	cin >> n >> m;
	for (int i = 1; i <= m; i++) {
		int u, v, w, s;
		cin >> u >> v >> w >> s;
		a[u].push_back((Adj){v, w, s});
		ia[v].push_back((Adj){u, w, s});
	}

	dfs(0, 1);
	dfs(n, 0);

	for (int u = 0; u <= n; u++) {
		if (!okSt[u] || !okEd[u]) continue;
		for (auto e: a[u])
			ind[e.v]++;
	}

	if (ind[0] > 0) {
		cout << "INF" << endl;
		return 0;
	}

	// topo
	queue<int> q;
	q.push(0);
	sumS[0] = 1;

	while (!q.empty()) {
		int u = q.front();
		q.pop();
		for (auto e: a[u]) {
			ind[e.v]--;
			if (ind[e.v] == 0)
				q.push(e.v);

			// cal
			sumLS[e.v] = (sumLS[e.v] + sumLS[u] * e.s + sumS[u] * e.w % MOD * e.s) % MOD;
			sumS[e.v] = (sumS[e.v] + sumS[u] * e.s) % MOD;
		}
	}

	bool flag = 0;
	for (int i = 0; i <= n; i++)
		if (okSt[i] && okEd[i] && ind[i])
			flag = 1;
	if (flag) {
		cout << "INF" << endl;
		return 0;
	}

	ll ans = sumLS[n];
	ans = (ans % MOD + MOD) % MOD;
	cout << ans << endl;

	return 0;
}
0