ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

PAT 1030 Travel Plan*

2019-05-02 14:51:01  阅读:254  来源: 互联网

标签:PAT int Travel cost1 cs path 1030 1005 dis


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 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
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int m,s;
int n;
int d;

int dis[1005] = {0};
int cs[1005] = {0};
int cost1[1005][1005] = {0};
int cost2[1005][1005] = {0};
int vis[1005] = {0};
int pre[1005] = {0};



void diji(int x){
    fill(dis,dis+n,INT_MAX);
    fill(cs,cs+n,INT_MAX);
    dis[x] = 0;
    cs[x] = 0;
    for(int i=0;i < n;i++) pre[i] = i;
    while(1){
        int minn = INT_MAX;
        int v = -1;
        for(int i=0;i < n;i++){
            if(!vis[i]&&dis[i] < minn){
                v = i;
                minn = dis[i];
            }
        }
        if(v == -1)break;
        vis[v] = 1;
        for(int i=0;i < n;i++){
            if(!vis[i]&&cost1[i][v]!=INT_MAX){
                if(dis[v]+cost1[v][i] < dis[i]){
                    dis[i] = dis[v]+cost1[v][i];
                    pre[i] = v;
                    cs[i] = cs[v] + cost2[v][i];
                }
                else if(dis[v]+cost1[v][i] == dis[i]&&cs[v]+cost2[v][i] < cs[i]){
                    pre[i] = v;
                    cs[i] = cs[v] + cost2[v][i];
                }
            }
        }
    }
}


vector<int> path;

void dfs(int v){
    if(v==s){
        path.push_back(v);
        return;
    }
    dfs(pre[v]);
    path.push_back(v);
}





int main(){

    cin >> n >> m >> s >> d;
    for(int i=0;i < n;i++){
        for(int j=0;j < n;j++){
            cost1[i][j] = INT_MAX;
            cost1[i][i] = 0;
        }
    }
    for(int i=0;i < m;i++){
        int x,y,a,b;
        cin >> x >> y >> a >> b;
        cost1[x][y] = a;
        cost1[y][x] = a;
        cost2[x][y] = b;
        cost2[y][x] = b;
    }
    diji(s);
    dfs(d);

    for(int i=0;i < path.size();i++){
        cout << path[i] << " ";
    }

    cout << dis[d] << " " << cs[d];

    return 0;
}

——好难。。图论太差了

 

标签:PAT,int,Travel,cost1,cs,path,1030,1005,dis
来源: https://www.cnblogs.com/cunyusup/p/10802312.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有