题目链接:

Choose the best route

Time Limit: 2000/1000 MS (Java/Others)   

 Memory Limit: 32768/32768 K (Java/Others)

Problem 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=<s<=n) n stands for the number of bus stations in this city and m stands for the number of directed ways between bus stations .(Maybe there are several ways between two bus stations .) s stands for the bus station that near Kiki’s friend’s home.
Then follow m lines ,each line contains three integers p , q , t (0<t<=1000). means from station p to station q there is a way and it will costs t minutes .
Then a line with an integer w(0<w<n), means the number of stations Kiki can take at the beginning. Then follows w integers stands for these stations.
 
Output
 
The output contains one line for each data set : the least time Kiki needs to spend ,if it’s impossible to find such a route ,just output “-1”.
 
Sample Input
 
5 8 5
1 2 2
1 5 3
1 3 4
2 4 7
2 5 6
2 3 5
3 5 1
4 5 1
2
2 3
4 3 4
1 2 3
1 3 4
2 3 2
1
1
 
Sample Output
 
1
-1
 
题意
 
给一个有向图,问从多个起点的任意一个出发到达终点的最短时间;
 
思路
 
把0当做所有起点的起点,那么这些起点到0的距离都是0,这样可以用dijkstra算法跑一波得到答案,如果用Floyd算所有节点对应该会tle;
 
AC代码
 
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL mod=1e9+;
const int N=1e5+;
const int inf=0x3f3f3f3f;
int n,m,s;
int p[][],flag[],dis[];
void dijkstra()
{
memset(flag,,sizeof(flag));
for(int i=;i<=n;i++)
{
dis[i]=p[][i];
}
flag[]=;
int temp;
for(int i = ;i <= n;i++)
{
int mmin=inf;
for(int j = ;j<=n;j++)
{
if(!flag[j]&&dis[j]<mmin)
{
mmin=dis[j];
temp=j;
}
}
if(mmin == inf)break;
flag[temp]=;
for(int j=;j<=n;j++)
{
if(dis[j]>dis[temp]+p[temp][j])
dis[j]=dis[temp]+p[temp][j];
}
}
}
int main()
{
while(scanf("%d%d%d",&n,&m,&s)!=EOF)
{
for(int i=;i<=n;i++)
{
dis[i]=inf;
for(int j=;j<=n;j++)
{
if(i == j)p[i][j]=;
else p[i][j]=inf;
}
}
int u,v,w;
for(int i = ;i < m;i ++)
{
scanf("%d%d%d",&u,&v,&w);
p[u][v]=min(p[u][v],w);
}
int num,x;
scanf("%d",&num);
for(int i=;i<num;i++)
{
scanf("%d",&x);
p[][x]=;
}
dijkstra();
if(dis[s] == inf)printf("-1\n");
else printf("%d\n",dis[s]);
} return ;
}

hdu-2680 Choose the best route(最短路)的更多相关文章

  1. hdu 2680 Choose the best route

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2680 Choose the best route Description One day , Kiki ...

  2. hdu 2680 Choose the best route (dijkstra算法 最短路问题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2680 Choose the best route Time Limit: 2000/1000 MS ( ...

  3. hdu 2680 Choose the best route 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2680 题目意思:实质就是给定一个多源点到单一终点的最短路. 卑鄙题---有向图.初始化map时 千万不 ...

  4. HDU 2680 Choose the best route(SPFA)

    Problem DescriptionOne day , Kiki wants to visit one of her friends. As she is liable to carsickness ...

  5. hdu 2680 Choose the best route (dijkstra算法)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2680 /************************************************* ...

  6. HDU 2680 Choose the best route 最短路问题

    题目描述:Kiki想去他的一个朋友家,他的朋友家包括所有的公交站点一共有n 个,一共有m条线路,线路都是单向的,然后Kiki可以在他附近的几个公交站乘车,求最短的路径长度是多少. 解题报告:这道题的特 ...

  7. HDU 2680 Choose the best route(多起点单终点最短路问题)题解

    题意:小A要乘车到s车站,他有w个起始车站可选,问最短时间. 思路:用Floyd超时,Dijkstra遍历,但是也超时.仔细看看你会发现这道题目好像是多源点单终点问题,终点已经确定,那么我们可以直接转 ...

  8. HDU2680 Choose the best route 最短路 分类: ACM 2015-03-18 23:30 37人阅读 评论(0) 收藏

    Choose the best route Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  9. HDU 2068 Choose the best route

    http://acm.hdu.edu.cn/showproblem.php?pid=2680 Problem Description One day , Kiki wants to visit one ...

随机推荐

  1. Xcode6 pch文件

    XCode6里, 默认是没有pch文件的,如果我们想使用pch文件,需要手动添加,添加步骤如下 1.在XCode6中是么有pch文件的,如下图     2.创建pch文件     3.配置pch文件 ...

  2. Scrapy学习-8-ItemLoader

    ItemLoader使用 作用 方便管理维护重用xpath或css规则   实例 itemloader+图片处理 # items.py import scrapy from scrapy.loader ...

  3. Mongodb报错:ERROR: child process failed, exited with error number 1

    Mongodb在启动时报错: 2018-10-16T11:18:54.533+0800 I CONTROL [main] Automatically disabling TLS 1.0, to for ...

  4. hdu4848 求到达每个点总时间最短(sum[d[i]])。

    开始的时候是暴力dfs+剪枝,怎么也不行.后来参考他人思想: 先求出每个点之间的最短路(这样预处理之后的搜索就可以判重返回了),截肢还是关键:1最优性剪枝(尽量最优:目前的状态+预计还有的最小时间&g ...

  5. AC日记——友好城市 洛谷 P2782

    题目背景 无 题目描述 有一条横贯东西的大河,河有笔直的南北两岸,岸上各有位置各不相同的N个城市.北岸的每个城市有且仅有一个友好城市在南岸,而且不同城市的友好城市不相同.没对友好城市都向政府申请在河上 ...

  6. Peter Norvig:十年学会编程

    为啥都想速成? 随便逛一下书店,你会看到<7天自学Java>等诸如此类的N天甚至N小时学习Visual Basic.Windows.Internet的书.我用亚马逊网站的搜索功能,出版年份 ...

  7. let与const命令

    (需要注意的地方) 1.ES6 新增了let命令,用来声明变量.它的用法类似于var,但是所声明的变量,只在let命令所在的代码块内有效. 2.for循环还有一个特别之处,就是设置循环变量的那部分是一 ...

  8. ffmpeg 时间戳

    转http://blog.csdn.net/yfh1985sdq/article/details/5721953 AVpacket里的时间戳pts和dts.单位好像是us. 问 : 时间戳pts和dt ...

  9. BZOJ1016最小生成树计数 最小生成树 + 排列组合

    @[最小生成樹, 排列組合] Discription 现在给出了一个简单无向加权图.你不满足于求出这个图的最小生成树,而希望知道这个图中有多少个不同的 最小生成树.(如果两颗最小生成树中至少有一条边不 ...

  10. 如何细粒度地控制你的MyBatis二级缓存(mybatis-enhanced-cache插件实现)

    前几天网友chanfish 给我抛出了一个问题,笼统地讲就是如何能细粒度地控制MyBatis的二级缓存问题,酝酿了几天,觉得可以写个插件来实现这个这一功能.本文就是从问题入手,一步步分析现存的MyBa ...