hdu2680 Choose the best route 最短路,超级起点(多源Dijkstra算法,)

Description
One day , Kiki wants to visit one of her friends. As she is liable to carsickness , she wants to arrive at her friend’s home as soon as possible . Now give you a map of the city’s traffic route, and the stations which are near Kiki’s home so that she can take. You may suppose Kiki can change the bus at any station. Please find out the least time Kiki needs to spend. To make it easy, if the city have n bus stations ,the stations will been expressed as an integer 1,2,3…n.

Input
There are several test cases.
Each case begins with three integers n, m and s,(n<1000,m<20000,1=


#include<iostream>
#include<stdio.h>
#include<queue>
#include<stack>
#include<algorithm>
#include<string.h>
#include<string>
#include<math.h>
using namespace std;
int maze[1005][1005];
int dis[1005];
int vis[1005];
#define INF 0x7f7f7f
int main()
{
    int n,m,s;
    while(scanf("%d%d%d",&n,&m,&s)!=EOF)
    {
        memset(maze,INF,sizeof(maze));
        memset(vis,false,sizeof(vis));
        int st,en,len;
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d%d",&st,&en,&len);
            if(maze[st][en]>len)
            {
                maze[st][en]=len;
            }
        }
        int w;
        scanf("%d",&w);
        for(int i=1;i<=w;i++)
        {

            scanf("%d",&st);
            maze[0][st]=0;
        }
        for(int i=1;i<=n;i++)
            dis[i]=INF;
        dis[0]=0;
        for(int i=0;i<=n;i++)
        {
            int x,m=INF;
            for(int y=0;y<=n;y++)
            {
                if(!vis[y]&&m>=dis[y])
                {
                    m=dis[y];
                    x=y;
                }
            }
            vis[x]=true;
            for(int y=0;y<=n;y++)
            {
                if(dis[y]>dis[x]+maze[x][y])
                {
                    dis[y]=dis[x]+maze[x][y];
                }
            }
        }
        if(dis[s]==INF)
            printf("-1\n");
        else
            printf("%d\n",dis[s]);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值