hdu 4396 More lumber is required
2013-04-29 16:55
351 查看
/* * Author: johnsondu * Time: 2013-4-29 * Problem: hdu 4396 More lumber is required * Url: http://acm.hdu.edu.cn/showproblem.php?pid=4396 * stratege: 二维spfa, dis[][]前者为城市,后者记录走过的路径数 */ #include <iostream> #include <cstdio> #include <cmath> #include <algorithm> #include <cstring> #include <queue> using namespace std ; #define inf 0xfffffff #define N 5005 #define E 100005 struct edge { int u, v, w, next ; }e[E*2] ; int head , cnt ; int dis [55] ; bool vis [55] ; int n, m, S, T, K ; int ans ; void init () { memset (head, -1, sizeof (head)) ; cnt = 0 ; } void addedge (int u, int v, int w) { e[cnt].u = u ; e[cnt].v = v ; e[cnt].w = w ; e[cnt].next = head[u] ; head[u] = cnt ++ ; } struct Node { int a, b ; }cur, next; int spfa () { int i ; for (int i = 0; i <= n; i ++) for (int j = 0; j <= K; j ++) dis[i][j] = inf ; dis[S][0] = 0 ; vis[S][0] = true ; queue <Node > Q ; cur.a = S ; cur.b = 0 ; Q.push (cur) ; while (!Q.empty()) { cur = Q.front() ; Q.pop () ; vis[cur.a][cur.b] = false ; if (cur.a == T && cur.b == K) { ans = min (ans, dis[T][K]) ; } for (i = head[cur.a]; i != -1; i = e[i].next) { next.a = e[i].v ; next.b = cur.b + 1 ; if (next.b >= K) { next.b = K ; } if (dis[next.a][next.b] > dis[cur.a][cur.b] + e[i].w) { dis[next.a][next.b] = dis[cur.a][cur.b] + e[i].w ; if (!vis[next.a][next.b]) { vis[next.a][next.b] = true ; Q.push (next) ; } } } } return 1 ; } int main () { //freopen ("data.txt", "r", stdin) ; int a, b, c ; while (scanf ("%d%d", &n, &m) != EOF) { init () ; while (m --) { scanf ("%d%d%d", &a, &b, &c) ; addedge (a, b, c) ; addedge (b, a, c) ; } scanf ("%d%d%d", &S, &T, &K) ; ans = inf ; K = (K+9)/10 ; spfa () ; printf ("%d\n", ans == inf ? -1 : ans) ; } return 0 ; }
相关文章推荐
- hdu 4396 More lumber is required(最短路,SPFA,4级)
- HDU 4396 More lumber is required [至少经过K边最短路]
- HDU 4396 More lumber is required(最短路)
- hdu - 4396 More lumber is required(二维最短路)
- hdu 4396 More lumber is required(最短路)
- hdu 4396 More lumber is required
- HDU - 4396 More lumber is required (BFS 最短路)
- hdu 4396 More lumber is required(最短路,SPFA,4级)
- hdu 4396 More lumber is required (二维SPFA)
- HDOJ 题目4396 More lumber is required(至少经过k条边的最短路)
- More lumber is required
- HDU-1856 More is better
- hdu 1856 More is better(并查集)
- Hdu 1856(离散化+并查集)More is better
- hdu 1856 More is better (并查集)
- hdu 1856 More is better
- HDU-1856-More is better
- hdu 1856 More is better 并查集模板题
- HDU 1856 More is better
- hdu 1856 More is better