PAT(甲级)1030 Travel Plan(带两个权值的最短路)

来源:互联网 发布:数据上报出现异常 编辑:程序博客网 时间:2024/06/06 02:50

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就行了,不过这里有两个权值,即距离相同时优先输出花费最小的那个路,故采用邻接表存图,然后再用pre[]记录前趋点就可以输出路径和花费了

完整代码如下:

#include<iostream>#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>#include<cstdlib>#include<vector>#include<set>#include<queue>using namespace std;const int MAX = 510;const int INF = 99999999;pair<int,int> dis[MAX];//first代表距离,second代表花费bool used[MAX];int pre[MAX];struct Edge{    int to,d,cost;};vector<Edge> G[MAX];int n,m,b,o;void Dijkstra(int a){    for(int i=0;i<n;i++){        dis[i].first = dis[i].second = INF;//这里是更新为INF,我竟然刚开始把他更新为0真是囧。        used[i] = false;        pre[i] = -1;    }    dis[a].first = dis[a].second = 0;//更新起点    while(true){        int v = -1;        for(int u=0;u<n;u++){            if(used[u])                continue;        /*优先选择距离小的,如果距离相等优先选择花费小的*/            if(v == -1 || ((dis[u].first < dis[v].first) || (dis[u].first == dis[v].first && dis[u].second < dis[v].second)))                v = u;        }        if(v == -1) break;//如果这个点没有更新就证明所有点都已经使用过了,就跳出。        used[v] = true;        for(vector<Edge>::iterator it = G[v].begin();it != G[v].end();++it){//便利这个点的所有边,这里要用vector的迭代器            if(dis[it->to].first > dis[v].first + it->d){                pre[it->to] = v;//如果发生距离更新,就更新前趋点。                dis[it->to].first = dis[v].first + it->d;//这里距离和花费都要更新.                dis[it->to].second = dis[v].second + it->cost;            }            else if(dis[it->to].first == dis[v].first + it->d && dis[it->to].second > dis[v].second + it->cost){                pre[it->to] = v;                dis[it->to].second = dis[v].second + it->cost;            }        }    }}int main(void){    cin >> n >> m >> b >> o;    int x,y;    Edge e;    for(int i=0;i<m;i++){        cin >> x >> y >> e.d >> e.cost;        e.to = y;        G[x].push_back(e);        e.to = x;        G[y].push_back(e);    }    Dijkstra(b);    int step[MAX],top = 0;    step[++top] = o;    int k = pre[o];    while(k != -1){        step[++top] = k;        k = pre[k];    }    for(int i=top;i>=1;i--){//输出路径        cout << step[i] << " ";    }    cout << dis[o].first << " " << dis[o].second;    return 0;}
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 如果在厂里饭卡丢了怎么办 工作上做错了事想辞职怎么办 结婚辞职怀孕感觉做错了怎么办 换新手机了钉钉怎么办 公众号申请链接无效怎么办 指纹打卡机时间调错已打卡怎么办 智慧云平台先锋讲座打不开怎么办 下雨穿套裙工装好冷怎么办 国家债务违约不要国际融资怎么办 欠钱的确还不起怎么办 生完孩孑后手发账握不死手怎么办 工作调动没啥消息怎么办 裁剪刀老是推歪怎么办 衣服上粘胶水了怎么办 衣服钻掉了有胶怎么办 衣服上贴纸掉了怎么办 裤子沾上502胶水怎么办 衣服上沾泡沫胶怎么办 衣服上面滴上502怎么办 502胶水弄衣服上怎么办 裤子上粘了胶怎么办 胶水滴在衣服上怎么办 衣服上有502胶水怎么办 衣服上面粘了胶怎么办 衣服上的胶干了怎么办 凌晨4点到火车站怎么办 运管罚款没钱交怎么办 郑州地铁票没买怎么办 遇到吸毒者拦路威胁要钱怎么办 开车遇见拦路要钱的怎么办 高速上有人拦车怎么办 马路上有人拦车怎么办 苹果手机下截软件要钱怎么办 孩子在学校问同学要钱怎么办 在学校被同学要钱怎么办 把人家店砸了要怎么办 外汇出金不到账怎么办 把罚款单弄丢了怎么办 在12306买不到下铺怎么办有 地铁票买反了怎么办 香港买错特惠票怎么办