欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 维修 > 【魔法阵——广义Dijkstra,DP】

【魔法阵——广义Dijkstra,DP】

2025/2/12 6:56:48 来源:https://blog.csdn.net/m0_73669127/article/details/145581095  浏览:    关键词:【魔法阵——广义Dijkstra,DP】

题目

代码

#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
const int inf = 0x3f3f3f3f;
int g[N][N], d[N][N];
bool st[N][N];
int n, k, m;
struct Node
{int v, c, i;bool operator < (const Node &y) const{return v > y.v;}
};
priority_queue<Node> pq;
void dijkstra()
{memset(d, 0x3f, sizeof d);pq.push({0, 0, 0});d[0][0] = 0;while(pq.size()){auto u = pq.top(); pq.pop();int v = u.v, c = u.c, i = u.i;if(st[c][i]) continue;st[c][i] = 1;for(int j = 0; j < n; j++){if(g[i][j] == inf) continue;if(d[j][0] > d[i][0] + g[i][j]){d[j][0] = d[i][0] + g[i][j];pq.push({d[j][0], 0, j});}if(c == k){if(d[j][k] > d[i][k] + g[i][j]){d[j][k] = d[i][k] + g[i][j];pq.push({d[j][k], k, j});}}if(c+1 <= k && d[j][c+1] > d[i][c]){d[j][c+1] = d[i][c];pq.push({d[j][c+1], c+1, j});}}}}
int main()
{ios::sync_with_stdio(0); cin.tie(0);memset(g, 0x3f, sizeof g);cin >> n >> k >> m;for(int i = 1; i <= m; i++){int a, b, c;cin >> a >> b >> c;g[a][b] = c;g[b][a] = c;}dijkstra();cout << min(d[n-1][0], d[n-1][k]);
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com