zoukankan      html  css  js  c++  java
  • PAT 甲级 1030 Travel Plan

    https://pintia.cn/problem-sets/994805342720868352/problems/994805464397627392

    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 4

    代码:

    #include <bits/stdc++.h>
    using namespace std;
    
    #define inf 0x3f3f3f3f
    
    int N, M, S, D;
    int mp[550][550], cost[550][550];
    int dis1[550],dis2[550],dis3[550];
    int vis[550];
    int pre[550];
    
    int MinStep, out = INT_MAX;
    
    void dijkstra(int S, int dis[]) {
        dis[S] = 0;
        memset(vis, 0, sizeof(vis));
        int temp = S;
    
        for(int i = 0; i < N; i ++) {
            int minn = inf;
            for(int j = 0; j < N; j ++) {
                if(dis[j] < minn && vis[j] == 0) {
                    minn = dis[j];
                    temp = j;
                }
            }
            vis[temp] = 1;
            for(int k = 0; k < N; k ++)
                if(vis[k] == 0 && mp[temp][k] != inf) {
                    if(dis[k] > mp[temp][k] + dis[temp])
                        dis[k] = mp[temp][k] + dis[temp];
            }
        }
    }
    
    void dijkstra(int S) {
    
        dis3[S] = 0;
        memset(vis, 0, sizeof(vis));
        int temp = S;
    
        for(int i = 0; i < N; i ++) {
            int minn = inf;
            for(int j = 0; j < N; j ++) {
                if(dis3[j] < minn && vis[j] == 0) {
                    minn = dis3[j];
                    temp = j;
                }
            }
            vis[temp] = 1;
            for(int k = 0; k < N; k ++)
                if(mp[temp][k] + dis1[temp] + dis2[k] == MinStep)
                    if(vis[k] == 0 && cost[temp][k] != inf)
                        if(dis3[k] > cost[temp][k] + dis3[temp]){
                            dis3[k] = cost[temp][k] + dis3[temp];
                            pre[k] = temp;
                        }
        }
    }
    
    
    void output(int d){
        if(d==-1) return ;
        output(pre[d]);
        printf("%d ", d);
    }
    
    int main() {
    
        memset(pre,-1, sizeof(pre));
        memset(vis, 0, sizeof(vis));
        memset(cost,inf, sizeof(cost));
    
        memset(dis1, inf, sizeof(dis1));
        memset(dis2, inf, sizeof(dis2));
        memset(dis3, inf, sizeof(dis3));
    
        memset(mp, inf, sizeof(mp));
    
        scanf("%d%d%d%d", &N, &M, &S, &D);
        for(int i = 0; i < M; i ++) {
            int st, en, dist, val;
            scanf("%d%d%d%d", &st, &en, &dist, &val);
            mp[st][en] = mp[en][st] = min(dist, mp[en][st]);
            cost[st][en] = cost[en][st] = min(val, cost[st][en]);
        }
    
        dijkstra(S, dis1);
        dijkstra(D, dis2);
    
        MinStep = dis1[D];
    
        dijkstra(S);
    
        output(D);
        printf("%d %d
    ", dis1[D], dis3[D]);
        return 0;
    }
    

      两遍 dijkstra 一上午经历了无数遍点开题目又退出 枯了 睡一会清醒清醒再来写吧

  • 相关阅读:
    MongoDB mongod.log "connection refused because too many open connections" 处理方法
    12C RAC 故障分析
    Linux(Redhat 7.6)安装PG(Postgresql 9.6.0)
    OGG异构平台安装部署 MySQL->Oracle
    Redhat 7安装Oracle 11.2.0.4 RAC 数据库软件中报错:Error in invoking target 'agent nmhs' of makefile
    Redhat 7.6安装11G RAC GI时遇到此类报错
    Oracle 19.3 RAC on Redhat 7.6 安装最佳实践
    RAC环境中某数据文件(非system表空间)创建在本地,不停机迁移到ASM磁盘中
    python 多线程
    go博客大全
  • 原文地址:https://www.cnblogs.com/zlrrrr/p/10369432.html
Copyright © 2011-2022 走看看