hdu 2680 Choose the best route (dijkstra算法)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2680
/************************************************************************/
/*
hdu Arbitrage
dijkstra算法
题目大意:dijkstra算法,求点与点之间最短距离。因为此题的起始点不定,所以可用
反向图来求得,终点确定,从终点出发,dijkstra算法,求出其他点到终点最小距离。
本题数据量较大,用floyd算法超时。
*/
/************************************************************************/ #include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include <algorithm> using namespace std; #define MIN(a,b) a<b?a:b
#define MAX 0xfffffff const int N = ;
int maps[N][N];
int dj[N],vis[N];
int m,n,s,w,num,min_num; void build_map()
{
int p,q,t;
for (int i = ; i <= n; i++)
for (int j = i; j <= n; j++)
maps[i][j] = maps[j][i] = ((i==j)?:MAX); for (int i = ; i <= m; i++)
{
scanf("%d%d%d",&p,&q,&t);
if(maps[q][p] > t) maps[q][p] = t;//这里注意构建反向图
} for (int i = ; i <= n; i++)
dj[i] = MAX;
} /*
//此floyd算法超时
void floyud()
{
for (int k = 1; k <= n; k++)
{
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
maps[i][j] = MIN(maps[i][j],maps[i][k] + maps[k][j]);
}
}
*/
void DJ()
{
int cur = s;
int next,min;
dj[cur] = ;
while()
{
min = MAX;
vis[cur] = ;
for (int i = ; i <= n; i++)
{
if (vis[i] == )continue;
if (dj[i] > dj[cur] + maps[cur][i])
dj[i] = dj[cur] + maps[cur][i];
if (dj[i] < min)
{
min = dj[i];
next = i;
}
}
if ( min == MAX)break;
cur = next;
}
} int main()
{
while(scanf("%d%d%d",&n,&m,&s)!= EOF )
{
build_map();
memset(vis,,sizeof(vis));
DJ();
min_num = MAX;
scanf("%d",&w);
while(w--)
{
scanf("%d",&num);
if ( dj[num] < min_num)
min_num = dj[num];
}
if (min_num == MAX)
printf("-1\n");
else printf("%d\n",min_num);
}
return ;
}
hdu 2680 Choose the best route (dijkstra算法)的更多相关文章
- 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 ( ...
- 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 解题报告
题目链接: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 ...
随机推荐
- SpringBoot集成MyBatis的分页插件PageHelper
俗话说:好
- 解决FatalThrowableError in EloquentUserProvider.php line 126: Class '\App\User'问题
解决方法:http://stackoverflow.com/questions/28516454/laravel-5-user-model-not-found 总结:是因为我把app下的User移动到 ...
- BAT面试的准备—iOS篇
本文主要用于记录在准备BAT面试中关于iOS遇到的问题和做一些相关面试题的笔记 iOS网络层设计 1.网络层和业务层的对接设计 使用哪种交互模式来和业务层对接 : 使用Delegate为主,目的是为了 ...
- iOS 开发中的 Tips(一)
背景 学习6个小Tips 那就跟我一起学习小知识点吧.目录如下: 修改Mac终端(Terminal)里不同类型文件的显示颜色 修改Mac终端(Terminal)的提示文字 Mac终端显示/隐藏文件命令 ...
- linux达人养成计划学习笔记(五)—— 关机和重启命令
一.shutdown 1.格式: shutdown [选项] 时间(now) 选项: -c: 取消前一个关机命令 -h: 关机 -r: 重启 2.程序放入后台执行: shutdown -r 时间 &a ...
- HDU 3999 The order of a Tree (先序遍历)
The order of a Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- jQuery UI API - 可拖拽小部件(Draggable Widget)(转)
所属类别 交互(Interactions) 用法 描述:允许使用鼠标移动元素. 版本新增:1.0 依赖: UI 核心(UI Core) 部件库(Widget Factory) 鼠标交互(Mouse I ...
- opencv 中出现错误 -215:Assertion failed
cv2.error: OpenCV(4.0.1) D:\Build\OpenCV\opencv-4.0.1\modules\imgproc\src\color.cpp:181: error: (-21 ...
- 关于less在DW中高亮显示问题
首先, 找到DW 安装目录. Adobe Dreamweaver CS5.5\configuration\DocumentTypes 中的,MMDocumentTypes.xml 这个文件,然后用记事 ...
- 【转】Java 有值类型吗?
Java 有值类型吗? 有人看了我之前的文章『Swift 语言的设计错误』,问我:“你说 Java 只有引用类型(reference type),但是根据 Java 的官方文档,Java 也有值类型( ...