hdu 4396 More lumber is required

来源:互联网 发布:干程序员帅哥变 编辑:程序博客网 时间:2024/04/28 13:59
/**   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 100005struct edge{    int u, v, w, next ;}e[E*2] ;int head[N], cnt ;int dis[N][55] ;bool vis[N][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 ;}

原创粉丝点击