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

Choose the best route

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7201    Accepted Submission(s):
2350

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
 
 
题目大意:这个题目的变量比较多,所以比较恶心啦。不过也就是dijkstra的模板问题~
也是给了起点和终点然后找出最短路。这里有一个小技巧就是虚拟起点为"0",把0到任一个真正的起点的距离置为0,到其他点为无穷大,这样可以节省很多时间!!
还有一个要注意的就是这个路是单向的,今天刚过了英语四级,看着英文题目觉得还可以了哈~(*^__^*) 嘻嘻……
 
 #include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int node[],map[][],n,Min;
const int INF=; void dijkstra()
{
int vis[]= {};
int tm=,m;
node[tm]=;
vis[tm]=;
for(int i = ; i <= n; i++)
node[i] = INF;
for (int k=; k<=n; k++)
{
Min=INF;
for (int i=; i<=n; i++)
if (!vis[i])
{
if (node[i]>map[tm][i]+node[tm])
node[i]=map[tm][i]+node[tm];
if (Min>node[i])
{
Min=node[i];
m=i;
}
}
vis[m]=;
tm=m;
}
} int main ()
{
int m,s;
while (~scanf("%d%d%d",&n,&m,&s))
{
memset(map, INF, sizeof(map));
for (int i=; i<=m; i++)
{
int p,q,t;
cin>>p>>q>>t;
if (map[p][q]>t)
map[p][q]=t;
}
int w,cost;
cin>>w;
for (int i=; i<=w; i++)
{
scanf ("%d",&cost);
map[][cost]=;
}
dijkstra();
if(node[s] ==INF)
printf("-1\n");
else
printf("%d\n", node[s]);
}
return ;
}

hdu 2680 Choose the best route (dijkstra算法 最短路问题)的更多相关文章

  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 /************************************************* ...

  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(多起点单终点最短路问题)题解

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

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

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

  7. HDU 2680 最短路 迪杰斯特拉算法 添加超级源点

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

  8. 【hdu 2544最短路】【Dijkstra算法模板题】

    Dijkstra算法 分析 Dijkstra算法适用于边权为正的情况.它可用于计算正权图上的单源最短路( Single-Source Shortest Paths, SSSP) , 即从单个源点出发, ...

  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. 系统管理员需知:25个Linux服务器安全技巧(转)

    来源:51CTO 作者:51CTO       大家都认为 Linux 默认是安全的,我大体是认可的 (这是个有争议的话题).Linux默认确实有内置的安全模型.你需要打开它并且对其进行定制,这样才能 ...

  2. SpringBoot2.0(二) 配置文件多环境

    在SpringBoot中,多环节的配置文件名基于application-{profile}.properties的格式,其中{profile}对应环境标识,比如: application-daily. ...

  3. 【python】 可迭代对象、迭代器、生成器

    可迭代对象 iterable 可直接作用于for循环的对象统称为可迭代对象. 有 list. dict.tuple.set.str等数据类型,还有 generator(包括生成器和带yield的gen ...

  4. openstack之Glance介绍

    什么是Glance glance即image service(镜像服务),是为虚拟机的创建提供镜像服务 为什么要有Glance 我们基于openstack是构建基本的Iaas平台对外提供虚机,而虚机在 ...

  5. 关于 [lambda x: x*i for i in range(4)] 理解

    题目: lst = [lambda x: x*i for i in range(4)] res = [m(2) for m in lst] print res 实际输出:[6, 6, 6, 6] 想要 ...

  6. [洛谷P3975][TJOI2015]弦论

    题目大意:求一个字符串的第$k$大字串,$t$表示长得一样位置不同的字串是否算多个 题解:$SAM$,先求出每个位置可以到达多少个字串($Right$数组),然后在转移图上$DP$,若$t=1$,初始 ...

  7. cf1073d Berland Fair

    ~~~题面~~~ 题解: 可以发现,每走完一圈付的钱和买的数量是有周期性的,即如果没有因为缺钱而买不起某家店的东西,那么这一圈的所以决策将会和上一圈相同,这个应该是很好理解的,想想就好了. 因为钱数固 ...

  8. [HNOI2008]GT考试 矩阵优化DP

    ---题面--- 题解: 一开始看觉得很难,理解了之后其实还挺容易的. 首先我们考虑朴素DP: 令f[i][j]表示长串到了第i项, 与不吉利数字(模式串)匹配到了第j项的方案. 显然ans = f[ ...

  9. Android Canvas 绘图

    画布(Canvas)是图形编程中一个很普通的概念,通常由三个基本的绘图组件组成:       Canvas  提供了绘图方法,可以向底层的位图绘制基本图形.       Paint  也称为" ...

  10. BZOJ4514:[SDOI2016]数字配对——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=4514 有 n 种数字,第 i 种数字是 ai.有 bi 个,权值是 ci. 若两个数字 ai.aj ...