ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#462 | #84. 「NOIP2017」逛公园 | Pigsyy | 100 | 2613ms | 28596kb | C++23 | 3.3kb | 2025-04-24 20:41:38 | 2025-04-24 20:41:39 |
answer
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
typedef long long ll;
template<typename T>
void read(T &num) {
int ch = getchar();
num = 0;
while (ch < 48 || ch > 57)
ch = getchar();
while (ch >= 48 && ch <= 57)
num = (num << 3) + (num << 1) + (ch ^ 48), ch = getchar();
}
const int maxn = 100005, maxm = 200005;
int T, n, m, k, p;
int head[maxn], total;
struct Edge {
int to, next, len;
} e[maxm];
void add(int u, int v, int w) {
e[++total] = Edge{v, head[u], w};
head[u] = total;
}
void clear() {
memset(head, 0, sizeof(int) * (n + 3));
total = 0;
}
struct Input {
int u, v, w;
} a[maxm];
bool vis[maxn];
ll dis_1[maxn], dis_n[maxn];
std::priority_queue<std::pair<ll, int>>q;
void dijkstra(int s, ll *dis) {
memset(vis, 0, sizeof(bool) * (n + 3));
memset(dis, 31, sizeof(ll) * (n + 3));
dis[s] = 0;
q.push({0, s});
while (!q.empty()) {
int u = q.top().second;
q.pop();
if (vis[u])
continue;
vis[u] = true;
for (int i = head[u]; i; i = e[i].next) {
int v = e[i].to;
if (dis[v] > dis[u] + e[i].len) {
dis[v] = dis[u] + e[i].len;
q.push({-dis[v], v});
}
}
}
}
int degree[maxn], id[maxn];
int queue[maxn], front, tail;
void toposort() {
front = 1, tail = 0;
queue[++tail] = 1;
int now = 0;
while (front <= tail) {
int u = queue[front++];
id[++now] = u;
for (int i = head[u]; i; i = e[i].next) {
int v = e[i].to;
if ((--degree[v]) == 0)
queue[++tail] = v;
}
}
}
int dp[maxn][51];
int solve() {
read(n), read(m), read(k), read(p);
clear();
for (int i = 1; i <= m; i++) {
read(a[i].u), read(a[i].v), read(a[i].w);
add(a[i].u, a[i].v, a[i].w);
}
dijkstra(1, dis_1);
clear();
for (int i = 1; i <= m; i++)
add(a[i].v, a[i].u, a[i].w);
dijkstra(n, dis_n);
clear();
memset(degree, 0, sizeof(int) * (n + 3));
memset(id, 0, sizeof(int) * (n + 3));
for (int i = 1; i <= m; i++)
if (dis_1[a[i].u] + a[i].w + dis_n[a[i].v] <= dis_1[n] + k && dis_1[a[i].u] + a[i].w == dis_1[a[i].v]) {
add(a[i].u, a[i].v, 0);
degree[a[i].v]++;
}
for (int i = 1; i <= m; i++)
a[i].w = dis_1[a[i].u] + a[i].w - dis_1[a[i].v];
toposort();
for (int i = 1; i <= n; i++)
if (degree[i])
return -1;
memset(dp, 0, sizeof(int) * 51 * (n + 3));
dp[1][0] = 1;
for (int j = 0; j <= k; j++) {
for (int x = 1; x <= n; x++) {
int u = id[x];
for (int i = head[u]; i; i = e[i].next) {
int v = e[i].to;
dp[v][j] = (dp[v][j] + dp[u][j]) % p;
}
}
for (int i = 1; i <= m; i++)
if (a[i].w && j + a[i].w <= k)
dp[a[i].v][j + a[i].w] = (dp[a[i].v][j + a[i].w] + dp[a[i].u][j]) % p;
}
ll answer = 0;
for (int i = 0; i <= k; i++)
answer += dp[n][i];
return answer % p;
}
int main() {
read(T);
while (T--)
printf("%d\n", solve());
return 0;
}
详细
小提示:点击横条可展开更详细的信息
Test #1:
score: 10
Accepted
time: 2ms
memory: 10988kb
input:
5 5 10 0 624775377 1 2 1 2 5 2 2 4 1 5 4 2 4 2 1 4 5 2 2 3 1 3 2 1 1 3 2 3 5 1 5 10 0 511184987 1 4 ...
output:
3 1 2 3 1
result:
ok 5 tokens
Test #2:
score: 10
Accepted
time: 3ms
memory: 10936kb
input:
5 80 2000 0 631649440 1 46 1 58 80 2 46 8 2 8 62 1 62 48 1 48 43 2 43 75 2 75 34 1 34 25 1 25 6 1 6 ...
output:
2 1 2 2 5
result:
ok 5 tokens
Test #3:
score: 10
Accepted
time: 13ms
memory: 11200kb
input:
5 323 2000 50 87927407 1 248 7 90 323 3 248 241 4 241 304 7 304 10 4 10 305 2 305 106 8 106 45 8 45 ...
output:
51522827 2988598 54529 356722918 2482940
result:
ok 5 tokens
Test #4:
score: 10
Accepted
time: 5ms
memory: 11104kb
input:
5 621 2000 50 895097246 1 302 9 437 621 3 302 8 8 8 303 4 303 104 10 104 555 1 555 203 7 203 290 3 2...
output:
1980630 217591923 270559256 296386552 6181645
result:
ok 5 tokens
Test #5:
score: 10
Accepted
time: 7ms
memory: 11136kb
input:
5 498 2000 50 707357691 1 350 9 476 498 6 350 249 6 249 242 2 242 382 7 382 306 3 306 486 4 486 13 2...
output:
5687543 394833049 16902 145963440 80323868
result:
ok 5 tokens
Test #6:
score: 10
Accepted
time: 10ms
memory: 11052kb
input:
5 269 2000 50 646160359 1 248 7 254 269 4 248 241 9 241 9 3 9 104 4 104 43 4 43 203 0 203 24 8 24 24...
output:
150264746 -1 2746956 296106 407995859
result:
ok 5 tokens
Test #7:
score: 10
Accepted
time: 205ms
memory: 11912kb
input:
5 1962 200000 0 859981312 1 862 3 1591 1962 2 862 1785 2 1785 754 1 754 1918 1 1918 1330 1 1330 12 1...
output:
17 69 2 4 1
result:
ok 5 tokens
Test #8:
score: 10
Accepted
time: 864ms
memory: 26544kb
input:
3 75195 200000 50 361302901 1 40696 2 6304 75195 3 40696 73596 3 73596 13616 7 13616 65546 2 65546 1...
output:
15190 308007794 13050905
result:
ok 3 tokens
Test #9:
score: 10
Accepted
time: 734ms
memory: 26524kb
input:
3 13019 200000 50 810576281 1 7928 9 5519 13019 0 7928 8060 3 8060 9 1 9 12262 5 12262 3008 6 3008 2...
output:
-1 7941111 742832979
result:
ok 3 tokens
Test #10:
score: 10
Accepted
time: 770ms
memory: 28596kb
input:
3 82773 200000 50 582580818 1 40696 6 49256 82773 9 40696 73596 6 73596 13616 0 13616 65546 6 65546 ...
output:
682378 322352839 -1
result:
ok 3 tokens