题目大意:

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

解题思路:

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. uva 1561 - Cycle Game(推理)

    option=com_onlinejudge&Itemid=8&page=show_problem&problem=4336" style=""& ...

  2. mybatis级联查询

    1.定义四个实体.User   Role    Privilege   Resource,他们之间的对于关系为 2.需求:我通过用户名username查找出该用户对应的角色以及角色对应的权限和资源 3 ...

  3. python下yield(生成器)

    python下的协程: #encoding=utf-8 """ 协程----微小的进程 yield生成器-----生成一个可迭代对象比如list, tuple,dir 1 ...

  4. windows cmd: 打开windows系统程序或服务的常见命令

    Windows常用CMD命令 http://www.cnblogs.com/sbaicl/archive/2013/03/05/2944001.html 其实查找Windows自带程序的命令行很简单, ...

  5. WRTnode 的 HTTP Web PWM 调光实验(2016-05-16)

    前言 这里是节取自 物联网的任意门——WRTnode2R 评测 中的 http web PWM 调光灯实验,所以有一些前置设置如果没有描述清楚可参考该处. 正文 步骤一:编辑 html 文件放在 /w ...

  6. 网站压力测试工具之WebBench

    1 Web Bench简介 WebBench是有名的网站压力测试工具,由Lionbridge公司开发,最多可以模拟3万个并发连接去测试网站的负载能力. Webbech能测试处在相同硬件上,不同服务的性 ...

  7. Android Activity和Intent机制学习笔记

    转自 http://www.cnblogs.com/feisky: Activity Android中,Activity是所有程序的根本,所有程序的流程都运行在Activity之中,Activity具 ...

  8. Python之路Day14

    主要内容:jQuery进阶.CSS伪类和伪元素.jQuery插件 tab菜单样式 checkbox全选.反选 位置:scrollTop和offset 事件:两种绑定事件的方式和委托delegate a ...

  9. (zz)Linux下Gcc生成和使用静态库和动态库详解

    http://blog.chinaunix.net/uid-23592843-id-223539.html

  10. Python学习之路——模块

    一.模块: 模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需 ...