当前位置:网站首页>Leetcode 1928. minimum cost of reaching the destination within the specified time
Leetcode 1928. minimum cost of reaching the destination within the specified time
2022-07-24 20:20:00 【HumbleFool】
LeetCode 1928. The minimum cost of reaching the destination within the specified time 
Two dimensional shortest path
typedef pair<int, int> PII;
#define x first
#define y second
const int N = 1010, M = 2010, INF = 0x3f3f3f3f;
class Solution {
public:
int h[N], w[M], e[M], ne[M], idx = 0;
int dist[N][N]; // Starting point i, Spend time on j Minimum toll of
bool st[N][N] = {
false};
void add(int a, int b, int c)
{
e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx ++;
}
void spfa(int m, vector<int>& pf)
{
memset(dist, 0x3f, sizeof dist);
queue<PII> q;
q.push({
0, 0});
dist[0][0] = pf[0];
st[0][0] = true;
while(!q.empty())
{
auto t = q.front();
q.pop();
st[t.x][t.y] = false;
for(int i = h[t.x]; ~i; i = ne[i])
{
int x = e[i], y = t.y + w[i];
if(y > m) continue;
if(dist[x][y] > dist[t.x][t.y] + pf[x])
{
dist[x][y] = dist[t.x][t.y] + pf[x];
if(!st[x][y])
{
q.push({
x, y});
st[x][y] = true;
}
}
}
}
}
int minCost(int maxTime, vector<vector<int>>& edges, vector<int>& pf) {
memset(h, -1, sizeof h);
for(auto&& e : edges)
{
int a = e[0], b = e[1], c = e[2];
add(a, b, c), add(b, a, c);
}
spfa(maxTime, pf);
int n = pf.size();
int res = INF;
for(int i = 0; i <= maxTime; i ++)
{
res = min(res, dist[n - 1][i]);
}
if(res == INF) return -1;
else return res;
}
};
边栏推荐
- 01 | 开篇词:手把手教你搭建一个博客网站
- Lights of thousands of families in the year of xinchou
- Do you want to enroll in a training class or study by yourself?
- Introduction to fastdfs high availability
- Wechat applet -that.setdata ({}) set complex field data
- [training Day9] rotate [violence] [thinking]
- [training Day6] game [mathematics]
- Valdo2021 - vascular space segmentation in vascular disease detection challenge (I)
- 872. Maximum common divisor
- 【LeetCode】1184. 公交站间的距离
猜你喜欢

Synthesis route of ALA PNA alanine modified PNA peptide nucleic acid | AC ala PNA

Analysis and Simulation of strlen function
![[training Day6] dream [priority queue] [greed]](/img/1b/309b53618b8a116862971799ce4e78.jpg)
[training Day6] dream [priority queue] [greed]

Istio II traffic hijacking process

What should Ali pay attention to during the interview? Personal account of Alibaba interns who passed five rounds of interviews

Substr and substring function usage in SQL

Google's display of Chrome browser icons was abandoned, and it was intended to be a small rocket
![[training Day6] triangle [mathematics] [violence]](/img/57/d603886c202de7d46ea03487b633f5.png)
[training Day6] triangle [mathematics] [violence]

【德味】安全:如何为行人提供更多保护
![[training Day6] game [mathematics]](/img/b2/09c752d789eead9a6b60f4b4b1d5d4.png)
[training Day6] game [mathematics]
随机推荐
Review the code implementation of memcpy function
From code farmer to great musician, you only need these music processing tools
《自尊的6大支柱》自尊来源于自身的感受
Monotone stack and monotone queue (linear complexity optimization)
How to integrate Kata in kubernetes cluster
2019 Hangdian multi school first 6581 vacation [thinking]
Read the registry through the ATL library clegkey (simple and convenient)
1. Mx6u-alpha development board (buzzer experiment)
Risk control system, implemented by flink+clickhouse!
Lunch break train & problem thinking: thinking about the problem of converting the string formed by hour: minute: second to second
(forward) usage of PostMessage
Istio二之流量劫持过程
[training Day8] series [matrix multiplication]
[training Day8] tent [mathematics] [DP]
Analysis of xmldecoder parsing process
Stop using UUID indiscriminately. Have you tested the performance gap between self incrementing ID and UUID?
Alibaba cloud technology expert Yang Zeqiang: building observable capabilities on elastic computing cloud
Richview table table alignment
[training Day10] linear [mathematics] [thinking]
Machine learning job interview summary: five key points that resume should pay attention to