Choose the best route

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 8224    Accepted Submission(s): 2706

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

用Floyd怎么做都超时,用Dijstra。

题目说有几个可以选择的开始的点,如果一个一个比较这些点到最终点s的距离肯定会超时。

关键之处来了,要把几个点先汇到一个虚拟的点,这些点到这个虚拟的点的距离都是0,这样就可以一次就算出这些点到终点的最短距离的最小值。

代码:

#include<stdio.h>
#include<iostream>
using namespace std;
#define N 1111
#define INF 0x3f3f3f3f int a,b,x,n,s,m;
int mat[N][N];
int used[N],d[N];
void dijkstra()
{
for(int i=0;i<=n;i++)
{
used[i]=0;
d[i]=mat[0][i];
}
while(1)
{
int v=-1;
for(int u=0;u<=n;u++)
if(!used[u]&&(v==-1||d[u]<d[v]))
v=u;
if(v==-1)break;
used[v]=1;
for(int u=0;u<=n;u++)
d[u]=min(d[u],d[v]+mat[v][u]);
}
} int main()
{
while(~scanf("%d%d%d",&n,&m,&s))
{
for(int i=0;i<=n;i++)
for(int j=0;j<=n;j++)
if(i==j)
mat[i][j]=0;
else
mat[i][j]=INF;
while(m--)
{
scanf("%d%d%d",&a,&b,&x);
if(x<mat[a][b])
mat[a][b]=x;
}
int w,ww;
scanf("%d",&w);
while(w--)
{
scanf("%d",&ww);
mat[0][ww]=0;
}
dijkstra();
if(d[s]==INF)
cout<<-1<<endl;
else
cout<<d[s]<<endl;
}
return 0;
}

这题还有一个搞笑的地方,头文件用<stdio.h>可以,换成<cstdio>就超时了。所以以后都用<stdio.h>放弃,<cstdio>

版权声明:本文为博主原创文章,未经博主允许不得转载。

