hdu 2680 最短路

原创 2016年05月31日 14:41:25

Choose the best route

TimeLimit: 2000/1000 MS (Java/Others)    Memory Limit:32768/32768 K (Java/Others)
Total Submission(s): 12423    Accepted Submission(s): 4039

Problem Description

One day , Kiki wants to visit one ofher friends. As she is liable to carsickness , she wants to arrive at herfriend’s home as soon as possible . Now give you a map of the city’s trafficroute, and the stations which are near Kiki’s home so that she can take. Youmay suppose Kiki can change the bus at any station. Please find out the leasttime Kiki needs to spend. To make it easy, if the city have n bus stations ,thestations 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 thenumber of directed ways between bus stations .(Maybe there are several waysbetween two bus stations .) s stands for the bus station that near Kiki’sfriend’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 willcosts t minutes .
Then a line with an integer w(0<w<n), means the number of stations Kikican take at the beginning. Then follows w integers stands for these stations.

 

 

Output

The output contains one line for eachdata set : the least time Kiki needs to spend ,if it’s impossible to find sucha route ,just output “-1”.

 

 

Sample Input

58 5

12 2

15 3

13 4

24 7

25 6

23 5

35 1

45 1

2

23

43 4

12 3

13 4

23 2

1

1

 

 

Sample Output

1

-1

 

 

Author

dandelion

 

 

Source

2009浙江大学计算机研考复试(机试部分)——全真模拟

 

 

分析:

把起点看成0的位置  把Kiki可以开始的点与起点的权值赋0 然后再找到s的最短路就可以了

注意:

从p到q是单向的  (被这个地方坑到了)

构造有向图  完美ac

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#define INF 0x3f3f3f3f
#define MAX 2000
using namespace std;
int vis[MAX],dis[MAX],map[MAX][MAX];
int n,m,s;
void dij()
{
    memset(dis,0,sizeof(dis));
    for(int i = 0; i <= n; i++)
        vis[i] = map[0][i];
    dis[0] = 1;
    int min,k;
    for(int i = 0; i <= n; i++)
    {
        min = INF;
        for(int j = 0; j <= n; j++)
        {
            if(!dis[j]&&vis[j] < min)
            {
                min = vis[j];
                k = j;
            }
        }
        if(min == INF)
            break;
        dis[k] = 1;
        for(int j = 0; j <= n; j++)
        {
            if(dis[j]==0&&vis[j] > vis[k] + map[k][j])
            {
                vis[j] = vis[k] + map[k][j];
            }
        }
    }
}
int main()
{
    int p,q,t;
    while(~scanf("%d%d%d",&n,&m,&s))
    {
        for(int i = 0; i <= n; i++)
            for(int j = 0; j <= n; j++)
                map[i][j] = INF;
        for(int i = 1; i <= m; i++)
        {
            scanf("%d%d%d",&p,&q,&t);
            if(t < map[p][q])
                map[p][q] = t;
        }
        int w,h;
        scanf("%d",&w);
        for(int i = 1; i <= w; i++)
        {
            scanf("%d",&h);
            map[0][h] = 0;
        }
        /*for(int i = 0; i <= n; i++)
        {
            for(int j = 0; j <= n; j++)
                printf("%d ",map[i][j]);
            printf("\n");
        }*/
        dij();
        if(vis[s]!=INF)
            printf("%d\n",vis[s]);
        else
            printf("-1\n");
    }
    return 0;
}


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

相关文章推荐

HDU 2680 Choose the best route (最短路+迪杰斯特拉)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2680 题面: 级新生如何加入ACM集训队?  ...

hdu2680最短路Choose the best route

hdu2680Choose the best route   先将多个起点用一个超级源点连接 ,权值为0 然后用dijkstra就可以得到结果了     #include using na...

HdU2680——Choose the best route(单源最短路Bell man-ford)

题目链接 题目大意简述:        “Kiki”要去看她的friend,从她家到她朋友家有很多公交站,所以也有很多路线,每段路花费时间不同,要求求耗时最短的坐车方式,输出最短时间,若无法到达,...

HDU 2680 Choose the best route 变形最短路 DIJKSTRA

题意:求多个源点单个汇点的最短路。 1000个点,很明显floyd会爆,用dijkstra和bellman ford\spfa应该都行把。。。 网上大神说了两种思路: 1.新建一个超级源点,和各...
  • hcbbt
  • hcbbt
  • 2013年11月28日 19:39
  • 1131

hdu 2680 最短路基础题 spfa实现 算法总结

题目链接http://acm.hdu.edu.cn/showproblem.php?pid=2680 题目大意,就是一个人可以从多个起点开始出发,看到终点的最短路是多少。。只有可以运用和hdu2...
  • liujc_
  • liujc_
  • 2015年04月13日 09:00
  • 583

HDU 2680 Choose the best route 【最短路 反向建图 dijkstra & SPFA 】

Choose the best route Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot...
  • wyjwyl
  • wyjwyl
  • 2015年08月17日 20:17
  • 228

HDU-2680 Choose the best route(最短路[Dijkstra])

第一道用优先队列写的最短路题。 受到前几题的影响,没仔细读题,认为是无向边,WA后重看发现是有向边,改过后就AC 由于起点Kiki附近的公交车站有很多,而Kiki's friend附近的公交车站只有...

#HDU 2680 Choose the best route 【SPFA最短路】

题目: Choose the best route Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 ...
  • Reskip
  • Reskip
  • 2016年03月02日 11:47
  • 161

HDOJ 2680 Choose the best route (最短路变形 Dijkstra && SPFA)

Choose the best route (最短路变形 Dijkstra) 最短路 Dijkstra(有向图)由于 是 多个 起点,而只有一个终点 ,每个起点循环一次 Dijkstra 算法 会...

poj-2680-Choose the best route-dijkstra(基础最短路)

题目大意: 已知n 个点,m条路线,s为终点;给出m条路线及其权值;给出w个起点,求最短路! 思路:基础的dijkstra,有向无环正权最短路,只要把终点和起点 reverse考虑便可。 AC代码...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:hdu 2680 最短路
举报原因:
原因补充:

(最多只允许输入30个字)