题目大意: 已知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】【2938】【POI2000】病毒

    AC自动机 好题>_<(其实是一次AC有些感动) 嗯要找到无限长的一个字符串不包含任何一个模板串,就意味着在AC自动机(Trie图)上找到一个不经过任何一个危险结点的环,深搜一下就好了…… ...

  2. 【bzoj1013】[JSOI2008]球形空间产生器sphere

    1013: [JSOI2008]球形空间产生器sphere Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 4530  Solved: 2364[Subm ...

  3. openOffice将doc在线预览

    最近,有个项目要用到类似DOCIN的文档转换和阅读的功能,于是就开始找相关的资料,最后总结出2种解决办法,以下就来探讨下两种方法的各自实现. 第一种:通过FLASH PAPER来转换DOC文档直接生成 ...

  4. codeforces #232 div2 解题报告

    A:简单题:我们可以把点换成段处理,然后枚举段看是否被霸占了: #include<iostream> #include<]; ]=;     ;i<=n;i++)     { ...

  5. javascript笔记 面向对象

    Javascript是一种面向对象的弱语言,既然有面向对象,就有继承 继承: 1.call函数和apply函数:区别在于它们参数上的不同,固定参数的用call,可变参数的用apply.换句话说,就是a ...

  6. poj 1986

    Distance Queries Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 8638   Accepted: 3032 ...

  7. 【leetcode】Multiply Strings(middle)

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  8. 真机模拟器.a文件编译报错

  9. (12)nehe教程6 纹理映射

    在这一课里,我将教会你如何把纹理映射到立方体的六个面. 学习 texture map 纹理映射(贴图)有很多好处.比方说您想让一颗导弹飞过屏幕.根据前几课的知识,我们最可行的办法可能是很多个多边形来构 ...

  10. (3)VS2010+Opencv-2.4.8的配置攻略

    这是windows平台上的东西,我为什么要写到安卓这一块呢 因为作者做的安卓方面的东西需要先在windows平台实现一下,所以就想写这篇东西,也参考了网上很多教程,不得不感叹,这些软件版本更新的太快. ...