题目大意: 已知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; }

作者:u011652573 发表于2014-3-6 0:18:03 原文链接
阅读:92 评论:0 查看评论

[原]poj-2680-Choose the best route-dijkstra(基础最短路)的更多相关文章

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

  2. hdu 2680 Choose the best route

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2680 Choose the best route Description One day , Kiki ...

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

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

  4. hdoj 2680 choose the best route

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

  5. hdu 2680 Choose the best route 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2680 题目意思:实质就是给定一个多源点到单一终点的最短路. 卑鄙题---有向图.初始化map时 千万不 ...

  6. HDU 2680 Choose the best route(SPFA)

    Problem DescriptionOne day , Kiki wants to visit one of her friends. As she is liable to carsickness ...

  7. HDU 2680 Choose the best route(多起点单终点最短路问题)题解

    题意:小A要乘车到s车站,他有w个起始车站可选,问最短时间. 思路:用Floyd超时,Dijkstra遍历,但是也超时.仔细看看你会发现这道题目好像是多源点单终点问题,终点已经确定,那么我们可以直接转 ...

  8. HDU 2680 Choose the best route 最短路问题

    题目描述:Kiki想去他的一个朋友家,他的朋友家包括所有的公交站点一共有n 个,一共有m条线路,线路都是单向的,然后Kiki可以在他附近的几个公交站乘车,求最短的路径长度是多少. 解题报告:这道题的特 ...

  9. Choose the best route(最短路)dijk

    http://acm.hdu.edu.cn/showproblem.php?pid=2680 Choose the best route Time Limit: 2000/1000 MS (Java/ ...

  10. 最短路问题-- Dijkstra Choose the best route

    Choose the best route Problem Description One day , Kiki wants to visit one of her friends. As she i ...

随机推荐

  1. 【转载】《Ext JS 4 First Look》翻译之一:新特性

    免责声明:     本文转自网络文章,转载此文章仅为个人收藏,分享知识,如有侵权,请联系博主进行删除.     原文作者:^_^肥仔John      原文地址:http://www.cnblogs. ...

  2. 深入理解jQuery中live与bind方法的区别

    本篇文章主要是对jQuery中live与bind方法的区别进行了详细的分析介绍,需要的朋友可以过来参考下,希望对大家有所帮助 注意如果是通过jq添加的层和对象一定要用live(),用其他的都不起作用 ...

  3. shell编程之echo命令

    Linux echo命令不能显示文件中的内容.显示字符串或者变量功能说明:显示文字.语 法:echo [-ne][字符串]或 echo [--help][--version]补充说明:echo会将输入 ...

  4. UVA 10078 The Art Gallery

    Problem: Century Arts has hundreds of art galleries scattered all around the country and you are hir ...

  5. 【BZOJ】【1040】【ZJOI2008】骑士

    树形DP/基环树DP 我掉坑掉了好多…… 这题图比较特殊,每个连通块都是一棵基环树(我一开始以为图是连通的了……sigh,我说为什么网上的题解都要累加ans……),那么对于一棵基环树,我们先dfs找到 ...

  6. Windows+Apache+MySQL+PHP(WAMP)环境搭建

    运行操作系统:Windows Server 2008 R2 Apache版本:Apache 2.2 MySQL版本:MySQL 5.5 PHP版本:PHP 5.6.14(当前最新版) 更新日期:201 ...

  7. ol3简介

    ol3从根本上进行了重新设计,相对于ol2,他符合现代浏览器的一些设计理念,使用了js新的特性,initial版本的目的是支持原来ol2的大多数功能,支持公网上流行的缓存切片,支持常见的矢量数据格式. ...

  8. eclipse下使用API操作HDFS

    1)使用eclipse,在HDFS上创建新目录 import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Fil ...

  9. 从CMO到龙泉寺弟子:佛法改变人生

    今天,移动精英开发社为大家请来了佛家弟子孫濟初师兄给大家分享学佛心得.濟初师兄根据自己的亲身经历,讲述了佛法对他人生的影响,以及他所获得的启示.本文系国内ITOM管理平台OneAPM授权转载. 孫濟初 ...

  10. 有关npm rum的3个简洁技巧

    [编者按]本文作者为来自 MongoDB 的 NodeJS 工程师 Valeri Karpov.Valeri 专注于维护常见的 Mongoose ODM,是<Professional Angul ...