HDU2680 Choose the best route 最短路 分类: ACM 2015-03-18 23:30 37人阅读 评论(0) 收藏的更多相关文章

  1. 动态链接库(DLL) 分类: c/c++ 2015-01-04 23:30 423人阅读 评论(0) 收藏

    动态链接库:我们经常把常用的代码制作成一个可执行模块供其他可执行文件调用,这样的模块称为链接库,分为动态链接库和静态链接库. 对于静态链接库,LIB包含具体实现代码且会被包含进EXE中,导致文件过大, ...

  2. 认识C++中的临时对象temporary object 分类: C/C++ 2015-05-11 23:20 137人阅读 评论(0) 收藏

    C++中临时对象又称无名对象.临时对象主要出现在如下场景. 1.建立一个没有命名的非堆(non-heap)对象,也就是无名对象时,会产生临时对象. Integer inte= Integer(5); ...

  3. Windows7下QT5开发环境搭建 分类: QT开发 2015-03-09 23:44 65人阅读 评论(0) 收藏

    Windows7下QT开法环境常见搭配方法有两种. 第一种是:QT Creator+QT SDK: 第二种是:VS+qt-vs-addin+QT SDK: 以上两种均可,所需文件见QT社区,QT下载地 ...

  4. 树莓派安装mjpg-streamer视频监控 分类: Raspberry Pi 2015-04-12 23:41 144人阅读 评论(0) 收藏

    原来使用Motion在树莓派上跑1280x720分辨率的三颗摄像头.占用内存太严重,关闭诸多功能之后还是不行.故转战mjpg-streamer. 首先安装所需软件 sudo apt-get insta ...

  5. IOS即时通讯XMPP搭建openfire服务器 分类: ios技术 2015-03-07 11:30 53人阅读 评论(0) 收藏

    一.下载并安装openfire 1.到http://www.igniterealtime.org/downloads/index.jsp下载最新openfire for mac版 比如:Openfir ...

  6. 【从0到1学Web前端】CSS定位问题三(相对定位,绝对定位) 分类: HTML+CSS 2015-05-29 23:01 842人阅读 评论(0) 收藏

    引子: 开始的时候我想先要解决一个问题,怎么设置一个div盒子撑满整个屏幕? 看下面的html代码: <body> <div id="father-body"&g ...

  7. 积分图像 分类: 图像处理 Matlab 2015-06-06 10:30 149人阅读 评论(0) 收藏

    积分图像(integral image)是一种快速计算矩形区域之和的数据结构,常利用它对算法进行加速.积分图像中处的值是原始灰度图像的左上角与当前点所围成的矩形区域内所有像素点的灰度值之和,即: 其中 ...

  8. 循环队列 分类: c/c++ 2014-10-10 23:28 605人阅读 评论(0) 收藏

    利用线性表实现队列,为了有效利用空间,将其设计为循环结构,防止假溢出:牺牲一个存储单元以区分队空.队满. 设front队头,rear队尾,N为顺序表大小 队空:rear==front 队满:(rear ...

  9. 链表中用标兵结点简化代码 分类: c/c++ 2014-09-29 23:10 475人阅读 评论(0) 收藏

    标兵结点(头结点)是在链表中的第一个结点,不存放数据,仅仅是个标记 利用标兵结点可以简化代码.下面实现双向链表中的按值删除元素的函数,分别实现 带标兵结点和不带标兵结点两版本,对比可见标兵结点的好处. ...

随机推荐

  1. C#配置系统未能初始化

    如果配置文件中包含 configSections 元素,则 configSections 元素必须是 configuration 元素的第一个子元素.",将appSettings放到conf ...

  2. Linux磁盘占用100%解决方法

    /opt分区被web日志堆满了,导致一些服务无法正常运行,于是rm -fr掉这些日志(近11GB),但是服务仍没有恢复正常,用df -hT看,该分区占用还是100%: [root@anjing opt ...

  3. operator重载的使用

    C++的大多数运算符都可以通过operator来实现重载. 简单的operator+ #include <iostream> using namespace std; class A { ...

  4. linux下安装虚拟机qemu kqemu

    一,为什么要装虚拟机,为什么选择qemu 我的系统里面有3个linux系统,这些系统都是独立的,有的时候,我想一台电脑,能更真实的模拟二台,这个时候我们就可以装个虚拟机.其实如果真的很有钱的话,可能考 ...

  5. 【UVa-442】矩阵链乘——简单栈练习

    题目描述: 输入n个矩阵的维度和一些矩阵链乘表达式,输出乘法的次数.如果乘法无法进行,输出error. Sample Input 9 A 50 10 B 10 20 C 20 5 D 30 35 E ...

  6. 如何使用 Java 中的数组

    Java 中操作数组只需要四个步骤: 1. 声明数组 语法:  数据类型[ ] 数组名: 或者   数据类型 数组名[ ]: 其中,数组名可以是任意合法的变量名,如: 2. 分配空间 简单地说,就是指 ...

  7. 关于浮动-float

    1.存在浏览器兼容问题:js代码 2.对于这种存在浏览器兼容问题的问题,我们可以绕开兼容性问题,先在css样式写好,然后通过该变className 3.学习的博客 https://paran.io/c ...

  8. Oracle数据库“Specified cast is农田valid”

    这种错误是笔者在执行一条计算符合条件的行有多少个,用OracleDataReader读取计算出的行数时发生. 查询语句为: Select Count(1) FROM HP_TS Where TS_ID ...

  9. [Everyday Mathematic]20150216

    设 $A,B,C$ 是同阶方阵, 试证: $$\bex (A-B)C=BA^{-1}\ra C(A-B)=A^{-1}B. \eex$$

  10. C#调用WebService实现天气预报 http://www.webxml.com.cn

     C#调用WebService实现天气预报 2011-02-21 14:24:06 标签:天气预报 休闲 WebServices 职场 C# 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始 ...