题目链接:

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. 【转】UITableViewCell自适应高度 UILabel自适应高度和自动换行

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {     ...

  2. 有向图最小路径覆盖方法浅析、证明 //hdu 3861

    路径覆盖就是在图中找一些路径,使之覆盖了图中的所有顶点,且任何一个顶点有且只有一条路径与之关联. 对于一个有向无环图怎么求最小路径覆盖? 先构造二分图: 对于原图,先拆点,吧每个点i拆成ii,iii. ...

  3. iscroll API

    概况 资料来源 http://cubiq.org/iscroll-4 http://www.cnblogs.com/wanghun/archive/2012/10/17/2727416.html ht ...

  4. Spring实战Day6

    3.4 bean的作用域 Spring中bean的作用域 单例(Singleton):在整个应用中,只创建bean的一个实例. 原型(Prototype):每次注入或者通过Spring应用上下文获取的 ...

  5. synchronized初识

    作用域: 1.对象实例内--->People jack = new Jack(); ①此作用域内的synchronized锁 ,可以防止多个线程同时访问这个对象的synchronized方法 ② ...

  6. Oracle ORA-01033: ORACLE initialization or shutdown in progress

    先说明,我出现此错误的原因是:我手动通过drop语句删除表空间,结果磁盘中文件还存在,然后我手动删除了文件,重启了oracle服务,再去连接oracle时就出现了这个错误. 网上也有“连接Oracle ...

  7. sqlplus登陆scott用户,以及退出连接

    进入sqlplus界面 即登陆成功,PLsql也一样 退出连接:

  8. android CheckBox使用和状态获得

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools=&q ...

  9. 【gradle】mac上安装gradle

    根据gradle官网指导,一步一步安装即可 https://gradle.org/install/ 或者,如果你没有办法墙出去,或者本地使用命令下载gradle比较慢的话,可以采用下面的方式 ==== ...

  10. poj 1694 An Old Stone Game 树形dp

    //poj 1694 //sep9 #include <iostream> #include <algorithm> using namespace std; const in ...