luogu P3946 ことりのおやつ 题解
Posted misakaazusa
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了luogu P3946 ことりのおやつ 题解相关的知识,希望对你有一定的参考价值。
题目链接:https://www.luogu.org/problemnew/show/P3946
交好几遍是因为虽然能过一直有提醒..强迫症qwq
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 5 * 1e6 + 10;
ll n, m, s, t, g, q;
ll dis[maxn], h[maxn], l[maxn];
bool vis[maxn];
struct edge{
ll from, to, next, len;
}e[maxn<<2];
ll head[maxn], cnt;
queue<int> Q;
void add(ll u, ll v, ll w)
{
e[++cnt].from = u;
e[cnt].len = w;
e[cnt].next = head[u];
e[cnt].to = v;
head[u] = cnt;
}
void SPFA()
{
while(!Q.empty())
{
ll now = Q.front(); Q.pop();
vis[now] = 0;
for(int i = head[now]; i != -1; i = e[i].next)
{
//cout<<e[i].from<<" "<<e[i].to<<endl;
if(e[i].to == t && dis[e[i].to] > dis[now] + e[i].len)
{
dis[e[i].to] = dis[now] + e[i].len;
if(!vis[e[i].to])
{
Q.push(e[i].to);
vis[e[i].to] = 1;
}
continue;
}
if(dis[e[i].to] > dis[now] + e[i].len && (dis[e[i].from] + e[i].len) * q + h[e[i].to] <= l[e[i].to])
{
dis[e[i].to] = dis[now] + e[i].len;
if(!vis[e[i].to])
{
Q.push(e[i].to);
vis[e[i].to] = 1;
}
}
}
}
}
int main()
{
memset(dis, 127, sizeof(dis));
memset(head, -1, sizeof(head));
scanf("%lld%lld%lld%lld%lld%lld",&n,&m,&s,&t,&g,&q);
for(int i = 1; i <= n; i++)
scanf("%lld%lld",&h[i],&l[i]);
for(int i = 1; i <= m; i++)
{
ll u, v, w;
scanf("%lld%lld%lld",&u,&v,&w);
add(u, v, w);
add(v, u, w);
}
dis[s] = 0, vis[s] = 1, Q.push(s);
SPFA();
if(dis[t] <= g)
printf("%lld",dis[t]);
else
printf("wtnap wa kotori no oyatsu desu!
");
return 0;
}
以上是关于luogu P3946 ことりのおやつ 题解的主要内容,如果未能解决你的问题,请参考以下文章