当前位置:网站首页>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;
}
};
边栏推荐
- Lunch break train & problem thinking: on multidimensional array statistics of the number of elements
- Qt| control qscrollbar display position
- [shader realizes the flicker effect of three primary colors of television signal _shader effect Chapter 5]
- [training Day8] interesting number [digital DP]
- Istio一之Envoy工作原理
- Lunch break train & problem thinking: thinking about the problem of converting the string formed by hour: minute: second to second
- Framework API online viewing source code
- Mysql8 doesn't seem to support MyISAM partition tables. Does polardb-x support MyISAM partition tables?
- TCP sliding window, singleton mode (lazy and hungry) double checked locking / double checked locking (DCL)
- [mathematical modeling / mathematical programming model]
猜你喜欢
![[training Day6] dream [priority queue] [greed]](/img/1b/309b53618b8a116862971799ce4e78.jpg)
[training Day6] dream [priority queue] [greed]

Redis basic knowledge, application scenarios, cluster installation

(posted) differences and connections between beanfactory and factorybean

Valdo2021 - vascular space segmentation in vascular disease detection challenge (2)

Azide labeled PNA peptide nucleic acid | methylene blue labeled PNA peptide nucleic acid | tyrosine modified PNA | Tyr PNA Qiyue Bio

Read the registry through the ATL library clegkey (simple and convenient)

TCP sliding window, singleton mode (lazy and hungry) double checked locking / double checked locking (DCL)

Usage and introduction of MySQL binlog

Make Huawei router into FTP server (realize upload and download function)

"Hualiu is the top stream"? Share your idea of yyds
随机推荐
[training Day6] game [mathematics]
2019 Hangzhou Electric Multi School Game 7 6651 final exam [Law + thinking]
Each blogger needs to ask himself seven basic questions
Analysis and Simulation of strlen function
Usage and introduction of MySQL binlog
Analysis of xmldecoder parsing process
MySQL advanced 2
Azide labeled PNA peptide nucleic acid | methylene blue labeled PNA peptide nucleic acid | tyrosine modified PNA | Tyr PNA Qiyue Bio
870. Approximate number
Is the incremental update of SQL Server database identified according to the where clause? Can't do stream update? Each table should
C form application treeview control use
Modbus communication protocol specification (Chinese) sharing
Hide the middle digit of ID number
[FreeRTOS] 10 event flag group
01 | 开篇词:手把手教你搭建一个博客网站
英文翻译中文常见脏话
【LeetCode】1184. 公交站间的距离
871. Sum of divisors
1. Mx6u-alpha development board (buzzer experiment)
Lights of thousands of families in the year of xinchou