hdu2680 Choose the best route 最短路,超级起点(多源Dijkstra算法,)
2015-08-10 19:46
399 查看
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=
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<stdio.h> #include<queue> #include<stack> #include<algorithm> #include<string.h> #include<string> #include<math.h> using namespace std; int maze[1005][1005]; int dis[1005]; int vis[1005]; #define INF 0x7f7f7f int main() { int n,m,s; while(scanf("%d%d%d",&n,&m,&s)!=EOF) { memset(maze,INF,sizeof(maze)); memset(vis,false,sizeof(vis)); int st,en,len; for(int i=1;i<=m;i++) { scanf("%d%d%d",&st,&en,&len); if(maze[st][en]>len) { maze[st][en]=len; } } int w; scanf("%d",&w); for(int i=1;i<=w;i++) { scanf("%d",&st); maze[0][st]=0; } for(int i=1;i<=n;i++) dis[i]=INF; dis[0]=0; for(int i=0;i<=n;i++) { int x,m=INF; for(int y=0;y<=n;y++) { if(!vis[y]&&m>=dis[y]) { m=dis[y]; x=y; } } vis[x]=true; for(int y=0;y<=n;y++) { if(dis[y]>dis[x]+maze[x][y]) { dis[y]=dis[x]+maze[x][y]; } } } if(dis[s]==INF) printf("-1\n"); else printf("%d\n",dis[s]); } return 0; }
相关文章推荐
- 535 Error: authentication failed
- 数据结构(15)静态链表
- A+B
- 一道面试题细说C++类型转换
- PHP中间uniqid在高并发重复问题
- 基于 Android NDK 的学习之旅-----HelloWorld
- 算法1:k-近邻
- java基础第十讲——数据库、单例设计模式、服务器、项目(数据库—服务器—网络)
- NOIP2009 Hankson的趣味题
- 【COCI 2013】Inspeaker
- 数据库
- JDBC应用小案例
- 创建UIWebView
- 数据结构(14)单链表
- MySQL数据库与登录注册
- 防止.NET应用程序内存溢出(OOM)的一些措施
- CodeForces 4C-Registration system
- HTML笔记
- C语言-Branch
- poj-1611 The Suspects