• 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<=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
    
     题目大意:找到花费最少,幸福值最高的路径
    思路:用dijkstra算法找到花费最少的路径, 然后再用dfs找幸福值最高的路径
     1 #include<iostream>
     2 #include<vector>
     3 #include<map>
     4 using namespace std;
     5 const int inf=99999999;
     6 int cost[205][205], weight[205], w[205];
     7 vector<int> pre[205], temp, path, vis(205, false);
     8 map<string, int> index;
     9 map<int, string> index1;
    10 int maxavg=-1, maxx=-1, cnt=0, money=0;
    11 void dfs(int v){
    12   temp.push_back(v);
    13   if(v==0){
    14     cnt++;
    15     int tempsum=0, avg=0;
    16     for(int j=0; j<temp.size()-1; j++) tempsum += weight[temp[j]];
    17     avg = tempsum/(temp.size()-1);
    18     if(tempsum>maxx){
    19       path=temp;
    20       maxx = tempsum;
    21       maxavg = avg;
    22     }else if(tempsum==maxx&&avg>maxavg){
    23       path=temp;
    24       maxavg = avg;
    25     }
    26     temp.pop_back();
    27     return;
    28   }
    29   for(int i=0; i<pre[v].size(); i++) dfs(pre[v][i]);
    30   temp.pop_back();
    31 }
    32 int main(){
    33   int n, k, i, happiness, c;
    34   string s, s1, s2;
    35   fill(cost[0], cost[0]+205*205, inf);
    36   fill(w, w+205, inf);
    37   cin>>n>>k>>s;
    38   index[s]=0;  index1[0]=s;
    39   for(i=1; i<n; i++){
    40     cin>>s1>>happiness;
    41     weight[i]=happiness;
    42     index[s1]=i;
    43     index1[i]=s1;
    44   }
    45   for(i=0; i<k; i++){
    46     cin>>s1>>s2>>c;
    47     int ia=index[s1], ib=index[s2];
    48     cost[ia][ib]=cost[ib][ia]=c;
    49   }
    50   pre[0].push_back(0);   w[0]=0;
    51   for(i=0; i<n; i++){
    52     int u=-1, minn=inf;
    53     for(int j=0; j<n; j++){
    54       if(!vis[j] && w[j]<minn){
    55         minn=w[j];
    56         u=j;
    57       }
    58     }
    59     if(u==-1) break;
    60     vis[u] = true;
    61     for(int v=0; v<n; v++){
    62       if(!vis[v] && cost[u][v]!=inf){
    63         if(w[v]>w[u]+cost[u][v]){
    64           w[v]=w[u]+cost[u][v];
    65           pre[v].clear();
    66           pre[v].push_back(u);
    67         }else if(w[v]==w[u]+cost[u][v]){
    68           pre[v].push_back(u);
    69         }
    70       }
    71     }
    72   }
    73   dfs(index["ROM"]);
    74   for(i=path.size()-1; i>0; i--) money += cost[path[i]][path[i-1]];
    75   cout<<cnt<<" "<<money<<" "<<maxx<<" "<<maxavg<<endl;
    76   for(i=path.size()-1; i>=0; i--){
    77     if(i==path.size()-1) cout<<index1[path[i]];
    78     else cout<<"->"<<index1[path[i]];
    79   }
    80   return 0;
    81 }
    有疑惑或者更好的解决方法的朋友,可以联系我,大家一起探讨。qq:1546431565
  • 相关阅读:
    使用 virtualenv/venv 和 pip 管理虚拟环境
    Python:virtualenv 和 venv 的区别
    Python | Get unique values from a list
    Python ORM 框架 Peewee 知识点
    python之配置日志的几种方式
    python 中的 None,知识点
    Gunicorn 知识点
    gunicorn部署flask的log处理
    Python 逻辑运算符(and、or、not)、成员运算符(in、not in)、身份运算符(is、is not)
    Python 中没有 null,用 None 表示
  • 原文地址:https://www.cnblogs.com/mr-stn/p/9180698.html
Copyright © 2020-2023  润新知