zoukankan      html  css  js  c++  java
  • PAT 甲级 1087 All Roads Lead to Rome

    https://pintia.cn/problem-sets/994805342720868352/problems/994805379664297984

    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

    代码:

    #include <bits/stdc++.h>
    using namespace std;
    
    #define inf 0x3f3f3f3f
    int N, K;
    string st;
    map<string, int> mp, cost;
    map<int, string> pos;
    int maze[220][220], vis[220], dis[220], see[220];
    int MinStep = 0, cnt = 0;
    vector<vector<int> > ans;
    vector<int> v;
    
    struct Node{
        double ave;
        int all = 0;
        int num;
        int n;
    }node[100010];
    
    bool cmp(const Node &a, const Node &b) {
        if(a.all != b.all) return a.all > b.all;
        return a.ave > b.ave;
    }
    
    void dfs(int act, int step) {
        if(step > MinStep) return;
    
        if(act == mp["ROM"]) {
            cnt ++;
            ans.push_back(v);
            return;
        }
    
        for(int i = 1; i <= N; i ++) {
            if(maze[act][i] != inf && dis[act] + maze[act][i] == dis[i] && see[i] == 0) {
                v.push_back(i);
                see[i] = 1;
                dfs(i, step + maze[act][i]);
                v.pop_back();
                see[i] = 0;
            }
        }
    }
    
    void dijkstra(int act) {
        dis[act] = 0;
        int temp = act;
    
        for(int i = 1; i <= N; i ++) {
            int minn = inf;
            for(int j = 1; j <= N; j ++) {
                if(dis[j] < minn && vis[j] == 0) {
                    minn = dis[j];
                    temp = j;
                }
            }
            vis[temp] = 1;
            for(int k = 1; k <= N; k ++) {
                if(vis[k] == 0)
                    dis[k] = min(dis[k], maze[temp][k] + dis[temp]);
            }
        }
    }
    
    int main() {
        memset(dis, inf, sizeof(dis));
        memset(vis, 0, sizeof(vis));
        memset(maze, inf, sizeof(maze));
        memset(see, 0, sizeof(see));
        scanf("%d%d", &N, &K);
        cin >> st;
        mp[st] = 1;
        pos[1] = st;
        for(int i = 0; i < N - 1; i ++) {
            string city; int val;
            cin >> city >> val;
            mp[city] = i + 2;
            pos[i + 2] = city;
            cost[city] = val;
        }
        for(int i = 0; i < K; i ++) {
            string stt, en; int cos;
            cin >> stt >> en >> cos;
            maze[mp[stt]][mp[en]] = maze[mp[en]][mp[stt]] = cos;
        }
        dijkstra(1);
        MinStep = dis[mp["ROM"]];
    
        dfs(1, 0);
    
        for(int i = 0; i < ans.size(); i ++) {
            node[i].num = i;
            node[i].n = ans[i].size();
            for(int j = 0; j < ans[i].size(); j ++) {
                node[i].all += cost[pos[ans[i][j]]];
            }
            node[i].ave = 1.0 * node[i].all / node[i].n;
        }
    
        sort(node, node + ans.size(), cmp);
        printf("%d %d %d %d
    ", cnt, MinStep, node[0].all, (int)node[0].ave);
        cout << st;
        for(int i = 0; i < ans[node[0].num].size(); i ++) {
            cout << "->";
            cout << pos[ans[node[0].num][i]];
        }
        return 0;
    }
    

      渐渐开始不喜欢最短路

  • 相关阅读:
    求职方法论
    测试经验与教训_学习笔记
    测试架构师修炼之道_学习笔记
    Jmeter测试oracle
    Jmeter 非UI界面jmx脚本不能正常退出
    Jmeter参数化的理解
    jmeter 测试并发
    Jmeter测试数据库
    pytorch runtime error: CUDNN_STATUS_MAPPING_ERROR
    Python/pytorch 切换国内源/AttributeError: module 'torch.jit' has no attribute 'unused'/not a trusted or secure host
  • 原文地址:https://www.cnblogs.com/zlrrrr/p/10389549.html
Copyright © 2011-2022 走看看