zoukankan      html  css  js  c++  java
  • A1087. All Roads Lead to Rome

    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

      1 #include<cstdio>
      2 #include<iostream>
      3 #include<vector>
      4 #include<algorithm>
      5 #include<string>
      6 #include<map>
      7 using namespace std;
      8 int G[201][201], happy[201];
      9 int visit[201], dst[201];
     10 vector<int> pre[201];
     11 const int INF = 100000000;
     12 map<string, int> str2num;
     13 map<int, string> num2str;
     14 int N, K;
     15 string start, fina = "ROM";
     16 int start_, fina_, cnt = 0;
     17 void dijkstra(int s){
     18     fill(visit, visit + 201, 0);
     19     fill(dst, dst + 201, INF);
     20     dst[s] = 0;
     21     for(int i = 0; i < N; i++){
     22         int u = -1, minLen = INF;
     23         for(int j = 0; j < N; j++){
     24             if(visit[j] == 0 && dst[j] < minLen){
     25                 minLen = dst[j];
     26                 u = j;
     27             }
     28         }
     29         if(u == -1)
     30             return;
     31         visit[u] = 1;
     32         for(int j = 0; j < N; j++){
     33             if(visit[j] == 0 && G[u][j] != INF){
     34                 if(G[u][j] + dst[u] < dst[j]){
     35                     dst[j] = G[u][j] + dst[u];
     36                     pre[j].clear();
     37                     pre[j].push_back(u);
     38                 }else if(G[u][j] + dst[u] == dst[j]){
     39                     pre[j].push_back(u);
     40                 }
     41             }
     42         }
     43     }
     44 }
     45 
     46 vector<int> path, ans;
     47 int pathNum = 0, maxHapp = -1, maxNum = -1;
     48 void DFS(int vt){
     49     path.push_back(vt);
     50     if(vt == start_){
     51         pathNum++;
     52         int sumTemp = 0;
     53         for(int i = path.size() - 2; i >= 0; i--){
     54             sumTemp += happy[path[i]];
     55         }
     56         if(sumTemp > maxHapp){
     57             maxNum = path.size() - 1;
     58             maxHapp = sumTemp;
     59             ans = path;
     60         }else if(sumTemp == maxHapp && path.size() < maxNum){
     61             maxNum = path.size() - 1;
     62             maxHapp = sumTemp;
     63             ans = path;
     64         }
     65         path.pop_back();
     66         return;
     67     }
     68     int Len = pre[vt].size();
     69     for(int i = 0; i < Len; i++){
     70         DFS(pre[vt][i]);
     71     }
     72     path.pop_back();
     73     return;
     74 }
     75 int strNum(string ss){
     76     if(str2num.count(ss) == 0){
     77         str2num[ss] = cnt;
     78         num2str[cnt] = ss;
     79         return cnt++;
     80     }else{
     81         return str2num[ss];
     82     }
     83 }
     84 int main(){
     85     cin >> N >> K >> start;
     86     string temps, temps2;
     87     fill(G[0], G[0] + 201*201, INF);
     88     int tempL;
     89     for(int i = 0; i < N - 1; i++){
     90         cin >> temps >> tempL;
     91         happy[strNum(temps)] = tempL; 
     92     }
     93     for(int i = 0; i < K; i++){
     94         cin >> temps >> temps2 >> tempL;
     95         int u1 = strNum(temps), u2 = strNum(temps2);
     96         G[u1][u2] = G[u2][u1] = tempL;
     97     }
     98     start_ = strNum(start);
     99     fina_ = strNum(fina);
    100     dijkstra(start_);
    101     int costSum = dst[fina_];
    102     DFS(fina_);
    103     printf("%d %d %d %d
    %s", pathNum, costSum, maxHapp, maxHapp / maxNum, num2str[start_].c_str());
    104     for(int i = ans.size() - 2; i >= 0; i--){
    105         printf("->%s", num2str[ans[i]].c_str());
    106     }
    107     cin >> N;
    108     return 0;
    109 }
    View Code

    总结:

    1、题意:求出最短路,如果有多条,则求出一路上点权之和(开始节点不计入)最大的一条,如果还有多条,则求出点权之和的均值最大的一条(依然不计入开始节点)。

    2、输入的城市名字通过map转化为整数节点,使用一个全局变量cnt分配节点编号。

    3、pre[K]表示K节点的所有前驱节点。整个pre数组需要从目的地节点回溯到开始节点。

  • 相关阅读:
    转Oracle、MySql、SQLServer 数据分页查询
    转 DevExpress-ASPxPageControl 动态添加 TabPage 内容
    淘淘实惠多www.taohuiduo.com-专注独家折扣、1折特卖、9块9包邮、全场包邮
    "命名空间"system.web"中不存在类型或命名空间名称security"错误解决方法
    转摘 Eclipse智能提示及快捷键
    转(C#)Winform中MD5加密
    ComboBoxEdit数据绑定
    格式化日期和时间
    时间格式转换
    如何安装ESXi的补丁
  • 原文地址:https://www.cnblogs.com/zhuqiwei-blog/p/8563330.html
Copyright © 2011-2022 走看看