HDU 1548 A strange lift (Dijkstra算法)

来源:互联网 发布:数据科学家养成手册 编辑:程序博客网 时间:2024/05/21 10:48

Choose the best route

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 62   Accepted Submission(s) : 27

Font: Times New Roman | Verdana | Georgia

Font Size:  

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 51 2 21 5 31 3 42 4 72 5 62 3 53 5 14 5 122 34 3 41 2 31 3 42 3 211

Sample Output

1-1



这道题目WA了很多次,原因就是没用将dist[起点]=0这句话写进去,也就是起点等于终点的情况没用考虑。
下面是AC的代码:

#include<iostream>
#include<cstdio>
#include<string.h>
using namespace std;
int map[201][201];
int dist[201];
int visited[201];
int m,n,sum,flag;
const int inf=9999999999;




void Dijkstra(int st)
{
    memset(visited,0,sizeof(visited));
    visited[st]=1;
    for(int i=1;i<=n;i++)
        dist[i]=map[st][i];
        dist[st]=0;
    for(int i=1;i<n;i++)
    {
        int min=inf;
        int pos;
        for(int j=1;j<=n;j++)
        {
            if(!visited[j]&&dist[j]<min)
            {
                min=dist[j];
                pos=j;
            }
        }
        if(min==inf) break;
        visited[pos]=1;
        for(int j=1;j<=n;j++)
        {
            if(!visited[j]&&dist[j]>map[pos][j]+dist[pos])
                dist[j]=map[pos][j]+dist[pos];
        }
    }


}
int main()
{
   while(~scanf("%d",&n),n)
   {
       int st,end;
       scanf("%d%d",&st,&end);
       int k[201];
       for(int i = 1; i<=n; i++)
            for(int j = 1; j<=n; j++)
                map[i][j] = inf;
       for(int i=1;i<=n;i++)
        cin>>k[i];
        for(int i=1;i<=n;i++)
        {
            if(i-k[i]>0)
            {
                map[i][i-k[i]]=1;
            }
            if(i+k[i]<=n)
            {
                map[i][i+k[i]]=1;
            }
        }
        Dijkstra(st);
        if(dist[end]<inf) cout<<dist[end]<<endl;
        else cout<<"-1"<<endl;
   }
   return 0;
}


0 0
原创粉丝点击