zoukankan      html  css  js  c++  java
  • 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 N1); M is the number of highways; S and D are the starting and the destination cities, respectively. Then M lines 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
     
     题解:
    直接使用dijkstra找出最短并且花费最少的路径
    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=1010;
    #define inf 0x3fffffff
    int G[maxn][maxn];
    int GP[maxn][maxn];//记录每条路的花费
    int path[maxn];
    bool collected[maxn];
    int dist[maxn];//源点到该点的最短距离
    int price[maxn];//记录源点到该点的花费
    int n,m,s,d;
    void dijkstra(int s){
        dist[s]=0;
        price[s]=0;
        for(int i=0;i<n;i++){
            int v,mind=inf,minv=-1;
            for(int i=0;i<n;i++){
                if(collected[i]==false&&dist[i]<mind){
                    mind=dist[i];
                    minv=i;
                }
            }
            v=minv;
            if(v==-1){
                return ;
            }
            collected[v]=true;
            for(int i=0;i<n;i++){
                if(collected[i]==false&&G[v][i]!=inf){
                    if(dist[v]+G[v][i]<dist[i]){
                        dist[i]=dist[v]+G[v][i];
                        path[i]=v;
                        price[i]=price[v]+GP[v][i];
    //                    printf("%d %d
    ",i,price[i]);
                    }
                    else if(dist[v]+G[v][i]==dist[i]){
                        if(price[v]+GP[v][i]<price[i]){
                            price[i]=GP[v][i]+price[v];
                            path[i]=v;
    //                        printf("%d %d
    ",i,price[i]);
                        }
                    }
                }
            }
        }
    }
    int main(){
        fill(dist,dist+maxn,inf);
        fill(GP[0],GP[0]+maxn*maxn,inf);
        fill(path,path+maxn,-1);
        fill(collected,collected+maxn,false);
        fill(price,price+maxn,inf);
        fill(G[0],G[0]+maxn*maxn,inf);
        scanf("%d %d %d %d",&n,&m,&s,&d);
        for(int i=0;i<m;i++){
            int a,b,c,e;
            scanf("%d %d %d %d",&a,&b,&c,&e);
            G[a][b]=c;
            G[b][a]=c;
            GP[a][b]=e;
            GP[b][a]=e;
        }
        dijkstra(s);
        int k[maxn];
        int i=1;
        k[0]=d;
        int g=d;
        while(path[g]!=-1){
            k[i]=path[g];
            g=path[g];
            i++;
        }
        int sumPath=0;
        for(int j=i-1;j>0;j--){
            sumPath+=G[k[j]][k[j-1]];
        }
        for(int j=i-1;j>=0;j--){
            printf("%d ",k[j]);
        }
        printf("%d ",sumPath);
        printf("%d
    ",price[d]);
        return 0;
    }
     
  • 相关阅读:
    某一字段分组取最大(小)值所在行的数据
    【JVM】01虚拟机内存模型
    POJ 1845 Sumdiv (求某个数的所有正因子的和)
    POJ 2992 Divisors (求因子个数)
    POJ 3696 The Luckiest number (欧拉函数,好题)
    POJ 1811 Prime Test (Pollard rho 大整数分解)
    POJ 2429 GCD & LCM Inverse (Pollard rho整数分解+dfs枚举)
    POJ 1305 Fermat vs. Pythagoras (毕达哥拉斯三元组)
    POJ 2142 The Balance (解不定方程,找最小值)
    POJ 1006 Biorhythms (中国剩余定理)
  • 原文地址:https://www.cnblogs.com/dreamzj/p/14359845.html
Copyright © 2011-2022 走看看