标签:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <queue>
using namespace std;
const int N = 155;
const int INF = 0x3f3f3f3f;
typedef long long ll;
int A, B, M, L, K;
int G[N][N];
void floyd() {
for (int k = 1; k <= A; k++) {
for (int i = 1; i <= A + B; i++) {
for (int j = 1; j <= A + B; j++) {
if (G[i][j] > G[i][k] + G[k][j]) {
G[i][j] = G[i][k] + G[k][j];
}
}
}
}
}
struct Node{
int u, k, cos;
bool operator <(const Node& a)const {
return cos > a.cos;
}
};
int d[N][15];
int dijkstra() {
priority_queue<Node> Q;
for (int i = 0; i <= A + B; i++) {
for (int j = 0; j <= 12; j++) {
d[i][j] = INF;
}
}
Q.push((Node){A + B, K, 0});
d[A + B][K] = 0;
while (!Q.empty()) {
int u = Q.top().u;
int k = Q.top().k;
if (u == 1) return Q.top().cos;
Q.pop();
for (int i = 1; i <= A + B; i++) {
if (i == u) continue;
if (G[u][i] == INF) continue;
if (d[i][k] > d[u][k] + G[u][i]) {
d[i][k] = d[u][k] + G[u][i];
Q.push((Node){i, k, d[i][k]});
}
if (G[u][i] <= L && d[i][k - 1] > d[u][k] && k != 0) {
d[i][k - 1] = d[u][k];
Q.push((Node){i, k - 1, d[i][k - 1]});
}
}
}
}
void input() {
scanf("%d %d %d %d %d", &A, &B, &M, &L, &K);
for (int i = 1; i <= A + B; i++) {
for (int j = 1; j <= A + B; j++) {
G[i][j] = INF;
}
}
int a, b, c;
for (int i = 0; i < M; i++) {
scanf("%d %d %d", &a, &b, &c);
G[a][b] = G[b][a] = c;
}
floyd();
}
int main() {
int T;
scanf("%d", &T);
while (T--) {
input();
printf("%d\n", dijkstra());
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许也可以转载,不过要注明出处哦。
uva 10269 Adventure of Super Mario (floyd + dijkstra)
标签:
原文地址:http://blog.csdn.net/llx523113241/article/details/47293883