您的位置:首页 > 产品设计 > UI/UE

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 ;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: