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. UVa 821 Page Hopping【Floyd】

    题意:给出一个n个点的有向图,任意两个点之间都相互到达,求任意两点间最短距离的平均值 因为n很小,所以可以用floyd 建立出图,然后用floyd,统计d[][]不为0且不为INF的边的和及条数,就可 ...

  2. BZOJ 3295 动态逆序对

    调了好久.... 转化成三维偏序,cdq处理. 好像比较快? #include<iostream> #include<cstdio> #include<cstring&g ...

  3. BZOJ 1076 奖励关

    注意几点: 1.为什么要逆推?由此状态可以轻易算出彼状态是否可行,而彼状态却无法轻易还原为此状态. 2.为什么可以逆推?假设时光倒流了....23333 3.注意位运算的准确,大胆写方程. #incl ...

  4. acdream 1682 吃不完的糖果(环形最大子段和)

    Problem Description 娜娜好不容易才在你的帮助下"跳"过了这个湖,果然车到山前必有路,大战之后必有回复,大难不死,必有后福!现在在娜娜面前的就是好多好多的糖果还有 ...

  5. pandas.Panel数据

    from pandas import Panel, DataFrame import numpy as np dd = {} for i in range(1, 3): name = 'X' + st ...

  6. jquery中html()/text()/val()区别

    html就是你可以添加<span></span><li></li>的标记text只能写文本如果写了上面的标记则会以文本形式输出,就是输出标签体的内容va ...

  7. 收集些日本的VPS

    http://www.serverqueen.jp/service/vps.html http://www.seeds.ne.jp http://dream.jp/vps/ http://fc2-vp ...

  8. Android View绘制13问13答

    1.View的绘制流程分几步,从哪开始?哪个过程结束以后能看到view? 答:从ViewRoot的performTraversals开始,经过measure,layout,draw 三个流程.draw ...

  9. gcc-4.8.3安装,gdb-7.6安装

    gdb用法: http://blog.chinaunix.net/uid-26548237-id-3435525.html gdb-7.6.tar.gz:  (官网下载:http://ftp.gnu. ...

  10. SQL0668N 由于表 "db2inst1.test" 上的原因代码 "3",所以不允许操作(解因为LOAD引起的LOAD暂挂状态锁)

    DB2解因为LOAD引起的LOAD暂挂状态锁   一般解锁命名是,SET INTEGRITY FOR temp_test IMMEDIATE CHECKED   但是load暂挂状态是解不了的,可以l ...