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

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 (0 Then 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


这道题值得一提的两点
1.在图论中注意重边问题是必须的,有向无向也是同等重要的,如这道题 from station p to station q
说的就很清楚是有向图

2.开始没算复杂度就直接写了这道题,后来超时以为是写的朴素的dijkstra导致的,然后想也没想就
去看解题报告了,然后才知道加入超级源点这么巧妙的思想,悲剧的错过了一次思维锻炼的机会!
然后这道题如果拓展成给多个起点和多个终点,求一条任意连接起点终点的最短路,那应该就只需要
再加入一个超级汇点就可以了

#include 
#include 

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\n");
        else
            printf("%d\n",ans);
    }
    return 0;
}





你可能感兴趣的:(图论)