zoukankan      html  css  js  c++  java
  • 1030 Travel Plan (30)(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 (<=500) 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 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 <iostream>
    #include <cstdio>
    #include <cstring>
    #include <vector>
    #include <algorithm>
    #include <set>
    #include <map>
    #define inf 0x3f3f3f3f
    #define MAX 501
    using namespace std;
    int n,m,s,d,tdis,tcost,a,b;
    int edis[MAX][MAX];
    int ecost[MAX][MAX];
    int dis[MAX];
    int cost[MAX];
    int vis[MAX];
    int path[MAX];
    void print(int k) {
        if(k == s)return;
        print(path[k]);
        printf(" %d",k);
    }
    int main() {
        scanf("%d%d%d%d",&n,&m,&s,&d);
        for(int i = 0;i < n;i ++) {
            for(int j = 0;j < n;j ++)
                edis[i][j] = inf;
            edis[i][i] = 0;
            dis[i] = inf;
        }
        for(int i = 0;i < m;i ++) {
            scanf("%d%d%d%d",&a,&b,&tdis,&tcost);
            edis[a][b] = edis[b][a] = tdis;
            ecost[a][b] = ecost[b][a] = tcost;
        }
        dis[s] = 0;
        int t,mi;
        while(1) {
            t = -1;
            mi = inf;
            for(int i = 0;i < n;i ++) {
                if(!vis[i] && dis[i] < mi)mi = dis[i],t = i;
            }
            if(t == -1)break;
            vis[t] = 1;
            for(int i = 0;i < n;i ++) {
                if(vis[i] || edis[t][i] == inf)continue;
                if(mi + edis[t][i] < dis[i]) {
                    dis[i] = mi + edis[t][i];
                    cost[i] = cost[t] + ecost[t][i];
                    path[i] = t;
                }
                else if(mi + edis[t][i] == dis[i] && cost[t] + ecost[t][i] < cost[i]) {
                    cost[i] = cost[t] + ecost[t][i];
                    path[i] = t;
                }
            }
        }
        printf("%d",s);
        print(d);
        printf(" %d %d",dis[d],cost[d]);
    }
    View Code
  • 相关阅读:
    2021年通达信指标公式大全,值得收藏!
    网络兼职?威客?为什么我会觉得网络兼职,威客会是人生中应该具备的一种能力!
    SeMusic 音乐网站源代码,PHP音乐系统,人人都是站长人人都可副业创业!
    JavaScript 查看图片,带缩放放大效果
    JS (javascript) 计算循环当前时间,javascript 时间钟表
    关键词被冷藏?关键词没排名?任务网站长们该何去何从?
    关键词任务网被K,对于任务网该何去何从?我认为任务网存活只有一条出路!
    C3属性的轮播图(持续更新)
    自己写的文字轮播(简陋版)
    带锁的3D切割轮播图
  • 原文地址:https://www.cnblogs.com/8023spz/p/9131347.html
Copyright © 2011-2022 走看看