HDU2680-Choose the best route(经典最短路问题dijkstra算法)
来源:互联网 发布:centos mysql配置文件 编辑:程序博客网 时间:2024/06/05 06:28
Choose the best route
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=
#include<iostream>#include<cstring>#include<cstdio>#include<cmath>#include<algorithm>using namespace std;const int inf =0x3f3f3f3f;const int MAX = 1005; int n, m, s; int map[MAX][MAX], dis[MAX], vis[MAX]; void dijkstra(){ memset(vis,0,sizeof(vis)); int i,j,pos,minn; for(i=1; i<=n; i++) dis[i]=map[0][i]; vis[0]=1; for(i=0; i<=n; i++) { minn=inf; for(j=1; j<=n; j++) if(dis[j]<minn && !vis[j]) { minn=dis[j]; pos=j; } if(minn==inf) break; vis[pos]=1; for(j=1; j<=n; j++) { if(dis[j]>dis[pos]+map[pos][j] && !vis[j]) dis[j]=dis[pos]+map[pos][j]; } }}int main(){ while(~scanf("%d%d%d",&n,&m,&s)) { memset(map,inf,sizeof(map)); for(int i=0; i<=n; i++) map[i][i]=0; int p,q,t; for(int i=0; i<m; i++) { scanf("%d%d%d",&p,&q,&t); map[p][q]=min(map[p][q],t); } int w,tmp; scanf("%d",&w); for(int i=1; i<=w; i++) { scanf("%d",&tmp); map[0][tmp]=0; } dijkstra(); if(dis[s]==inf) printf("-1\n"); else printf("%d\n",dis[s]); }}
方法二:
#include <cstdio> #include <cstring> #include <algorithm> #define inf 0x3f3f3f3f using namespace std; const int MAX = 1005; int n, m, s; int map[MAX][MAX], dis[MAX], vis[MAX]; void dijkstra(int s) { memset(vis, 0, sizeof(vis)); for(int i=1; i<=n; i++) dis[i] = map[s][i]; //初始化 vis[s] = 1; dis[s] = 0; int minn,pos; for(int i=1; i<n; i++) { minn=inf; for(int j = 1; j <= n; j++) if(!vis[j] && dis[j] < minn) minn = dis[pos = j]; if(minn == inf) break; vis[pos] = 1; for(int j=1; j<=n; j++) if(!vis[j] && dis[pos] + map[pos][j] < dis[j]) dis[j] = dis[pos] + map[pos][j]; } } int main() { while(scanf("%d %d %d", &n, &m, &s) != EOF) { memset(map, inf, sizeof(map)); while(m--) { int x, y, w; scanf("%d %d %d", &x, &y, &w); if(w < map[y][x]) map[y][x] = w; //注意是有向的,反方向 } dijkstra(s); int a, b, ans = inf; scanf("%d", &b); for(int i=1; i<=b; i++) { scanf("%d", &a); ans = min(ans, dis[a]); //寻找最小的站点 } if(ans != inf)printf("%d\n", ans); else printf("-1\n"); } return 0; }
阅读全文
0 0
- HDU2680-Choose the best route(经典最短路问题dijkstra算法)
- hdu2680 Choose the best route 最短路(Dijkstra算法)
- hdu2680 Choose the best route 最短路,超级起点(多源Dijkstra算法,)
- HDU2680 Choose the best route (最短路)(Dijkstra算法)
- hdu2680最短路Choose the best route
- HDU2680 Choose the best route 最短路
- HDU - 2680 - Choose the best route (经典最短路问题dijkstra算法!!)
- HDU2680 Choose the best route(最短路)
- hdu2680 Choose the best route (dijkstra)
- Choose the best route hdu2680 Dijkstra
- hdu2680 Choose the best route (Dijkstra)
- HDU2680:Choose the best route(Dijkstra)
- HDU2680:Choose the best route(Dijkstra)
- HDU2680 Choose the best route 【Dijkstra】
- HDU2680---Choose the best route(dijkstra&&SPFA)
- hdu2680 Choose the best route (dijkstra)
- hdu 2680 Choose the best route (dijkstra算法 最短路问题)
- 【最短路+dijkstra】 2680 Choose the best route
- VS2015+QT5.7+opencv3.10图像处理
- C++中cstring和int互换
- Java源码分析-ArrayList
- 无线安全之破解WPA/WPA2 加密WiFi
- leetcode23. Merge k Sorted Lists总结
- HDU2680-Choose the best route(经典最短路问题dijkstra算法)
- HDU 1599 find the mincost route(Floyd无向图最小环)
- bzoj2209括号序列splay
- 暑假spring学习总结之初遇IoC
- 大型项目架构演变思想 由浅入深
- Spring/SpringMVC在启动完成后执行方法
- Vue实例练习
- oracle数据库想学点经验不是那么容易的看看总是好的
- [2017-08-25] 将MNIST数据集转换成图片形式并保存