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 (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7201 Accepted Submission(s):
2350
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.
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.
least time Kiki needs to spend ,if it’s impossible to find such a route ,just
output “-1”.
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
-1

- #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算法 最短路问题)的更多相关文章
- hdu 2680 Choose the best route
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2680 Choose the best route Description One day , Kiki ...
- hdu 2680 Choose the best route (dijkstra算法)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2680 /************************************************* ...
- hdu 2680 Choose the best route 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2680 题目意思:实质就是给定一个多源点到单一终点的最短路. 卑鄙题---有向图.初始化map时 千万不 ...
- HDU 2680 Choose the best route(SPFA)
Problem DescriptionOne day , Kiki wants to visit one of her friends. As she is liable to carsickness ...
- HDU 2680 Choose the best route(多起点单终点最短路问题)题解
题意:小A要乘车到s车站,他有w个起始车站可选,问最短时间. 思路:用Floyd超时,Dijkstra遍历,但是也超时.仔细看看你会发现这道题目好像是多源点单终点问题,终点已经确定,那么我们可以直接转 ...
- HDU 2680 Choose the best route 最短路问题
题目描述:Kiki想去他的一个朋友家,他的朋友家包括所有的公交站点一共有n 个,一共有m条线路,线路都是单向的,然后Kiki可以在他附近的几个公交站乘车,求最短的路径长度是多少. 解题报告:这道题的特 ...
- HDU 2680 最短路 迪杰斯特拉算法 添加超级源点
Choose the best route Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- 【hdu 2544最短路】【Dijkstra算法模板题】
Dijkstra算法 分析 Dijkstra算法适用于边权为正的情况.它可用于计算正权图上的单源最短路( Single-Source Shortest Paths, SSSP) , 即从单个源点出发, ...
- HDU 2068 Choose the best route
http://acm.hdu.edu.cn/showproblem.php?pid=2680 Problem Description One day , Kiki wants to visit one ...
随机推荐
- cmake & make
大家都知道,写程序大体步骤为: 1.用编辑器编写源代码,如.c文件. 2.用编译器编译代码生成目标文件,如.o. 3.用链接器连接目标代码生成可执行文件,如.exe. 但如果源文件太多,一个一个编译时 ...
- [剑指Offer] 50.数组中重复的数字
题目描述 在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任意一个重复的数字. 例如,如果输入长度为 ...
- Spring编程式事务管理及声明式事务管理
本文将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. Spring 事务属性分析 事务管理 ...
- HDU 4638 group(离线+树状数组)
此题实质上是询问每段区间[l,r]内的数字能组成多少段连续的数字. 不大好用线段树合并区间来写. 考虑离线,类似于HH的项链的话,对询问按右端点排序,对于右端点的每一次右移,则对于询问,实际上是求的后 ...
- 【题解】洛谷P3709大爷的字符串题
最近想要练习一下莫队(实在是掌握的太不熟练了啊.)这题一开始看到有点懵(题面杀),后来发现是要求众数的个数.乍一看好像很难的样子. 但仔细分析一下:首先往序列当中加入一个数,这个是很简单的,只需要维护 ...
- 斜率优化第一题! HDU3507 | 单调队列优化DP
放一手原题 题解: 第一次写(抄)斜率优化,心里还是有点小激动的.讲一下怎么实现的! 首先我们可以考虑一个朴素的dp:DP[i]表示前i个数字的最少花费,显然我们有一个转移方程 DP[i]=min{D ...
- 函数strcpy的实现
strcpy函数的百科中给出了各种情况的详细说明,这里,仅给出一些注意事项: 1.strcpy的函数原型是: /* dest(destination)为目标字符串,src(source)为原字符串*/ ...
- [Leetcode] valid sudoku 有效数独
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...
- Shell编程语法
创建shell程序的步骤: 第一步:创建一个脚本文件.sh. 第二步:授予权限使它可以执行chmod u+x .sh 第三步:执行 ./example 或者 sh example 脚本调试: ...
- python----测试04.18
# py4测试题 # 1.8 << 2 # 等于? 8转化成二进制:1000 向左移动2位: 0010 0000 转化成十进制:32 # 2.通过内置函数计算5除以2的余数 print(d ...