[原]poj-2680-Choose the best route-dijkstra(基础最短路)
题目大意: 已知n 个点,m条路线,s为终点;给出m条路线及其权值;给出w个起点,求最短路!
思路:基础的dijkstra,有向无环正权最短路,只要把终点和起点 reverse考虑便可。
AC代码如下:
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
#define INF 1000000
#define M 1010
int n, m, s;
int d[M];
int w[M][M];
int v[M]; void dijkstra(int s)
{
memset(v,0,sizeof(v));
memset(d,INF,sizeof(d));
d[s] = 0;
for(int i = 0; i < n; i++)
{
int x, m = INF; //WA了半天找不到的bug,就是这里;开始忘了放在循环里了,ORZ。。。所以m会一直是0.。不WA才怪。
for(int j = 1; j <= n; j++)
if(!v[j]&&d[j] <= m) m = d[x = j];
v[x] = 1; for(int j = 1; j <= n; j++)
d[j] = d[j] < d[x] + w[x][j]? d[j]:d[x] + w[x][j]; }
} void init(int n)
{
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
w[i][j] = INF;
} int main()
{
while(scanf("%d%d%d",&n, &m, &s) == 3)
{
init(n);
for(int i = 0; i < m; i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
if(w[b][a] > c)
w[b][a] = c;
}
dijkstra(s); int la,st,Min = INF;
scanf("%d",&la); for(int i = 0; i < la; i++)
{
scanf("%d",&st);
if(d[st] < Min) Min = d[st];
//cout<<st<<"*"<<d[st]<<endl;
}
if(Min < INF)
printf("%d\n",Min);
else
cout<<"-1"<<endl;
}
return 0; }
[原]poj-2680-Choose the best route-dijkstra(基础最短路)的更多相关文章
- 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 ( ...
- hdu 2680 Choose the best route
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2680 Choose the best route Description One day , Kiki ...
- hdu 2680 Choose the best route (dijkstra算法)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2680 /************************************************* ...
- hdoj 2680 choose the best route
Problem Description One day , Kiki wants to visit one of her friends. As she is liable to carsicknes ...
- hdu 2680 Choose the best route 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2680 题目意思:实质就是给定一个多源点到单一终点的最短路. 卑鄙题---有向图.初始化map时 千万不 ...
- HDU 2680 Choose the best route(SPFA)
Problem DescriptionOne day , Kiki wants to visit one of her friends. As she is liable to carsickness ...
- HDU 2680 Choose the best route(多起点单终点最短路问题)题解
题意:小A要乘车到s车站,他有w个起始车站可选,问最短时间. 思路:用Floyd超时,Dijkstra遍历,但是也超时.仔细看看你会发现这道题目好像是多源点单终点问题,终点已经确定,那么我们可以直接转 ...
- HDU 2680 Choose the best route 最短路问题
题目描述:Kiki想去他的一个朋友家,他的朋友家包括所有的公交站点一共有n 个,一共有m条线路,线路都是单向的,然后Kiki可以在他附近的几个公交站乘车,求最短的路径长度是多少. 解题报告:这道题的特 ...
- Choose the best route(最短路)dijk
http://acm.hdu.edu.cn/showproblem.php?pid=2680 Choose the best route Time Limit: 2000/1000 MS (Java/ ...
- 最短路问题-- Dijkstra Choose the best route
Choose the best route Problem Description One day , Kiki wants to visit one of her friends. As she i ...
随机推荐
- 【BZOJ】【3439】Kpm的MC密码
Trie树/可持久化线段树 神题啊……搞了我一下午= =(其实第233个提交也是我的) 我一开始的思路:这个找kpm串的过程,其实就跟在AC自动机上沿fail倒着往下走是差不多的(看当前是哪些点的后缀 ...
- 序列化Color对象
如下: public class Class2 { [XmlIgnore] public Color Color1 { get { return color1; } set { color1 = va ...
- Hdu 1507 Uncle Tom's Inherited Land* 分类: Brush Mode 2014-07-30 09:28 112人阅读 评论(0) 收藏
Uncle Tom's Inherited Land* Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- aspx文件、aspx.cs文件、aspx.designer.cs文件之讲解
.aspx文件:(页面)书写页面代码.存储的是页面design代码.只是放各个控件的代码,处理代码一般放在.cs文件中. .aspx.cs文件:(代码隐藏页)书写类代码.存储的是程序代码.一般存放与数 ...
- href=#与href=javascriptvoid(0)的区别
#"包含了一个位置信息 默认的锚点是#top 也就是网页的上端 而javascript:void(0) 仅仅表示一个死链接 这就是为什么有的时候页面很长浏览链接明明是#可是跳动到了页首 而 ...
- Unity3D开发Windows Store应用程序 注意事项
原地址:http://blog.csdn.net/jbjwpzyl3611421/article/details/12704491 针对最近在移植window store项目中遇到的问题,我整理了官方 ...
- PHP 使用 Redis
PHP 使用 Redis 安装 开始在 PHP 中使用 Redis 前, 我们需要确保已经安装了 redis 服务及 PHP redis 驱动,且你的机器上能正常使用 PHP. 接下来让我们安装 PH ...
- vi/vim使用指北 ---- Beyond the Basic
更多的组合命令 [number]-[command]-[test object] number: 数字 comand: c,d,y (修改,删除,复制) test object: 移动光标的命 ...
- Swift 2.0 到底「新」在哪?
[编者按]2015年6月,一年一度的苹果 WWDC 大会如期而至,在大会上苹果发布了 Swift 2.0,引入了很多新的特性,以帮助开发者更快.更简单地构建应用.本篇文章作者是 Maxime defa ...
- F.I.S本地环境的搭建教程
一.准备开发环境: 1.安装JRE 2.安装nodejs 最好是msi文件,比较省事. 3.(如果是PHP项目)安装php. 首先下载php(我的是5.5.15版本,win7 64位系统) zip,然 ...