NC22594 Rinne Loves Graph

题意:

有n个城镇,m条边,每个城镇有一个值,1表示看守***,0表示无看守,你只能穿过看守点 k 点,问你能不能从1号点到达 n 号点,并且输出满足穿过看守点不超过 k 次的最快到达 n 号点的时间

做法:

这个题目实际上和 https://ac.nowcoder.com/acm/problem/14700 是一样的,一个是天数一个是穿过次数,本质上都是分层图,将相同穿过次数之后的n个点看作一层图,然后再来考虑每层图之间的状态转移即可,最后,算一下你在穿过次数小于等于 k 的前提下,到达 n 号点的最短距离即可,注意判断不可到达的情况

代码:

#include <bits/stdc++.h>
#define ll long long
#define sc scanf
#define pr printf
using namespace std;
const int MAXN = 1005;
struct edge
{
    int to;
    ll w;
    int nex;
}e[MAXN * 8];
int head[MAXN], tot;
void init()
{
    fill(head, head + MAXN, -1);
    tot = 1;
}
void add(int a, int b, ll c)
{
    e[tot] = edge{ b,c,head[a] };
    head[a] = tot++;
}
int h[MAXN];
bool book[11][MAXN];
ll dis[11][MAXN];
struct node
{
    int now;
    ll w;
    int cnt;
    bool operator <(const node& b) const {
        return w > b.w;
    }
};
void dij()
{
    for (int i = 0; i < 11; i++)
        for (int j = 0; j < MAXN; j++)
            dis[i][j] = 1e18;
    priority_queue<node>q;
    dis[0][1] = 0;
    q.push(node{ 1,0,0 });
    while (!q.empty())
    {
        node u = q.top();
        q.pop();
        if (book[u.cnt][u.now])
            continue;
        book[u.cnt][u.now] = true;
        for (int i = head[u.now]; i + 1; i = e[i].nex)
        {
            int v = e[i].to;
            if (u.cnt + h[u.now] > 10)
                continue;
            if (dis[u.cnt + h[u.now]][v] > u.w + e[i].w)
            {
                dis[u.cnt + h[u.now]][v] = u.w + e[i].w;
                q.push(node{ v,dis[u.cnt + h[u.now]][v] ,u.cnt + h[u.now] });
            }
        }
    }
}
int main()
{
    init();
    int n, m, k;
    sc("%d%d%d", &n, &m, &k);
    for (int i = 1; i <= n; i++)
        sc("%d", &h[i]);
    //h[n] = 0;
    while (m--)
    {
        int a, b; ll c;
        sc("%d%d%lld", &a, &b, &c);
        add(a, b, c);
        add(b, a, c);
    }
    dij();
    ll ans = 1e18;
    for (int i = 0; i <= k; i++)
        ans = min(ans, dis[i][n]);
    if (ans == 1e18)
        ans = -1;
    pr("%lld\n", ans);
}


全部评论

相关推荐

zephory:内容太乱了,根本捕捉不到重点,指导你会的很多,但是看不到具体的强项 个人技能宜精不宜多 项目那块太繁琐了,面试官或者hr只想知道你在项目中看了啥以及具体的收益
点赞 评论 收藏
分享
团子请爱我一次_十月...:不是戈门,干哪来了,这就是java嘛
点赞 评论 收藏
分享
10-14 21:00
门头沟学院 Java
吃花椒的狸猫:这个人说的倒是实话,特别是小公司,一个实习生哪里来的那么多要求
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务