当前位置:网站首页>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;
}
};
边栏推荐
- 2019 Hangzhou Electric Multi School Game 9 6684 Rikka with game [game question]
- Login Huawei device in SSH mode
- Qt| control qscrollbar display position
- [msp430g2553] graphical development notes (2) system clock and low power consumption mode
- 存储类别
- Monotone stack and monotone queue (linear complexity optimization)
- [shader realizes the flicker effect of three primary colors of television signal _shader effect Chapter 5]
- [training Day8] interesting number [digital DP]
- Huawei set up login with account and password
- Alibaba Sentinel 基操
猜你喜欢

Xiaomi 12s ultra products are so powerful, but foreigners can't buy Lei Jun: first concentrate on the Chinese market

Thinking of @requestbody caused by hi and hello requests

What should Ali pay attention to during the interview? Personal account of Alibaba interns who passed five rounds of interviews
![[training Day6] dream [priority queue] [greed]](/img/1b/309b53618b8a116862971799ce4e78.jpg)
[training Day6] dream [priority queue] [greed]

Student achievement management system based on PHP
![Microservice architecture | service monitoring and isolation - [sentinel] TBC](/img/28/8ca90e9dbd492688e50446f55959ff.png)
Microservice architecture | service monitoring and isolation - [sentinel] TBC

Getting started with COM programming 1- creating projects and writing interfaces

Introduction to fastdfs high availability

Lights of thousands of families in the year of xinchou

(posted) differences and connections between beanfactory and factorybean
随机推荐
[shader realizes the flicker effect of three primary colors of television signal _shader effect Chapter 5]
Read the registry through the ATL library clegkey (simple and convenient)
How does starknet change the L2 landscape?
870. Approximate number
[msp430g2553] graphical development notes (2) system clock and low power consumption mode
[mathematical modeling / mathematical programming model]
Synthesis of peptide nucleic acid PNA labeled with heptachydrin dye cy7 cy7-pna
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
Understand the domestic open source Magnolia license series agreement in simple terms
How to view the execution plan of a stored procedure in Youxuan database
Easy to use office network optimization tool onedns
[leetcode] 1184. Distance between bus stops
《自尊的6大支柱》自尊来源于自身的感受
01 | opening words: teach you to build a blog website hand in hand
Usage and introduction of MySQL binlog
In the era of new knowledge economy, who is producing knowledge?
Solve the problem that gd32f207 serial port can receive but send 00
Framework API online viewing source code
Flink Window&Time 原理
Unit DLU of resource editor