提交记录 9535


用户 题目 状态 得分 用时 内存 语言 代码长度
water_mi noip17c. 【NOIP2017】逛公园 Accepted 100 612.881 ms 27056 KB C++11 2.18 KB
提交时间 评测时间
2019-06-01 16:36:13 2020-08-01 01:39:22
#include <queue>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using std::min; using std::max;
using std::swap; using std::sort;
typedef long long ll;
typedef std::pair<int, int> pii;

template<typename T>
void read(T &x) {
    int flag = 1; x = 0; char ch = getchar();
    while(ch < '0' || ch > '9') { if(ch == '-') flag = -flag; ch = getchar(); }
    while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); x *= flag;
}
#if __cplusplus >= 201103l
template<typename T, typename... V>
void read(T &x, V&... v) { read(x), read(v...); }
#endif

const int N = 1e5 + 10, M = 2e5 + 10;
int cnt, from[N], to[M], nxt[M], dis[M];
struct Edge { int u, v, w; } d[M];
int T, n, m, k, p, f[100][N], dist[N];
inline void addEdge(int u, int v, int w) {
	to[++cnt] = v, nxt[cnt] = from[u], from[u] = cnt, dis[cnt] = w;
}
std::priority_queue<pii, std::vector<pii>, std::greater<pii>> Q;

void doit(int s) {
	memset(dist, 127, sizeof (dist[0]) * (n + 1));
	dist[s] = 0; Q.push({0, s});
	while(Q.size()) {
		pii u = Q.top(); Q.pop();
		if(u.first != dist[u.second]) continue;
		for(int i = from[u.second]; i; i = nxt[i]) {
			int v = to[i], w = dist[u.second] + dis[i];
			if(w < dist[v]) Q.push({dist[v] = w, v});
		}
	}
}

bool g[100][N], flag;
int dp(int k, int u) {
	if(k < 0) return 0;
	if(g[k][u]) { flag = 1; return 0; }
	if(~f[k][u]) return f[k][u];
	int &now = f[k][u]; now = 0; g[k][u] = 1;
	for(int i = from[u]; i; i = nxt[i]) {
		int v = to[i], w = dist[u] + k - dis[i];
		if(w - dist[v] > k) continue;
		(now += dp(w - dist[v], v)) %= p;
	}
	if(k == 0 && u == 1) now = 1;
	return g[k][u] = 0, now;
}

int main() {
	read(T);
	for(int Case = 1; Case <= T; ++Case) {
		read(n, m, k, p), flag = 0;
		cnt = 0, memset(from, 0, sizeof(from[0]) * (n + 1));
		for(int i = 1; i <= m; ++i)
			read(d[i].u, d[i].v, d[i].w), addEdge(d[i].u, d[i].v, d[i].w);
		for(int i = 0; i <= k; ++i)
			memset(f[i], -1, sizeof (f[i][0]) * (n + 1));
		doit(1);
		cnt = 0, memset(from, 0, sizeof(from[0]) * (n + 1));
		for(int i = 1; i <= m; ++i) addEdge(d[i].v, d[i].u, d[i].w);
		int ret = 0;
		for(int i = 0; i <= k; ++i) (ret += dp(i, n)) %= p;
		printf("%d\n", flag ? -1 : ret);
	}
    return 0;
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #143.86 us68 KBAcceptedScore: 10

Testcase #2358.08 us124 KBAcceptedScore: 10

Testcase #34.157 ms780 KBAcceptedScore: 10

Testcase #44.464 ms696 KBAcceptedScore: 10

Testcase #54.158 ms780 KBAcceptedScore: 10

Testcase #64.459 ms784 KBAcceptedScore: 10

Testcase #752.961 ms4 MB + 844 KBAcceptedScore: 10

Testcase #8411.503 ms24 MB + 324 KBAcceptedScore: 10

Testcase #9605.673 ms22 MB + 940 KBAcceptedScore: 10

Testcase #10612.881 ms26 MB + 432 KBAcceptedScore: 10


Judge Duck Online | 评测鸭在线
Server Time: 2024-04-27 16:55:02 | Loaded in 1 ms | Server Status
个人娱乐项目,仅供学习交流使用