HDOJ-2680-Choose the best route 解题报告
来源:互联网 发布:淘宝卖家设置产品折扣 编辑:程序博客网 时间:2024/06/05 07:03
这是一道不错的最短路题目,半水不水的题,还是需要动脑思考一下的。题意:琪琪想要去拜访她的朋友,但是这货容易晕车,所以要找一个花费时间最少的路线。现在给你路线图,让你找出从她家附近的起点站(可以有多个)到朋友家附近的终点站(只有一个)花费时间最少的路线。各个站点的编号从1到n。
我的解题思路:首先是输入站点数量,路线数量(在两个站点之间可以有多条路线)和终点;然后是各个路线的信息(起点终点以及耗时);最后再输入起点的个数和起点。由于站点数量最多可以达到1000,因此Floyd算法是不行的了(事实上我一开始就用这个写,TLE了)。我们换个思路,用Dijkstra啊!有几个起点就用几次Dijkstra。恩,这么做是没什么错,只要起点的数量不是很接近站点的数量,那么多多少少的确有所优化(没用此方法提交过)。但是我们可以有一种更加精明的解法啊,那就是把终点看做起点,起点看做终点,这样的话用一次Dijkstra就可以了,省时省力有省心啊有木有。可见,多思考一下别的解法还是有用的。
注意:本题为单向图。那么,起点看做终点,终点看做起点时要进行反向构图。
接下来是我的解题代码:Dijkstra解法
#include <stdio.h>#define N 1001#define INF 999999int map[N][N];int dis[N]; //在本题中代表从终点s到编号为下标的点的时间int flag[N]; //标志变量int n, m, s;void Init(); //初始化void Read(); //图信息的输入void Dijkstra();void DataProcess(); //起点的输入以及答案的计算int main(){ while (~scanf("%d %d %d", &n, &m, &s)) { Init(); Read(); Dijkstra(); DataProcess(); } return 0;}void Init() //初始化{ int i, j; for (i=1; i<=n; ++i) { for (j=1; j<=n; ++j) { map[i][j] = i == j ? 0 : INF; } dis[i] = INF; flag[i] = 0; } return;}void Read() //图信息的输入{ int i; int a, b, c; for (i=0; i<m; ++i) { scanf("%d %d %d", &a, &b, &c); if (map[b][a] > c) //处理重边问题 { map[b][a] = c; //反向构图 } } return;}void Dijkstra(){ int i, j, k; int min; dis[s] = 0; //设终点为起点 for (i=1; i<=n; ++i) { min = INF; for (j=1; j<=n; ++j) { if (flag[j] == 0 && dis[j] < min) { min = dis[k = j]; } } flag[k] = 1; for (j=1; j<=n; ++j) { if (flag[j] == 0 && dis[j] > dis[k] + map[k][j]) { dis[j] = dis[k] + map[k][j]; } } } return;}void DataProcess() //起点的输入以及答案的计算{ int i, w, x; int ans = INF; scanf("%d", &w); for (i=0; i<w; ++i) { scanf("%d", &x); if (ans > dis[x]) { ans = dis[x]; } } printf("%d\n", ans == INF ? -1 : ans); return;}
0 0
- HDOJ-2680-Choose the best route 解题报告
- HDU 2680 Choose the best route 解题报告
- hdu 2680 Choose the best route 最短路 解题报告
- ACM HDOJ 2680 (Choose the best route)
- hdoj 2680 Choose the best route
- HDOJ-2680Choose the best route(Dijkstra)
- HDOJ--2680--Choose the best route
- hdoj 2680 Choose the best route 【dijkstra】
- hdoj 2680 Choose the best route
- hdoj 2680 Choose the best route
- hdoj 2680 Choose the best route
- HDOJ Choose the best route
- HDU2680 Choose the best route 解题报告--Dijkstra
- HDOJ 2680 Choose the best route(最短路,dijkstra)
- HDOJ 题目2680 Choose the best route(最短路)
- HDOJ 题目2680Choose the best route(最短路)
- HDOJ 题目2680 Choose the best route(spfa)
- HDOJ 2680 Choose the best route(最短路--dijkstra)
- MFC 键盘响应
- Swift简介
- 如何开启mysql计划事件
- 【Cocos2d-x 粒子系统】火球随手指飞起来
- Nginx的upstream支持4种负载均衡方式的配置
- HDOJ-2680-Choose the best route 解题报告
- (黑马程序员)学习笔记,其他IO流对象
- 11.黑马程序员-内存管理
- Win7下PHP+APACHE+MySQL的配置
- highstock K线图 深入研究
- 杨幂生宝宝 小糯米
- C++多态的实现原理(转载)
- .Net 中的反射
- 汽车中的AT、MT和AMT