NOIP2017 D1T3 逛公园

逛公园

题目背景:

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;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你好!感谢你的提问。根据你的要求,我将为你解答关于NOIP2017普及t3问题的内容。 在这道题目中,我们需要解决一个关于棋盘的问题。题目描述了一个n×m的棋盘,每个格子上有一个非负整数。开始时,小明站在左上角的格子上,他可以向右或向下移动一步,每次移动的代价为目标格子上的数值。我们需要找到一条从左上角到右下角的路径,使得移动的总代价最小。 解决这个问题的一个常见的方法是使用动态规划(Dynamic Programming)。我们可以创建一个二维数组dp,其中dp[i][j]表示从起点到达坐标为(i, j)的格子时的最小代价。然后,我们可以按照从左上角到右下角的顺序依次计算dp数组的值。 具体的计算方法如下: 1. 首先,我们可以初始化dp数组的第一行和第一列,即dp[0][j]和dp[i][0],它们表示从起点到达第一行和第一列的格子时的最小代价。初始化的方法是累加前面的格子的代价。 2. 接下来,我们可以使用一个双重循环,从(1, 1)开始遍历整个棋盘。对于每个格子(i, j),我们可以选择从上方格子(i-1, j)或左方格子(i, j-1)中选择一个代价较小的路径,并加上当前格子的代价。即dp[i][j] = min(dp[i-1][j], dp[i][j-1]) + grid[i][j]。 3. 最后,当我们计算完dp数组的所有值后,dp[n-1][m-1]即为从起点到达右下角的格子时的最小代价。 这样,我们就可以得到从左上角到右下角的最小代价。希望能对你的问题有所帮助!如果你还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值