zoukankan      html  css  js  c++  java
  • PAT 甲级 1030 Travel Plan (30 分)(dijstra,较简单,但要注意是从0到n-1)

                                        1030 Travel Plan (30 分)

     

    A traveler's map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a program to help a traveler to decide the shortest path between his/her starting city and the destination. If such a shortest path is not unique, you are supposed to output the one with the minimum cost, which is guaranteed to be unique.

    Input Specification:

    Each input file contains one test case. Each case starts with a line containing 4 positive integers N, M, S, and D, where N (≤) is the number of cities (and hence the cities are numbered from 0 to N−1); M is the number of highways; S and D are the starting and the destination cities, respectively. Then Mlines follow, each provides the information of a highway, in the format:

    City1 City2 Distance Cost
    

    where the numbers are all integers no more than 500, and are separated by a space.

    Output Specification:

    For each test case, print in one line the cities along the shortest path from the starting point to the destination, followed by the total distance and the total cost of the path. The numbers must be separated by a space and there must be no extra space at the end of output.

    Sample Input:

    4 5 0 3
    0 1 1 20
    1 3 2 30
    0 3 4 10
    0 2 2 20
    2 3 1 20
    

    Sample Output:

    0 2 3 3 40

    题解:

     第一行是四个数字,n,m,s,d,分别代表有n个城市,有m条路,起点城市s和终点城市d;

    后面跟着m行数据,代表道路的情况,每一行四个数字,City1 City2 Distance Cost,让你求出来由起点到终点的最短的路径,如果距离相等时选择花费最小的道路,最后输出道路和最短的距离和花费;

    这道题也是用dijkstra算法来进行求解,记得在最短路径的判断中加入对于花费的判断(即,当距离一样时,选择花费更小的路径);

    对于路径的存储,用一个数组,每一次路径变化时,只需要存下来这个城市是由哪个城市来的,最后一个栈从终点城市回溯输出即可。

    第一次没过:

    没注意到城市是从0到n-1,我太粗心直接写成了1到n编号,后来才发现。

    这道题没有重边的情况,但是要注意将来会不会遇到重边的特殊情况。

    AC代码: 

    #include<bits/stdc++.h>
    using namespace std;
    #define inf 0x3f3f3f3f
    int n,m,s,dis;
    int e[505][505];//距离 
    int d[505];//距离 
    int dc[505];//花费 
    int ec[505][505];//花费 
    bool v[505];//标记有没有遍历过 
    int p[505];//记录路径 
    void dijstra(int s){
        memset(v,0,sizeof(v));
        for(int i=1;i<=n;i++){
            d[i]=e[s][i];
            dc[i]=ec[s][i];
            p[i]=s;
        }
        v[s]=1;
        for(int i=1;i<=n;i++){
            int k=-1;
            int mi=inf;
            for(int j=1;j<=n;j++){
                if(mi>d[j]&&!v[j]){
                    k=j;
                    mi=d[j];
                }
            }
            if(k==-1){
                break;
            }
            v[k]=1;
            for(int j=1;j<=n;j++){
                if(d[j]>d[k]+e[k][j]){
                    d[j]=d[k]+e[k][j];
                    dc[j]=dc[k]+ec[k][j];
                    p[j]=k;
                }else if(d[j]==d[k]+e[k][j]){
                    if(dc[j]>dc[k]+ec[k][j]){
                        dc[j]=dc[k]+ec[k][j];
                        p[j]=k;
                    }
                    
                }
            }
        }    
    }
    int main()
    {
        cin>>n>>m>>s>>dis;
        s++;//我是以1-n编号 
        dis++;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                if(i==j){
                    e[i][j]=ec[i][j]=0;
                }else{
                    e[i][j]=ec[i][j]=inf;
                }
            }
        }
        for(int i=1;i<=m;i++){
            int u,v,dd,cc;
            cin>>u>>v>>dd>>cc;
            u++;//我是以1-n编号 
            v++;
            e[u][v]=e[v][u]=dd;
            ec[u][v]=ec[v][u]=cc;
        }
        dijstra(s);
        //输出 
        stack<int>sta;
        while(!sta.empty()) sta.pop();
        int pair=dis;
        while(pair!=s){
            sta.push(pair);
            pair=p[pair];
        }
        cout<<s-1<<" ";
        while(!sta.empty()){
            cout<<sta.top()-1<<" ";
            sta.pop();
        }
        cout<<d[dis]<<" "<<dc[dis];
        return 0;
     } 
  • 相关阅读:
    Prometheus 简介
    Cassandra spring data 试用
    nginx fastcgi 优化
    mysql 用户管理
    js之iframe子页面与父页面通信
    ORACLE中SID和SERVICE_NAME的区别
    报“ Got minus one from a read call”的错误
    linux下重启oracle服务:监听器和实例
    25种提高网页加载速度的方法和技巧
    如何让你的网页加载时间降低到 1s 内
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/13270673.html
Copyright © 2011-2022 走看看