結果

問題 No.1364 [Renaming] Road to Cherry from Zelkova
ユーザー Example0911Example0911
提出日時 2021-01-22 22:42:07
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,803 bytes
コンパイル時間 2,545 ms
コンパイル使用メモリ 213,652 KB
実行使用メモリ 22,024 KB
最終ジャッジ日時 2023-08-27 20:47:23
合計ジャッジ時間 11,454 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
7,264 KB
testcase_01 AC 4 ms
7,348 KB
testcase_02 AC 4 ms
7,280 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 31 ms
11,144 KB
testcase_24 AC 29 ms
9,828 KB
testcase_25 AC 74 ms
14,376 KB
testcase_26 AC 119 ms
17,768 KB
testcase_27 AC 85 ms
15,540 KB
testcase_28 AC 55 ms
12,268 KB
testcase_29 AC 82 ms
15,388 KB
testcase_30 AC 59 ms
12,884 KB
testcase_31 AC 39 ms
11,800 KB
testcase_32 AC 65 ms
14,696 KB
testcase_33 AC 110 ms
16,548 KB
testcase_34 AC 106 ms
16,456 KB
testcase_35 WA -
testcase_36 WA -
testcase_37 AC 64 ms
13,280 KB
testcase_38 TLE -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
権限があれば一括ダウンロードができます

ソースコード

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;
struct mint {
	ll x; // typedef long long ll;
	mint(ll x = 0) :x((x%mod + mod) % mod) {}
	mint operator-() const { return mint(-x); }
	mint& operator+=(const mint a) {
		if ((x += a.x) >= mod) x -= mod;
		return *this;
	}
	mint& operator-=(const mint a) {
		if ((x += mod - a.x) >= mod) x -= mod;
		return *this;
	}
	mint& operator*=(const mint a) {
		(x *= a.x) %= mod;
		return *this;
	}
	mint operator+(const mint a) const {
		mint res(*this);
		return res += a;
	}
	mint operator-(const mint a) const {
		mint res(*this);
		return res -= a;
	}
	mint operator*(const mint a) const {
		mint res(*this);
		return res *= a;
	}
	mint pow(ll t) const {
		if (!t) return 1;
		mint a = pow(t >> 1);
		a *= a;
		if (t & 1) a *= *this;
		return a;
	}

	// for prime mod
	mint inv() const {
		return pow(mod - 2);
	}
	mint& operator/=(const mint a) {
		return (*this) *= a.inv();
	}
	mint operator/(const mint a) const {
		mint res(*this);
		return res /= a;
	}
};
istream& operator>>(istream& is, mint& a) { return is >> a.x; }
ostream& operator<<(ostream& os, const mint& a) { return os << a.x; }
struct Edge {
	ll to;
};
using Graph = vector<vector<Edge>>;
/* topo_sort(G): グラフG をトポロジカルソート
	返り値: トポロジカルソートされた頂点番号
	計算量: O(|E|+|V|)
 */
vector<int> topo_sort(const Graph &G) {  // bfs
	vector<int> ans;
	int n = (int)G.size();
	vector<int> ind(n);            // ind[i]: 頂点iに入る辺の数(次数)
	for (int i = 0; i < n; i++) {  // 次数を数えておく
		for (auto e : G[i]) {
			ind[e.to]++;
		}
	}
	queue<int> que;
	for (int i = 0; i < n; i++) {  // 次数が0の点をキューに入れる
		if (ind[i] == 0) {
			que.push(i);
		}
	}
	while (!que.empty()) {  // 幅優先探索
		int now = que.front();
		ans.push_back(now);
		que.pop();
		for (auto e : G[now]) {
			ind[e.to]--;
			if (ind[e.to] == 0) {
				que.push(e.to);
			}
		}
	}
	return ans;
}
ll N, M;
mint dp[100010], dp2[100010];
vector<pair<ll, P>>graph[100010];
void dfs(int v) {
	for (auto nv : graph[v]) {
		dp[nv.first] += dp[v];
		dp[nv.first] += (nv.second.first * nv.second.second % mod) * dp2[v].x;
		dp2[nv.first] = dp2[v].x * nv.second.second;
		dfs(nv.first);
	}
	if(v != N)dp[v] = 0;

}
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> N >> M;
	Graph G(N+1);
	for (int i = 0; i < M; i++) {
		ll u, v, l, a; cin >> u >> v >> l >> a;
		G[u].push_back({v});
		graph[u].push_back({ v, {l, a} });
	}
	auto ans = topo_sort(G);
	if (ans.size() != N + 1) {
		cout << "INF" << endl; return 0;
	}
	dp[ans[0]] = 0;
	dp2[ans[0]] = 1;
	dfs(ans[0]);
	mint ans2 = dp[N];
	cout << ans2 << endl;
	return 0;
}
0