关闭

hdu 2680 最短路

标签: 最短路数据结构
95人阅读 评论(0) 收藏 举报
分类:

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;
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:15662次
    • 积分:1213
    • 等级:
    • 排名:千里之外
    • 原创:108篇
    • 转载:19篇
    • 译文:0篇
    • 评论:0条