当前位置:网站首页>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;
}
};
边栏推荐
- Unity2d~ game practice of decrypting Zhou mu (completed in three days)
- Alibaba cloud technology expert Yang Zeqiang: building observable capabilities on elastic computing cloud
- Analysis and Simulation of strlen function
- Todolist case
- Each blogger needs to ask himself seven basic questions
- [shader realizes the flicker effect of three primary colors of television signal _shader effect Chapter 5]
- 【LeetCode】1184. 公交站间的距离
- Conversion between VC string and timestamp
- Getting started with COM programming 1- creating projects and writing interfaces
- Please ask a question. Follow the quick start method. After creating the table, the Flink SQL queries and displays the table structure, but there is an error when it exceeds the limit. What should we
猜你喜欢

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

Near infrared dye cy7.5 labeling PNA polypeptide experimental steps cy7.5-pna|188re labeling anti gene peptide nucleic acid (agpna)

Software testing interview tips | if you don't receive the offer, I'll wash my hair upside down

(posted) differences and connections between beanfactory and factorybean
![[shader realizes the flicker effect of three primary colors of television signal _shader effect Chapter 5]](/img/ca/fa87bc199f3aefebddf6eb58784dc7.png)
[shader realizes the flicker effect of three primary colors of television signal _shader effect Chapter 5]

Home Assistant中接入博联WiFi智能遥控

Are network security and data security indistinguishable? Why is data security important?

Thymeleaf application notes
![[training Day8] [luogu_p6335] staza [tarjan]](/img/cf/e2027549c56b8597e7cd579d737392.png)
[training Day8] [luogu_p6335] staza [tarjan]
![[training Day8] interesting number [digital DP]](/img/39/caad2ccff916d5ab0f8c3d93f3901d.png)
[training Day8] interesting number [digital DP]
随机推荐
147 set whether to cache by using the routing meta information - use of include and exclude - use of activated and deactivated
English grammar_ Demonstrative pronoun this / these / that / those
Read the registry through the ATL library clegkey (simple and convenient)
Xiaomi 12s ultra products are so powerful, but foreigners can't buy Lei Jun: first concentrate on the Chinese market
In the era of new knowledge economy, who is producing knowledge?
YouTube "label products" pilot project launched
Understand the domestic open source Magnolia license series agreement in simple terms
Istio二之流量劫持过程
[training Day9] rotate [violence] [thinking]
Huawei set up login with account and password
Alibaba Sentinel 基操
Lunch break train & problem thinking: on multidimensional array statistics of the number of elements
Unity3d eventsystem (event)
[training Day9] light tank [dynamic planning]
[FreeRTOS] 10 event flag group
872. Maximum common divisor
[German flavor] safety: how to provide more protection for pedestrians
[training Day6] game [mathematics]
Valdo2021 - vascular space segmentation in vascular disease detection challenge (I)
Sql164 next day retention rate of new users per day in November 2021