题目链接:

Choose the best route

Time Limit: 2000/1000 MS (Java/Others)   

 Memory Limit: 32768/32768 K (Java/Others)

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
 
题意
 
给一个有向图,问从多个起点的任意一个出发到达终点的最短时间;
 
思路
 
把0当做所有起点的起点,那么这些起点到0的距离都是0,这样可以用dijkstra算法跑一波得到答案,如果用Floyd算所有节点对应该会tle;
 
AC代码
 
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL mod=1e9+;
const int N=1e5+;
const int inf=0x3f3f3f3f;
int n,m,s;
int p[][],flag[],dis[];
void dijkstra()
{
memset(flag,,sizeof(flag));
for(int i=;i<=n;i++)
{
dis[i]=p[][i];
}
flag[]=;
int temp;
for(int i = ;i <= n;i++)
{
int mmin=inf;
for(int j = ;j<=n;j++)
{
if(!flag[j]&&dis[j]<mmin)
{
mmin=dis[j];
temp=j;
}
}
if(mmin == inf)break;
flag[temp]=;
for(int j=;j<=n;j++)
{
if(dis[j]>dis[temp]+p[temp][j])
dis[j]=dis[temp]+p[temp][j];
}
}
}
int main()
{
while(scanf("%d%d%d",&n,&m,&s)!=EOF)
{
for(int i=;i<=n;i++)
{
dis[i]=inf;
for(int j=;j<=n;j++)
{
if(i == j)p[i][j]=;
else p[i][j]=inf;
}
}
int u,v,w;
for(int i = ;i < m;i ++)
{
scanf("%d%d%d",&u,&v,&w);
p[u][v]=min(p[u][v],w);
}
int num,x;
scanf("%d",&num);
for(int i=;i<num;i++)
{
scanf("%d",&x);
p[][x]=;
}
dijkstra();
if(dis[s] == inf)printf("-1\n");
else printf("%d\n",dis[s]);
} return ;
}

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

  1. hdu 2680 Choose the best route

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

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

  3. hdu 2680 Choose the best route 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2680 题目意思:实质就是给定一个多源点到单一终点的最短路. 卑鄙题---有向图.初始化map时 千万不 ...

  4. HDU 2680 Choose the best route(SPFA)

    Problem DescriptionOne day , Kiki wants to visit one of her friends. As she is liable to carsickness ...

  5. hdu 2680 Choose the best route (dijkstra算法)

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

  6. HDU 2680 Choose the best route 最短路问题

    题目描述:Kiki想去他的一个朋友家,他的朋友家包括所有的公交站点一共有n 个,一共有m条线路,线路都是单向的,然后Kiki可以在他附近的几个公交站乘车,求最短的路径长度是多少. 解题报告:这道题的特 ...

  7. HDU 2680 Choose the best route(多起点单终点最短路问题)题解

    题意:小A要乘车到s车站,他有w个起始车站可选,问最短时间. 思路:用Floyd超时,Dijkstra遍历,但是也超时.仔细看看你会发现这道题目好像是多源点单终点问题,终点已经确定,那么我们可以直接转 ...

  8. 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 ...

  9. HDU 2068 Choose the best route

    http://acm.hdu.edu.cn/showproblem.php?pid=2680 Problem Description One day , Kiki wants to visit one ...

随机推荐

  1. iOS APP 架构漫谈[转]

      Mark  一下 很不错的文章   最近看了一些有关server的东西,一些很简单的东西,不外乎是一些文档规范,另外结合最近看的wwdc的一些video,觉得对软件架构(software arch ...

  2. android 活动

    一.Activity 声明周期 1 创建 把页面上的个元素加载到内存 onCreate 2 开始 把页面显示到屏幕 onStart 3 恢复 让页面在屏幕活动 onResume 4 暂停 停止页面动作 ...

  3. Docker资源限制实现——cgroup

    摘要 随着Docker技术被越来越多的个人.企业所接受,其用途也越来越广泛.Docker资源管理包含对CPU.内存.IO等资源的限制,但大部分Docker使用者在使用资源管理接口时往往还比较模糊. 本 ...

  4. jmeter Plugins Manager插件管理

    在 https://jmeter-plugins.org/downloads/all/ 下载插件,放到lib/ext Download plugins-manager.jar and put it i ...

  5. 【Todo】Java Callable和Future学习

    参考这篇文章:http://blog.csdn.net/ghsau/article/details/7451464  还有一个系列<Java多线程>

  6. 使用crontab定时执行脚本时别忘了输出重定向>

    原文:https://blog.csdn.net/solmyr_biti/article/details/50683279 -------------------------------------- ...

  7. 关于android 使用bitmap的OOM心得和解决方式

    android开发,从2010年開始学习到如今的独立完毕一个app,这漫长的四年,已经经历了非常多次bug的折磨.无数次的加班训练.然而,自以为自己已经比較了解android了,却近期在一个项目上.由 ...

  8. 使用RPi-Monitor监控、统计Guitar的运行状态

    前言 之前发在ickey社区上的一系列文章: 犹抱琵琶半遮面,无人知是荔枝来--unboxing & interview 一.二.三 葡萄美酒夜光杯,巧妇难为无米炊--资料与社区 一支穿云箭, ...

  9. 推断dxf文件的版本号

    打开DXF參考手冊,在DXF參考手冊中,点击"索引"-->输入"HEADER",在ACADVER字段有acd的版本号信息: 以下是用C语言,写的推断dxf ...

  10. react 使用 moment 进行 日期格式化

    在react中使用得先导入: import moment from 'moment'; 调用: npm install moment var moment = require('moment'); m ...