Miraclys

一言(ヒトコト)

洛谷P3953 逛公园

\(\large{题目链接}\)
\(\\\)
\(\Large\textbf{Solution: } \large{首先跑一遍最短路, 然后发现k特别小,考虑dp。\\设f[i][j]表示到第i个点,比最短路大j的路径个数。\\容易想到u -> v的转移即为 f[v][dis[u] + j + e[i].w - dis[v]] += f[u][j],其中dis[u]表示1到u的最短路。\\需要注意,j是按照从小到大转移到,所以需要先将所有点按照dis排序再进行转移。\\至于0环的情况,我还不会,咕咕咕。}\)

\(\Large\textbf{Code: }\)

//73分
#include <bits/stdc++.h>
#define gc() getchar() 
#define LL long long
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
using namespace std;
const LL N = 1e5 + 5;
const LL M = 2e5 + 5;
const LL inf = 0x7fffffff;
LL t, n, m, p, k, cnt, vis[N], head[N], dis[N];
LL f[N][55];

struct Edge {
	LL to, next, val;	
}e[M]; 

struct Node {
	LL n, d;	
	friend bool operator < (Node a, Node b) {
		return a.d > b.d;
	}
};

struct Dp {
	LL n, d;
	friend bool operator < (Dp a, Dp b) {
		return a.d < b.d;
	}	
}dp[N];

inline LL read() {
	LL x = 0;
	char ch = gc();
	while (!isdigit(ch)) ch = gc();
	while (isdigit(ch)) x = x * 10 + ch - '0', ch = gc();
	return x; 
}

inline void add(LL x, LL y, LL w) {
	e[++cnt].to = y;
	e[cnt].val = w;
	e[cnt].next = head[x];
	head[x] = cnt;
}

inline void Dijkstra() {
	priority_queue<Node> q;
	memset(vis, 0, sizeof (vis));
	rep(i, 1, n) dis[i] = inf; dis[1] = 0;
	q.push((Node) {1, 0});
	while (!q.empty()) {
		Node cur = q.top(); q.pop();
		LL x = cur.n;
		if (vis[x]) continue;
		vis[x] = 1;
		for (LL i = head[x]; i ; i = e[i].next) {
			LL u = e[i].to;
			if (vis[u]) continue;
			if (dis[u] > dis[x] + e[i].val) dis[u] = dis[x] + e[i].val, q.push((Node) {u, dis[u]});
		}
	}
}

inline void DP() {
	memset(f, 0, sizeof (f));
	f[1][0] = 1; 
	for (LL j = 0; j <= k; ++j) {
		for (LL u = 1; u <= n; ++u) {
			for (LL i = head[dp[u].n]; i ; i = e[i].next) {
				LL v = e[i].to, x = dp[u].n, cur = dis[x] + j + e[i].val - dis[v];
				if (cur <= k) f[v][cur] = (f[v][cur] + f[x][j]) % p;
			}
		}
	}
}
/*f[i][j] 表示到i超出j的方案数 
f[v][dis[u] + j + e[i].w - dis[v]] = f[u][j];*/

int main() {
	t = read();
	while (t--) {
		n = read(), m = read(), k = read(), p = read();
		LL x, y, w; cnt = 0; memset(head, 0, sizeof (head));
		while (m--) x = read(), y = read(), w = read(), add(x, y, w);
		Dijkstra();
		rep(i, 1, n) dp[i].n = i, dp[i].d = dis[i];
		sort(dp + 1, dp + 1 + n);//rep(i, 0, k) cout << f[n][i] << endl;
		DP();
		LL ans = 0;
		//rep(i, 0, k) cout << f[n][i] << endl;
		rep(i, 0, k) ans = (ans + f[n][i]) % p;
		printf("%lld\n", ans);
	} 
	return 0;
} 
posted @ 2020-03-25 22:17  Miraclys  阅读(99)  评论(0编辑  收藏  举报

关于本博客样式

部分创意和图片借鉴了

BNDong

的博客,在此感谢