• 1087. All Roads Lead to Rome (30)(Dijkstra + DFS)


    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

    题目大意:

    有N个城市,M条无向边,从某个给定的起始城市出发,前往名为ROM的城市。每个城市(除了起始城市)都有一个点权(称为幸福值),和边权(每条边所需的花费)。求从起点到ROM所需要的最少花费,并输出其路径。如果路径有多条,给出幸福值最大的那条。如果仍然不唯一,选择路径上的城市平均幸福值最大的那条路径

    分析:

    Dijkstra+DFS。Dijkstra算出所有最短路径的路线pre二维数组,DFS求最大happiness的路径path,并求出路径条数,最大happiness,最大average

    注意:

    average是除去起点的average。城市名称可以用m存储字符串所对应的数字,用m2存储数字对应的字符串
    原文链接:https://blog.csdn.net/liuchuo/article/details/52486213

    题解

    image
    image
    image
    image

    #include <bits/stdc++.h>
    
    using namespace std;
    const int MAXV=510;
    const int INF=1000000000;
    
    int n,m,k,st,G[MAXV][MAXV],weight[MAXV];
    int d[MAXV],numPath,maxW;
    double maxAvg;
    bool vis[MAXV];
    vector<int> pre[MAXV];
    vector<int> tempPath,path;
    map<string,int> cityToIndex;
    map<int,string> indexToCity;
    
    void Dijkstra(int s)
    {
        fill(d,d+MAXV,INF);
        d[s]=0;
        for(int i=0;i<n;i++){
            int u=-1,MIN=INF;
            for(int j=0;j<n;j++){
                if(vis[j]==false&&d[j]<MIN){
                    u=j;
                    MIN=d[j];
                }
            }
            if(u==-1) return;
            vis[u]=true;
            for(int v=0;v<n;v++){
                if(vis[v]==false&&G[u][v]!=INF){
                    if(d[u]+G[u][v]<d[v]){
                        d[v]=d[u]+G[u][v];
                        pre[v].clear();
                        pre[v].push_back(u);
                    }else if(d[v]==d[u]+G[u][v])
                        pre[v].push_back(u);
                }
            }
        }
    }
    void DFS(int v)
    {
        if(v==st){
            tempPath.push_back(v);
            numPath++;
            int tempW=0;
            for(int i=tempPath.size()-2;i>=0;i--){
                int id=tempPath[i];
                tempW+=weight[id];
            }
            double tempAvg=1.0*tempW/(tempPath.size()-1);
            if(tempW>maxW){
                maxW=tempW;
                maxAvg=tempAvg;
                path=tempPath;
            }else if(tempW==maxW&&tempAvg>maxAvg){
                maxAvg=tempAvg;
                path=tempPath;
            }
            tempPath.pop_back();
            return;
        }
        tempPath.push_back(v);
        for(int i=0;i<pre[v].size();i++)
            DFS(pre[v][i]);
        tempPath.pop_back();
    }
    int main()
    {
    #ifdef ONLINE_JUDGE
    #else
        freopen("1.txt", "r", stdin);
    #endif
        string start,city1,city2;
        cin>>n>>m>>start;
        cityToIndex[start]=0;
        indexToCity[0]=start;
        for(int i=1;i<=n-1;i++){
            cin>>city1>>weight[i];
            cityToIndex[city1]=i;
            indexToCity[i]=city1;
        }
        fill(G[0],G[0]+MAXV*MAXV,INF);
        for(int i=0;i<m;i++){
            cin>>city1>>city2;
            int c1=cityToIndex[city1],c2=cityToIndex[city2];
            cin>>G[c1][c2];
            G[c2][c1]=G[c1][c2];
        }
        Dijkstra(0);
        int rom=cityToIndex["ROM"];
        DFS(rom);
        cout<<numPath<<" "<<d[rom]<<" "<<maxW<<" "<<(int)maxAvg<<endl;
        for(int i=path.size()-1;i>=0;i--){
            cout<<indexToCity[path[i]];
            if(i>0) cout<<"->";
        }
        return 0;
    }
    
  • 相关阅读:
    56. Merge Intervals
    Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths CodeForces
    CodeForces 1328E-Tree Queries【LCA】
    2^x mod n = 1 HDU
    CodeForces 1327D
    CodeFoeces 1327E
    AtCoder Beginner Contest 159 E
    牛客14648-序列【莫比乌斯反演+容斥定理】
    完全平方数 HYSBZ
    MongoDB学习笔记(六、MongoDB复制集与分片)
  • 原文地址:https://www.cnblogs.com/moonlight1999/p/15906651.html
Copyright © 2020-2023  润新知