zoukankan      html  css  js  c++  java
  • 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
    

    dijkstra算法,需要记录路径,到某点最小cost,最大happiness,经过最小地点数。
    代码:
    #include <stdio.h>
    #include <string.h>
    #define inf 0x3f3f3f3f
    
    ///终点是ROM 城市名是三个大写字母
    int n,k,d,num,destination;
    int pos[20000],happiness[205],sumhappiness[205],dis[205],mp[205][205],visited[205],placenum[205],path[205],pathnum[205];
    char loc1[4],loc2[4],s[205][4];
    int change(const char *t) {
        return (t[2] - 'A') + (t[1] - 'A') * 26 + (t[0] - 'A') * 26 * 26;
    }
    void getpath(int t) {
        if(t)getpath(path[t]);
        if(t)printf("->");
        printf("%s",s[t]);
    }
    int main() {
        scanf("%d%d %s",&n,&k,loc1);
        int temp = change(loc1);///地点名对应int值
        pos[temp] = num ++;///int值对应位置 每安排一个位置,num就+1
        strcpy(s[num - 1],loc1);///同时记录对应位置 的地点名
        for(int i = 1;i < n;i ++) {
            scanf("%s %d",loc1,&d);
            temp = change(loc1);
            pos[temp] = num ++;
            happiness[num - 1] = d;
            strcpy(s[num - 1],loc1);
        }
        destination = pos[change("ROM")];
        for(int i = 0;i < num;i ++) {///初始化
            for(int j = 0;j < num;j ++) {
                mp[i][j] = inf;
            }
            dis[i] = inf;
            path[i] = -1;
        }
        dis[0] = 0;
        pathnum[0] = 1;///路径数初始为1
        for(int i = 0;i < k;i ++) {
            scanf("%s %s %d",loc1,loc2,&d);
            int a = pos[change(loc1)],b = pos[change(loc2)];
            mp[a][b] = mp[b][a] = d;
        }
        while(1) {///dijkstra
            int t = -1,mi = inf;
            for(int i = 0;i < num;i ++) {
                if(visited[i])continue;
                if(dis[i] < mi)mi = dis[i],t = i;
            }
            if(t == -1)break;
            visited[t] = 1;
            for(int i = 0;i < num;i ++) {
                if(visited[i] || mp[t][i] == inf)continue;
                if(dis[i] > dis[t] + mp[t][i]) {
                    dis[i] = dis[t] + mp[t][i];
                    sumhappiness[i] = sumhappiness[t] + happiness[i];
                    placenum[i] = placenum[t] + 1;
                    path[i] = t;
                    pathnum[i] = pathnum[t];
                }
                else if(dis[i] == dis[t] + mp[t][i]) {
                    pathnum[i] += pathnum[t];
                    if(sumhappiness[i] < sumhappiness[t] + happiness[i]) {
                        sumhappiness[i] = sumhappiness[t] + happiness[i];
                        path[i] = t;
                        placenum[i] = placenum[t] + 1;
                    }
                    else if(sumhappiness[i] == sumhappiness[t] + happiness[i] && placenum[i] > placenum[t] + 1) {
                        path[i] = t;
                        placenum[i] = placenum[t] + 1;
                    }
                }
            }
        }
        printf("%d %d %d %d
    ",pathnum[destination],dis[destination],sumhappiness[destination],sumhappiness[destination] / placenum[destination]);
        getpath(destination);
    }
  • 相关阅读:
    开源APM应用性能管理工具调研
    Inside ARC — to see the code inserted by the compiler
    报表应用系统中怎样正确使用图表功能
    创建cifs系统案例之“实现将Windows磁盘共享至Linux”
    Eclipse快捷键 10个最有用的快捷键
    如何生成KeyStore
    android中调用系统的发送短信、发送邮件、打电话功能
    android自带theme
    Android 报错:Conversion to Dalvik format failed: Unable to execute dex: java.nio.BufferOverflowException. Check the Eclipse log for stack trace.
    Android oncreate onupgrade什么时候被调用
  • 原文地址:https://www.cnblogs.com/8023spz/p/9298920.html
Copyright © 2011-2022 走看看