• PAT 1087 All Roads Lead to Rome[图论][迪杰斯特拉+dfs]


    1087 All Roads Lead to Rome (30)(30 分)

    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 recommended. 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 recommended 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

     //这个和其他单纯求边权与点权的题不一样,不能半路求出来点权一样,就更改路径了,而是需要考虑整条路径。所以必须是迪杰斯特拉+dfs遍历出最优路径。

    //提交一次代码,

    段错误:您的程序发生段错误,可能是数组越界,堆栈溢出(比如,递归调用层数太多)等情况引起
    case通过率为70.00%

    提交到pat上提示运行时错误和段错误。

    知道出错在什么地方了,dfs里,当s=0时,没有进行return ;导致递归调用层数太多,段错误

    #include <iostream>
    #include <vector>
    #include<map>
    #include<string>
    #include <algorithm>
    using namespace std;
    #define inf 99999
    map<string,int> name2id;
    map<int,string> id2name;
    
    int happy[205];
    int edge[205][205];
    int dist[205];//距离
    vector<int> pre[205];//前驱结点
    int cnt=0;//最短路径个数。
    int happiness=0,mavg=0;//每个节点到
    vector<int> path,temppath;
    int vis[205];
    int d;//目的地
    void dfs(int s){
        temppath.push_back(s);
        if(s==0){
            cnt++;
            int h=0;
            //起始点是什么意思?
            for(int i=0;i<temppath.size()-1;i++){
                h+=happy[temppath[i]];
            }
            if(h>happiness){
                happiness=h;
                mavg=h/(temppath.size()-1);
                path=temppath;
            }else if(h==happiness){
                if(h/(temppath.size()-1)>mavg){
                    mavg=h/(temppath.size()-1);
                    path=temppath;
                }
            }
            temppath.pop_back();
            return ;
        }
        for(int i=0;i<pre[s].size();i++)
            dfs(pre[s][i]);
        temppath.pop_back();
    }
    
    
    int main() {
        int n,m;
        string s;
        cin>>n>>m>>s;
        //初始化
        //fill(edge[0],edge[0]+n*n,inf);不能这样初始化,因为它是201*201的。!!
        fill(edge[0],edge[0]+205*205,inf);
        fill(dist,dist+205,inf);
        //将开始城市设置为0,
        name2id[s]=0;
        id2name[0]=s;
        happy[0]=0;
        string str;
        int hap;
        for(int i=1;i<n;i++){
            cin>>str>>hap;
            name2id[str]=i;
            id2name[i]=str;
            happy[i]= hap;
        }
        string s1,s2;
        int tm;
        for(int i=0;i<m;i++){
            cin>>s1>>s2>>tm;
            edge[name2id[s1]][name2id[s2]]=tm;
            edge[name2id[s2]][name2id[s1]]=tm;
        }
    //    for(int i=0;i<n;i++){
    //        for(int j=0;j<n;j++)
    //            cout<<edge[i][j]<<" ";
    //        cout<<'\n';
    //    }
        d=name2id["ROM"];
        dist[0]=0;
        for(int i=0;i<n;i++){
            int u=-1,minn=inf;
            for(int j=0;j<n;j++)
            if(!vis[j]&&dist[j]<minn){
                u=j;
                minn=dist[j];
            }
            if(u==-1||u==d)break;
            vis[u]=1;
            for(int j=0;j<n;j++){
                if(!vis[j]&&edge[u][j]!=inf){
                    if(dist[j]>dist[u]+edge[u][j]){
                        dist[j]=dist[u]+edge[u][j];
                        pre[j].clear();
                        pre[j].push_back(u);
                    }else if(dist[j]==dist[u]+edge[u][j])
                        pre[j].push_back(u);
                }
            }
        }
    
        dfs(d);
        cout<<cnt<<" "<<dist[d]<<" "<<happiness<<" "<<mavg<<'\n';
        cout<<s;
        for(int i=path.size()-2;i>=0;i--)
            cout<<"->"<<id2name[path[i]];
        return 0;
    }
  • 相关阅读:
    线性回归问题
    聚类:层次聚类
    聚类:(K-means)算法
    神经网络算法
    AutoEventWireup解释
    asp.net中runat="server"的含义
    十步完全理解SQL
    sqlserver中分区函数 partition by的用法
    被忽略却很有用的html标签
    net中使用母版页
  • 原文地址:https://www.cnblogs.com/BlueBlueSea/p/9460015.html
Copyright © 2020-2023  润新知