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);
}
查看8道真题和解析

联想公司福利 1500人发布