PAT 1087 All Roads Lead to Rome

7 篇文章 0 订阅
6 篇文章 0 订阅

Indeed there are many different tourist routes from our city to Rome. You are supposed to find your clients the route with the least cost while gaining the most happiness.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 positive integers N (2≤N≤200), the number of cities, and K, the total number of routes between pairs of cities; followed by the name of the starting city. The next N−1 lines each gives the name of a city and an integer that represents the happiness one can gain from that city, except the starting city. Then K lines follow, each describes a route between two cities in the format City1 City2 Cost. Here the name of a city is a string of 3 capital English letters, and the destination is always ROM which represents Rome.

Output Specification:

For each test case, we are supposed to find the route with the least cost. If such a route is not unique, the one with the maximum happiness will be recommanded. If such a route is still not unique, then we output the one with the maximum average happiness -- it is guaranteed by the judge that such a solution exists and is unique.

Hence in the first line of output, you must print 4 numbers: the number of different routes with the least cost, the cost, the happiness, and the average happiness (take the integer part only) of the recommanded route. Then in the next line, you are supposed to print the route in the format City1->City2->...->ROM.

Sample Input:

6 7 HZH
ROM 100
PKN 40
GDN 55
PRS 95
BLN 80
ROM GDN 1
BLN ROM 1
HZH PKN 1
PRS ROM 2
BLN HZH 2
PKN GDN 1
HZH PRS 1

Sample Output:

3 3 195 97
HZH->PRS->ROM

有几点需要注意:

  1. 为了避免麻烦,将所有地点映射到1-N上,这样在进行Dij算法时会容易许多。
  2. 保存到达每个节点的最短路径,虽说空间复杂度较高但是也没什么办法。
  3.  注意初始化时fill函数的使用,曾出现过错误。
#include <iostream>
#include <map>
#include <vector>
#include <cstring>
#include <climits>
using namespace std;
int N,K;
map<string,int> map1;
map<int,string> map2;
map<int,int> map3;//保存每个节点的happiness
int graph[205][205];
vector<vector<int>> paths[205];//到达各个节点的路径集合
int Dij(int s,int d){
    bool visited[N+1];
    int dis[N+1];
    memset(visited, false, sizeof(visited));
    fill(dis,dis+N+1,INT_MAX);
    dis[s]=0;
    vector<int> initPath;
    initPath.push_back(s);
    paths[s].push_back(initPath);
    for (int k = 0; k < N; ++k) {
        int index=0;
        int minVal=INT_MAX;
        for (int i = 1; i <=N ; ++i) {
            if(!visited[i]&&dis[i]<minVal){
                index=i;
                minVal=dis[i];
            }
        }
        visited[index]=true;
        if(d==index){
            return dis[index];
        }
        for (int i = 1; i <=N ; ++i) {
            if(!visited[i]&&graph[index][i]!=-1){
                if(dis[index]+graph[index][i]<dis[i]){
                    dis[i]=graph[index][i]+dis[index];
                    paths[i].clear();
                    for(auto vec:paths[index]){
                        vec.push_back(i);
                        paths[i].push_back(vec);
                    }
                }else if(dis[index]+graph[index][i]==dis[i]){
                    for(auto vec:paths[index]){
                        vec.push_back(i);
                        paths[i].push_back(vec);
                    }
                }
            }
        }
    }
    return dis[d];
}
int main() {
    cin>>N>>K;
    string start;
    cin>>start;
    map1[start]=1;
    map2[1]=start;
    int id=2;
    for (int i = 0; i < N - 1; ++i) {
        string name;
        int val;
        cin>>name>>val;
        map1[name]=id;
        map2[id]=name;
        map3[id]=val;
        id++;
    }
    memset(graph,-1, sizeof(graph));
    for (int i = 0; i < K; ++i) {
        string s1,s2;
        int dis;
        cin>>s1>>s2>>dis;
        int id1=map1[s1],id2=map1[s2];
        graph[id1][id2]=dis;
        graph[id2][id1]=dis;
    }
    int s=map1[start];
    int d=map1["ROM"];
    int dis=Dij(s,d);
    vector<vector<int>> vectors=paths[map1["ROM"]];
    vector<int> hs;
    for(const auto& p:vectors){
        int s=0;
        for(auto x:p){
            s+=map3[x];
        }
        hs.push_back(s);
    }
    int maxh=INT_MIN;
    int index=0;
    for (int i = 0; i < hs.size(); ++i) {
        if(hs[i]>maxh){
            maxh=hs[i];
            index=i;
        }
    }
    auto bestp=vectors[index];
    cout<<vectors.size()<<" "<<dis<<" "<<maxh<<" "<<maxh/(bestp.size()-1)<<endl;
    cout<<map2[bestp[0]];
    for (int j = 1; j < bestp.size(); ++j) {
        cout<<"->"<<map2[bestp[j]];
    }
    return 0;
}

 

