zoukankan      html  css  js  c++  java
  • 1087 All Roads Lead to Rome (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), 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 N1 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

    dijkstra+dfs

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=1111;
    #define eps 1e-8
    #define  inf  0x3fffffff
    int G[maxn][maxn];
    bool vis[maxn];
    int dist[maxn];
    int weight[maxn];
    vector<int> pre[maxn];
    vector<int> tempath;
    vector<int> path;
    int n,m;
    int st;
    int numpath=0,maxw=-1;
    double maxavg=0;
    string start;
    map<string,int> stoInt;
    map<int,string> itoSt;
    void dijkstra(int index){
        dist[index]=0;
        for(int i=0;i<n;i++){
            int mind=inf,minv=-1;
            for(int i=0;i<n;i++){
                if(vis[i]==false&&dist[i]<mind){
                    mind=dist[i];
                    minv=i;
                }
            }
            int u=minv;
            if(u==-1){
                return ;
            }
            vis[u]=true;
            for(int v=0;v<n;v++){
                if(vis[v]==false&&G[u][v]!=inf&&dist[u]+G[u][v]<dist[v]){
                    dist[v]=dist[u]+G[u][v];
                    pre[v].clear();
                    pre[v].push_back(u);
                }
                else if(dist[u]+G[u][v]==dist[v]){
                    pre[v].push_back(u);
                }
            }
        }
    }
    void dfs(int v){
        if(v==st){
            tempath.push_back(v);
            numpath++;
            int numw=0;
            for(int i=tempath.size()-2;i>=0;i--){
                numw+=weight[tempath[i]];
            }
            double numavg=1.0*numw/(tempath.size()-1);
            if(numw>maxw){
                maxw=numw;
                maxavg=numavg;
                path=tempath;
            }
            else if(numw==maxw&&numavg>maxavg){
                maxavg=numavg;
                path=tempath;
            }
            
            tempath.pop_back();
            return ;
        }
        tempath.push_back(v);
        for(int i=0;i<pre[v].size();i++){
            dfs(pre[v][i]);
        }
        tempath.pop_back();
    }
    int main(){
        fill(G[0],G[0]+maxn*maxn,inf);
        fill(vis,vis+maxn,false);
        fill(dist,dist+maxn,inf);
        cin>>n>>m>>start;
        stoInt[start]=0;
        itoSt[0]=start;
        string name;
        int value;
        for(int i=1;i<n;i++){
            cin>>name>>value;
            stoInt[name]=i;
            itoSt[i]=name;
            weight[i]=value;
        }
        string city1,city2;
        for(int i=0;i<m;i++){
            cin>>city1>>city2>>value;
            int c1=stoInt[city1];
            int c2=stoInt[city2];
            G[c1][c2]=value;
            G[c2][c1]=value;
        }
        dijkstra(0);
        int na=stoInt["ROM"];
        st=stoInt[start];
        dfs(na);
        printf("%d %d %d %.f
    ",numpath,dist[na],maxw,maxavg);
        for(int i=path.size()-1;i>=0;i--){
            if(i>0){
                printf("%s->",itoSt[path[i]].c_str());
            }
            else{
                printf("%s
    ",itoSt[path[i]].c_str());
            }
        }
        return 0;
    }


  • 相关阅读:
    qt中qmake的详解
    教程:从零开始 使用Python进行深度学习!
    win10系统下搭建Python开发环境和TensorFlow深度学习环境(CPU版)
    怎么选择视觉光源颜色
    pycharm安装及设置中文
    新建DataSet和DataTable,并从中提取数据到文本
    网站服务基础面试
    TCP、UDP数据包大小的限制
    TCP的三次握手与四次挥手理解及面试题(很全面)
    zabbix服务深入
  • 原文地址:https://www.cnblogs.com/dreamzj/p/14448058.html
Copyright © 2011-2022 走看看