题目大意: 已知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. 【BZOJ】【3280】小R的烦恼

    网络流/费用流 和软件开发那题基本相同,只是多加了一个“雇佣研究生”的限制:不同价格的研究生有不同的数量…… 那么只需加一个附加源点,对每一种研究生连边 S->ss 容量为l[i],费用为p[i ...

  2. idea maven添加jar包

    在“项目结构“里设置 选择libaray,添加jar包

  3. Leetcode#49 Anagrams

    原题地址 Anagram:变位词.两个单词是变位词关系的条件是:组成单词的字符相同,只是顺序不同 第一次看这道题看了半天没明白要干嘛,丫就不能给个样例输入输出么..后来还是看网上其他人的总结知道是怎么 ...

  4. js获得浏览器页面高宽

    不同的浏览器可能会有一些差别,使用的时候请先进行测试. var s = ""; s += " 网页可见区域宽:"+ document.body.clientWi ...

  5. VSS

    A deleted file of the same name already exists in this VSS project. Do you want to recover the delet ...

  6. 从浏览器的console获取angularjs的scope

    http://ionicframework.com/blog/angularjs-console/ 1: Access Scopes We can access any scope (even iso ...

  7. JavaScript 中的 this

    JavaScript 语言中的 this 由于其运行期绑定的特性,JavaScript 中的 this 含义要丰富得多,它可以是全局对象.当前对象或者任意对象,这完全取决于函数的调用方式.JavaSc ...

  8. Java开发WebService的几种方法--转载

    webservice的应用已经越来越广泛了,下面介绍几种在Java体系中开发webservice的方式,相当于做个记录. 1.Axis2 Axis是apache下一个开源的webservice开发组件 ...

  9. iOS视频压缩

    // // ViewController.m // iOS视频测试 // // Created by apple on 15/8/19. // Copyright (c) 2015年 tqh. All ...

  10. 评论 “App死亡潮:400万应用僵尸超八成,周期仅10月”

    点这里 原文: App死亡潮:400万应用僵尸超八成,周期仅10月 时间 2015-04-05 22:48:19  和讯科技相似文章 (16)原文  http://tech.hexun.com/201 ...