当前位置: 代码迷 >> 综合 >> NOIP2017 D1T3 逛公园
  详细解决方案

NOIP2017 D1T3 逛公园

热度:85   发布时间:2024-01-09 11:37:18.0

逛公园

题目背景:

NOIP2017 D1T3

分析:记忆化搜索 + 最短路

 

竟然卡常数……因为考场上并没有调出来spfa的转移,所以这道题就只有30pts了·····下来才发现,这不是一道非常显然的记搜DP吗······真的不知道自己考场上在想点啥······定义状态dp[i][j]表示,到i,和最短路差值为j的方案数,先用dijkstra跑一遍最短路,然后再建反向边,跑一遍记忆化搜索即可,然后就是如何判定0环,考虑0环的性质,显然,如果存在一个0环意味着,你在搜索处理某一个状态的时候,会走回到自己,那么我们只需要标记一下系统栈当中目前有的状态,如果发现当前自己的状态已经在系统栈中,说明,存在0环,输出-1即可,然后还有就是记搜的边界问题,如果用i = 1,j = 0时,返回1是不正确的,因为如果1本身在一个0环当中,就会RE掉了,所以事先从1向0,连一条边,在i = 0,j = 0时返回1就可以了。

Source:

/*created by scarlyw
*/
#include <cstdio>
#include <string>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
#include <cctype>
#include <vector>
#include <set>
#include <queue>inline char read() {static const int IN_LEN = 1024 * 1024;static char buf[IN_LEN], *s, *t;if (s == t) {t = (s = buf) + fread(buf, 1, IN_LEN, stdin);if (s == t) return -1;}return *s++;
}///*
template<class T>
inline void R(T &x) {static char c;static bool iosig;for (c = read(), iosig = false; !isdigit(c); c = read()) {if (c == -1) return ;if (c == '-') iosig = true;	}for (x = 0; isdigit(c); c = read()) x = ((x << 2) + x << 1) + (c ^ '0');if (iosig) x = -x;
}
//*/const int OUT_LEN = 1024 * 1024;
char obuf[OUT_LEN], *oh = obuf;
inline void write_char(char c) {if (oh == obuf + OUT_LEN) fwrite(obuf, 1, OUT_LEN, stdout), oh = obuf;*oh++ = c;
}template<class T>
inline void W(T x) {static int buf[30], cnt;if (x == 0) write_char('0');else {if (x < 0) write_char('-'), x = -x;for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 + 48;while (cnt) write_char(buf[cnt--]);}
}inline void flush() {fwrite(obuf, 1, oh - obuf, stdout);
}/*
template<class T>
inline void R(T &x) {static char c;static bool iosig;for (c = getchar(), iosig = false; !isdigit(c); c = getchar())if (c == '-') iosig = true;	for (x = 0; isdigit(c); c = getchar()) x = ((x << 2) + x << 1) + (c ^ '0');if (iosig) x = -x;
}
//*/const int MAXN = 100000 + 10;
const int MAXK = 50 + 5;int n, m, x, y, k, t, mod;inline void add(int &x, int t) {x += t, (x >= mod) ? (x -= mod) : 0;
}struct node {int to, w;node(int to = 0, int w = 0) : to(to), w(w) {}inline bool operator < (const node &a) const {return w > a.w;}
} ;struct edges {int x, y, z;
} e[MAXN << 1];std::vector<node> edge[MAXN];inline void add_edge(int x, int y, int z) {edge[x].push_back(node(y, z));
}inline void read_in() {R(n), R(m), R(k), R(mod);for (int i = 1; i <= n; ++i) edge[i].clear();for (int i = 1; i <= m; ++i)R(e[i].x), R(e[i].y), R(e[i].z), add_edge(e[i].x, e[i].y, e[i].z);
}int dis[MAXN];
inline void dijkstra(int s) {static bool vis[MAXN];memset(dis, 127, sizeof(int) * (n + 5));memset(vis, false, sizeof(bool) * (n + 5));std::priority_queue<node> q;q.push(node(s, 0)), dis[s] = 0;while (!q.empty()) {while (!q.empty() && vis[q.top().to]) q.pop();if (q.empty()) break ;int cur = q.top().to;q.pop(), vis[cur] = true;for (int p = 0; p < edge[cur].size(); ++p) {node *e = &edge[cur][p];if (!vis[e->to] && dis[e->to] > dis[cur] + e->w) dis[e->to] = dis[cur] + e->w, q.push(node(e->to, dis[e->to]));}}
}int f[MAXN][MAXK];
bool vis[MAXN][MAXK];
inline void clear() {for (int i = 1; i <= n; ++i) edge[i].clear();for (int i = 1; i <= m; ++i) add_edge(e[i].y, e[i].x, e[i].z);add_edge(1, 0, 0), memset(f, -1, sizeof(f)), memset(vis, 0, sizeof(vis));dis[0] = 0;
}int dfs(int cur, int cur_dis) {int d = cur_dis - dis[cur];if (d < 0) return 0;if (~f[cur][d]) return f[cur][d];if (cur == 0 && cur_dis == 0) return 1;if (vis[cur][d]) return -1;vis[cur][d] = true;int ret = 0;for (int p = 0; p < edge[cur].size(); ++p) {node *e = &edge[cur][p];int res = dfs(e->to, cur_dis - e->w);if (~res) add(ret, res);else return -1;}return vis[cur][d] = false, f[cur][d] = ret;
}inline void solve() {read_in(), dijkstra(1), clear();int ans = 0;for (int i = 0; i <= k; ++i) {int res = dfs(n, dis[n] + i);if (~res) add(ans, res);else {std::cout << "-1\n";return ;}}std::cout << ans << '\n';
}int main() {R(t);while (t--) solve();return 0;
}