hdu Choose the best route(最短---Dijkstra)

4396 ワード

Choose the best route
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4040    Accepted Submission(s): 1260
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=Then follow m lines ,each line contains three integers p , q , t (0Then a line with an integer w(0 
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

 
Author
dandelion
 
Source
2009浙江大学のコンピュータは試験して再び試験します
 
Recommend
lcy
この問題は言うべき2点だ.
1.図論では重辺問題に注意することが必要であり、有向無向も同様に重要である. from station p to station q
はっきり言って方向図がある
2.最初は複雑さを感じずにそのままこの問題を書きましたが、タイムアウトして地味なdijkstraが原因だと思っていました.
解題報告を見に行って、それからやっとスーパーソースに参加するこのような巧みな思想を知っていて、悲劇的に思考の鍛錬の機会を逃しました!
そしてこの問題が複数の始点と複数の終点に広がって、任意に始点の終点を結ぶ最短ルートを求めれば、それはただ
スーパーポイントをもう一つ追加すればいいです
#include <stdio.h>
#include <string.h>

int map[1005][1005];
int n;

int Dijkstra(int s,int e){
    bool done[1005];
    int d[1005];
    memset(done,0,sizeof(done));
    for(int i = 0;i <= n;i++)
        d[i] = (i == s ? 0 : 1000000);
    for(int i = 0;i <= n;i++){//    n+1   
        int minx,minn = 1000000;
        for(int j = 0;j <= n;j++)//   d[]    
            if(!done[j] && d[j] < minn){
                minn = d[j];
                minx = j;
            }
        done[minx] = 1;//       
        if(minx == e)
            return d[minx];
        for(int j = 0;j <= n;j++){//      d[]
            if(!done[j] && d[minx] + map[minx][j] < d[j]){
                d[j] = d[minx] + map[minx][j];
            }
        }
    }
    return -1;//            ,  -1
}

int main(){
    int m,s;
    int i,j,k;
    int p,q,t;
    int w,ww,ans;

    while(scanf("%d%d%d",&n,&m,&s) != EOF){
        ans = 100000000;
        for(i = 0;i < 1005;i++)
            for(j = 0;j < 1005;j++){
                if(i == j)
                    map[i][j] = 0;
                else
                    map[i][j] = 1000000;
            }
        while(m--){
            scanf("%d%d%d",&p,&q,&t);
            if(t < map[p][q])
                map[p][q] = t;
        }
        scanf("%d",&w);
        while(w--){
            scanf("%d",&ww);
            map[0][ww] = 0;
        }
        ans = Dijkstra(0,s);//    ,      0
        if(ans == -1)
            printf("-1
"); else printf("%d
",ans); } return 0; }