题目大意:

给你一个有向图,一个起点集合,一个终点,求最短路。。。。

解题思路:

1.自己多加一个超级源点,把起点集合连接到超级源点上,然后将起点与超级源点的集合的路径长度设为0,这样就称为一个n+1个点的单源最短路算法。

 #include <stdio.h>
#include <string.h> int map[][];
int n; int Dijkstra(int s,int e){
bool done[];
int d[];
memset(done,,sizeof(done));
for(int i = ;i <= n;i++)
d[i] = (i == s ? : );
for(int i = ;i <= n;i++){//最多执行n+1次操作
int minx,minn = ;
for(int j = ;j <= n;j++)//先找到d[]最小的点
if(!done[j] && d[j] < minn){
minn = d[j];
minx = j;
}
done[minx] = ;//将该点加入集合
if(minx == e)
return d[minx];
for(int j = ;j <= n;j++){//再更新所有的d[]
if(!done[j] && d[minx] + map[minx][j] < d[j]){
d[j] = d[minx] + map[minx][j];
}
}
}
return -;//如果没有找到到终点的路径,返回-1
} int main(){
int m,s;
int i,j,k;
int p,q,t;
int w,ww,ans; while(scanf("%d%d%d",&n,&m,&s) != EOF){
ans = ;
for(i = ;i < ;i++){
for(j = ;j < ;j++){
if(i == j)
map[i][j] = ;
else
map[i][j] = ;
}
} while(m--){
scanf("%d%d%d",&p,&q,&t);
if(t < map[p][q])//可能两站间存在多条线路取短的那条路
map[p][q] = t;
}
scanf("%d",&w);
while(w--){
scanf("%d",&ww);
map[][ww] = ;
}
ans = Dijkstra(,s);//巧妙之处,加入超级源点0
if(ans == -)
printf("-1\n");
else
printf("%d\n",ans);
}
return ;
}

Choose the best route

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 12   Accepted Submission(s) : 2

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

Problem 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=<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.

Output

The output contains one line for each data set : the least time Kiki
needs to spend ,if it’s impossible to find such a route ,just output
“-1”.

Sample Input

5 8 5
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

Sample Output

1
-1

Author

dandelion

HDOJ 2680 Dijkstra的更多相关文章

  1. hdoj 1874 dijkstra

    在做PAT的甲1003,思考DFS和图什么的,时间紧张直接去看柳神(日后上传柳神的C++版本)的订阅,得知是dijkstra,转去用hdoj 1874练手,写了两天,终于调出来了 题目链接:http: ...

  2. hdoj 2680 choose the best route

    Problem Description One day , Kiki wants to visit one of her friends. As she is liable to carsicknes ...

  3. hdu 2680 Choose the best route (dijkstra算法)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2680 /************************************************* ...

  4. 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 ( ...

  5. Choose the best route HDU杭电2680【dijkstra算法 || SPFA】

    http://acm.hdu.edu.cn/showproblem.php?pid=2680 Problem Description One day , Kiki wants to visit one ...

  6. hdu 2680 最短路径(dijkstra算法+多源最短路径单源化求最小值)这题有点意思

    Choose the best route Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  7. hdoj 1874 畅通工程续【dijkstra算法or spfa算法】

    畅通工程续 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  8. hdoj 2544 最短路【dijkstra or spfa】

    最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  9. HDOJ/HDU 2544 最短路---dijkstra算法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544 这题的思路可以见这里(同一类型):http://blog.csdn.net/xiaozhuaix ...

随机推荐

  1. poj 3128 Leonardo&#39;s Notebook(置换的幂)

    http://poj.org/problem?id=3128 大致题意:输入一串含26个大写字母的字符串,能够把它看做一个置换.推断这个置换是否是某个置换的平方. 思路:具体解释可參考url=ihxG ...

  2. Ubuntu 15.04 安装rmagick 2.15.4

    apt-get install  pkg-config, libmagick-dev,libmagickcore-dev, libmagickwand-dev, gem install rmagick

  3. STL之stack(栈)

    栈(statck)这种数据结构在计算机中是相当出名的.栈中的数据是先进后出的(First In Last Out, FILO).栈只有一个出口,允许新增元素(只能在栈顶上增加).移出元素(只能移出栈顶 ...

  4. 调用 sphinx-build生成HTML文件

    安装 Sphinx $ easy_install sphinx Searching for sphinx Reading http://pypi.python.org/simple/sphinx/ R ...

  5. phpeclipse

    http://phpeclipse.sourceforge.net/update/stable/1.2.x/

  6. (IOS)国际本地化设置

    首先New File,在Resource中选择Strings File: 然后命名该strings文件,必须命名为 Localizable : 再者选中该strings文件,在Localization ...

  7. asp.net 页面执行过程

    Application_BeginRequest Application_AuthenticateRequest Application_AuthorizeRequest Application_Re ...

  8. [代码搜索]OpenGrok搭建简易教程

    面对着动辄几十GB且随时不断更新的大型代码,我们产生了以下需求:1.快速搜索代码2.代码存放于本地/服务器3.代码可跳转4.跨平台5.易于维护... 显然SourceInsight.ctags.gre ...

  9. vs2013内置IISExpress相关问题

    问题描述,以前做的程序迁移到vs2013后出现500.22问题. HTTP 错误 500.22 - Internal Server Error 检测到在集成的托管管道模式下不适用的 ASP.NET 设 ...

  10. Read and Write NSArray, NSDictionary and NSSet to a File

    查询地址:http://iosdevelopertips.com/data-file-management/read-and-write-nsarray-nsdictionary-and-nsset- ...