CSDN海神之光上传的代码均可运行,亲测可用,直接替换数据即可,适合小白; 1、代码压缩包内容 主函数:main.m; 调用函数:其他m文件;无需运行 运行结果效果图; 2、代码运行版本 Matlab 2019b或2023b;若运行有误,根据提示修改;若不会,私信博主; 3、运行操作步骤 步骤一:将所有文件放到Matlab的当前文件夹中; 步骤二:双击打开main.m文件; 步骤三:点击运行,等程序运行完得到结果; 4、仿真咨询 如需其他服务,可私信博主或扫描博客文章底部QQ名片; 4.1 博客或资源的完整代码提供 4.2 期刊或参考文献复现 4.3 Matlab程序定制 4.4 科研合作 功率谱估计: 故障诊断分析: 雷达通信:雷达LFM、MIMO、成像、定位、干扰、检测、信号分析、脉冲压缩 滤波估计:SOC估计 目标定位:WSN定位、滤波跟踪、目标定位 生物电信号:肌电信号EMG、脑电信号EEG、心电信号ECG 通信系统:DOA估计、编码译码、变分模态分解、管道泄漏、滤波器、数字信号处理+传输+分析+去噪(CEEMDAN)、数字信号调制、误码率、信号估计、DTMF、信号检测识别融合、LEACH协议、信号检测、水声通信 1. EMD(经验模态分解,Empirical Mode Decomposition) 2. TVF-EMD(时变滤波的经验模态分解,Time-Varying Filtered Empirical Mode Decomposition) 3. EEMD(集成经验模态分解,Ensemble Empirical Mode Decomposition) 4. VMD(变分模态分解,Variational Mode Decomposition) 5. CEEMDAN(完全自适应噪声集合经验模态分解,Complementary Ensemble Empirical Mode Decomposition with Adaptive Noise) 6. LMD(局部均值分解,Local Mean Decomposition) 7. RLMD(鲁棒局部均值分解, Robust Local Mean Decomposition) 8. ITD(固有时间尺度分解,Intrinsic Time Decomposition) 9. SVMD(逐次变分模态分解,Sequential Variational Mode Decomposition) 10. ICEEMDAN(改进的完全自适应噪声集合经验模态分解,Improved Complementary Ensemble Empirical Mode Decomposition with Adaptive Noise) 11. FMD(特征模式分解,Feature Mode Decomposition) 12. REMD(鲁棒经验模态分解,Robust Empirical Mode Decomposition) 13. SGMD(辛几何模态分解,Spectral-Grouping-based Mode Decomposition) 14. RLMD(鲁棒局部均值分解,Robust Intrinsic Time Decomposition) 15. ESMD(极点对称模态分解, extreme-point symmetric mode decomposition) 16. CEEMD(互补集合经验模态分解,Complementary Ensemble Empirical Mode Decomposition) 17. SSA(奇异谱分析,Singular Spectrum Analysis) 18. SWD(群分解,Swarm Decomposition) 19. RPSEMD(再生相移正弦辅助经验模态分解,Regenerated Phase-shifted Sinusoids assisted Empirical Mode Decomposition) 20. EWT(经验小波变换,Empirical Wavelet Transform) 21. DWT(离散小波变换,Discraete wavelet transform) 22. TDD(时域分解,Time Domain Decomposition) 23. MODWT(最大重叠离散小波变换,Maximal Overlap Discrete Wavelet Transform) 24. MEMD(多元经验模态分解,Multivariate Empirical Mode Decomposition) 25. MVMD(多元变分模态分解,Multivariate Variational Mode Decomposition)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值