P1685 题解
题目分析
这是一道借助拓扑排序实现递推的题。
首先我们考虑递推式子:我们定义
递推的过程可以通过拓扑排序来实现,因为拓扑排序总是将入度为
最后我们只要输出
代码
注意一下关键字
#include <bits/stdc++.h>
#define int long long
#define maxn 10005
#define inf 1e9
#define mod 10000
using namespace std;
inline int read() { //来自 xjz 巨佬的快读
int x = 0, f = 1; char c = getchar();
while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); }
while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); }
return x * f;
}
int n, m, s, t, t0;
int ct[maxn], tme[maxn];
int in[maxn];
struct edge {
int v, w, nxt;
}e[maxn];
int head[maxn], cnt;
void add(int u, int v, int w) {
e[++cnt].v = v, e[cnt].w = w;
e[cnt].nxt = head[u], head[u] = cnt;
}
signed main() {
n = read(), m = read(), s = read(), t = read(), t0 = read();
for (int i = 1; i <= m; i++) {
int u = read(), v = read(), w = read();
add(u, v, w);
in[v]++;
}
//拓扑排序
queue<int> q;
for (int i = 1; i <= n; i++)
if (in[i] == 0) q.push(i), ct[i] = 1;
while (!q.empty()) {
int x = q.front();
q.pop();
for (int i = head[x]; i; i = e[i].nxt) {
int y = e[i].v, z = e[i].w;
//递推式子,上面有解释,注意实时模 mod ,一个也不能少
tme[y] = (tme[y] + tme[x] + ct[x] * z) % mod;
ct[y] = (ct[y] + ct[x]) % mod;
if (--in[y] == 0) q.push(y);
}
}
cout << (tme[t] + (ct[t] - 1) * t0 % mod) % mod;
}
完结撒花——