他说少年如歌

未来的成功来源于你现在的努力

hdu 2680 最短路

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


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_27437781/article/details/51545613
文章标签: 最短路 数据结构
个人分类: 最短路
想对作者说点什么? 我来说一句

最短路径 贪心实现代码

2009年09月06日 2KB 下载

没有更多推荐了,返回首页

不良信息举报

hdu 2680 最短路

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