2763: [JLOI2011]飞行路线
spfa 跑分层图
妙~
BZOJ 2763
/**************************************************************
Problem: 2763
User: Dream_Tonight
Language: C++
Result: Accepted
Time:3456 ms
Memory:8844 kb
****************************************************************/
#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <string>
#include <cstdio>
#include <cmath>
#include <stack>
#include <queue>
#include <list>
#include <map>
#include <set>
using namespace std;
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
typedef long long ll;
typedef pair<int, int> pii;
const int maxn = 1e5 + 7;
const int inf = 2139062143;
const int mod = 100000000;
int n, m, k, s, t, z, y, x, cnt = 0;
int head[maxn], dis[maxn][15];
struct node {
int to, value, next;
} edge[maxn];
void ins(int u, int v, int w) {
edge[++cnt] = (node) {v, w, head[u]}, head[u] = cnt;
}
int spfa() {
queue<pii> q;
memset(dis, 127, sizeof(dis));
q.push(make_pair(s, 0));
dis[s][0] = 0;
while (!q.empty()) {
pii u = q.front();
q.pop();
for (int i = head[u.first]; i; i = edge[i].next) {
int to = edge[i].to;
if (dis[to][u.second] > dis[u.first][u.second] + edge[i].value) {
dis[to][u.second] = dis[u.first][u.second] + edge[i].value;
q.push(make_pair(to, u.second));
}
if (u.second + 1 <= k && dis[to][u.second + 1] > dis[u.first][u.second]) {
dis[to][u.second + 1] = dis[u.first][u.second];
q.push(make_pair(to, u.second + 1));
}
}
}
return dis[t][k];
}
int main() {
#ifndef ONLINE_JUDGE
freopen("weather.in", "r", stdin);
#endif
scanf("%d%d%d", &n, &m, &k);
scanf("%d%d", &s, &t);
for (int i = 0; i < m; i++) {
scanf("%d%d%d", &x, &y, &z);
ins(x, y, z);
ins(y, x, z);
}
printf("%d", spfa());
return 0;
}