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

Choose the best route

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9602    Accepted Submission(s): 3111

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
 

题解:最短路 注意有重边,这里介绍一种用链表存 的时候可以不用考虑重边,方法就是将所有的边都标记为未访问,然后将其他边的值标记成INF,将开始那条边的值标记成0 ,然后加入n边,每次更新的时候就不用考虑重边了。

这个题因为数据量特别的大,dijk的算法本身是O (n^2)的,查询的时候调用n次dijk所以总共是O(n^3),所以最后会超时,可以逆向思维,因为终点是已知的所以从终点开始dijk一次后找到所有的已知起点中距离最小的点就可以了。

注意这个题中是有向边。

下面是代码:

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 2000
#define M 400000
#define INF 0x1fffffff
struct Edge{
int to ;
int v;
int next;
}edge[M];
int head[N];
int Ecnt;
void init()
{
Ecnt = ;
memset(head,-,sizeof(head));
}
void add(int from , int to ,int v)
{
edge[Ecnt].to = to;
edge[Ecnt].v = v;
edge[Ecnt].next = head[from];
head[from] = Ecnt++;
}
int dist[N];
bool p[N];
void dijk(int s, int n)
{
int i , j , k ;
for(i = ;i <= n ;i++)
{
p[i] = false;
dist[i] = INF;
}
//p[s] = true;
dist[s] = ; for( i = ; i < n ; i++)
{
int Min = INF ;
int k = ;
for( j = ; j <= n ; j++)
{
if(!p[j]&&dist[j]<Min)
{
Min = dist[j];
k = j;
}
}
if(Min == INF ) return ;
p[k] = true;
for(j = head[k]; j != - ; j = edge[j].next)
{
Edge e = edge[j];
if(!p[e.to]&&dist[e.to]>dist[k]+e.v)
dist[e.to] = dist[k]+e.v;
}
}
}
int main()
{
int n , m , s ;
while(~scanf("%d%d%d",&n,&m,&s))
{
init();
for(int i = ;i < m ; i++)
{
int p , q , t ;
scanf("%d%d%d",&p,&q,&t);
add(q,p,t);//逆向扫描,所以逆向加边
}
dijk(s,n);
int ss;
scanf("%d",&ss);
int ans = INF;
for(int i = ;i < ss; i++)
{
int w ;
scanf("%d",&w);
ans = min(ans,dist[w]);
}
if(ans==INF) printf("-1\n");
else printf("%d\n",ans);
}
return ;
}

Choose the best route(最短路)dijk的更多相关文章

  1. HDU2680 Choose the best route 最短路 分类: ACM 2015-03-18 23:30 37人阅读 评论(0) 收藏

    Choose the best route Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  2. hdu-2680 Choose the best route(最短路)

    题目链接: Choose the best route Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 32768/32768 K ( ...

  3. hdu2680 Choose the best route 最短路(多源转单源)

    此题中起点有1000个,边有20000条.用链式前向星建图,再枚举起点用SPFA的话,超时了.(按理说,两千万的复杂度应该没超吧.不过一般说计算机计算速度 1~10 千万次/秒.也许拿最烂的计算机来卡 ...

  4. hdu 2680 Choose the best route

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

  5. HDU2680 Choose the best route 2017-04-12 18:47 28人阅读 评论(0) 收藏

    Choose the best route Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Othe ...

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

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

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

  8. BZOJ 1266: [AHOI2006]上学路线route(最短路+最小割)

    第一问最短路.第二问,先把最短路的图建出来(边(u,v)满足d[s->u]+d[v->t]+d(u,v)==最短路径长度,就在图中,可以从源点和汇点分别跑一次最短路得到每个点到源点和汇点的 ...

  9. 最短路<dijk>

    题意: 有n个城市,有m条路,给出每条路的出发和结束的城市及长度,求从第一个城市到最后一个城市的最短路.按格式输出. power oj 2443 题解: 标准dijk算法. #include<c ...

随机推荐

  1. InnoDB 逻辑存储结构

    本文同时发表在https://github.com/zhangyachen/zhangyachen.github.io/issues/80 如果创建表时没有显示的定义主键,mysql会按如下方式创建主 ...

  2. DOM&JavaScript示例&练习

    以下示例均为html文件,保存至本地就可直接用浏览器打开以查看效果\(^o^)/~ 练习一:设置新闻字体 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTM ...

  3. oracle 处理时间和金额大小写的相关函数集合

    CREATE OR REPLACE FUNCTION MONEY_TO_CHINESE(MONEY IN VARCHAR2) RETURN VARCHAR2 IS C_MONEY ); M_STRIN ...

  4. 3、公司开会的必要性 - CEO之公司管理经验谈

    这几天在考虑开公司的问题.以前也有想过开公司创业,但是由于资金和团队问题搁置了.今天在网上看到了一篇文“[转]微软是这么管理员工的!你一定向往!”,想起以前在其它公司时开的一些会议的问题,就写了此文, ...

  5. thinkinginjava学习笔记03_基本语法

    由于java是c系语言,基本保留c语言的所有基本操作,就快速过一下: java中的基本操作符仅仅对基本类型有效:=.==.!=对所有对象有效(值传递),String类支持+.+=; 在对基本数据进行算 ...

  6. Django学习日记07_Admin

    django.contrib django.contrib是django中附带的一个工具集,由很多的附加组件组成.这些附加组件包括管理工具(django.contrib.admin).用户鉴别系统(d ...

  7. golang 用tar打包文件或文件夹

    打包文件用到了tar包,其中tar包的用法可以参考API golang提供了个函数用来遍历文件夹 filepath.Walk 函数具体描述如下: func Walk(root string, walk ...

  8. phpstorm+wamp+xdebug配置php调试环境

    本篇文章主要是:教大家如果搭建一套phpstorm+wamp+xdebug调试php的环境现在大多数的程序员使用的调试方式一般都是echo, var_dump, file_put_contents等其 ...

  9. MicroPython支持的开发板:高性能、低成本创客首选

    Python的开放.简洁.黏合正符合了现发展阶段对大数据分析.可视化.各种平台程序协作产生了快速的促进作用.自Python3的发布到现在已有五六年的时间,从刚发布的反对声音到慢慢被接受与喜欢经过了太漫 ...

  10. iOS 关于退出键盘两种方法和避免遮挡

    退出键盘: 方法1:不使用代理,直接使用: -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self.textFi ...