結果
| 問題 |
No.1364 [Renaming] Road to Cherry from Zelkova
|
| コンテスト | |
| ユーザー |
Example0911
|
| 提出日時 | 2021-01-22 22:42:07 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,803 bytes |
| コンパイル時間 | 4,290 ms |
| コンパイル使用メモリ | 206,724 KB |
| 最終ジャッジ日時 | 2025-01-18 04:57:57 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 17 WA * 23 TLE * 5 |
ソースコード
#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;
}
Example0911