二维最短路
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]; //起点到i,花费时间为j的最小通行费用
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;
}
};