1087. All Roads Lead to Rome (30)

简介: #include #include #include #include using namespace std;int n, k;const int inf = 99999999;const int msize...
#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;
int n, k;
const int inf = 99999999;
const int msize = 201;
int e[msize][msize], weight[msize], dis[msize];
bool visited[msize];
vector<int> pre[msize], temppath, path;
map<string, int> ma;
map<int, string> mb;
int maxvalue = 0, mindepth, cntpath = 0;
double maxavg;
//满足 最小代价->最大幸福->最大平均幸福
void dfs(int v){
    temppath.push_back(v);
    if(v == 1){
        int value = 0;
        for (int i = 0; i < temppath.size(); i++)
            value += weight[temppath[i]];
        double tempavg = 1.0 * value / (temppath.size() - 1);
        if (value > maxvalue) {
            maxvalue = value;
            maxavg = tempavg;
            path = temppath;
        }else if(value == maxvalue && tempavg > maxavg){
            maxavg = tempavg;
            path = temppath;
        }
        temppath.pop_back();
        cntpath++;
        return;
    }
    for (int i = 0; i < pre[v].size(); i++)
        dfs(pre[v][i]);
    temppath.pop_back();
}
int main() {
    fill(e[0], e[0] + msize * msize, inf);
    fill(dis, dis + msize, inf);
    cin >> n >> k;
    string s;
    cin >> s;
    ma[s] = 1;
    mb[1] = s;
    for (int i = 1; i < n; i++) {
        cin >> s >> weight[i+1];
        ma[s] = i + 1;
        mb[i+1] = s;
    }
    string sa, sb;
    int temp;
    for (int i = 0; i < k; i++) {
        cin >> sa >> sb >> temp;
        e[ma[sa]][ma[sb]] = temp;
        e[ma[sb]][ma[sa]] = temp;
    }
    //dijkstra
    dis[1] = 0;
    for (int i = 0; i < n; i++) {
        int u = -1, minn = inf;
        for (int j = 1; j <= n; j++) {
            if(visited[j] == false && dis[j] < minn){
                u = j;
                minn = dis[j];
            }
        }
        if (u == -1) break;
        visited[u] = true;
        for (int v = 1; v <= n; v++) {
            if (visited[v] == false && e[u][v] != inf) {
                if (dis[u] + e[u][v] < dis[v]) {
                    dis[v] = dis[u] + e[u][v];
                    pre[v].clear();
                    pre[v].push_back(u);
                }else if(dis[u] + e[u][v] == dis[v]){
                    pre[v].push_back(u);
                }
            }
        }
    }
    int rom = ma["ROM"];
    dfs(rom);
    printf("%d %d %d %d\n", cntpath, dis[rom], maxvalue, (int)maxavg);
    for (int i = (int)path.size() - 1; i >= 1; i--)
        cout << mb[path[i]] << "->";
    cout << "ROM\n";
    return 0;
}
目录
相关文章
|
C++
hackerrank challenges median
只能说这题我想多了,使用普通的插入排序完全可以解决这道题,在查找的时候用二分加快查找速度。 正确解题报告 这道题的关键在于,不能用int,因为两个int相加可能会越界!因为这个WA了好多遍。所以改用long long。 对double,使用math.h中的函数ceil(double)可以取整,根据ceil(v) == v的结果可以判断v是否是整数。
51 0
|
存储 机器学习/深度学习 算法
【PAT甲级】1087 All Roads Lead to Rome
【PAT甲级】1087 All Roads Lead to Rome
77 0
The Preliminary Contest for ICPC China Nanchang National Invitational A题 PERFECT NUMBER PROBLEM
The Preliminary Contest for ICPC China Nanchang National Invitational A题 PERFECT NUMBER PROBLEM
70 0
|
人工智能
Educational Codeforces Round 98 (Rated for Div. 2)B-Toy Blocks
You are asked to watch your nephew who likes to play with toy blocks in a strange way. He has n boxes and the i-th box has ai blocks. His game consists of two steps: he chooses an arbitrary box i; he tries to move all blocks from the i-th box to other boxes.
260 0
Determination of movement type in SAP STO outbound delivery
Determination of movement type in SAP STO outbound delivery
Determination of movement type in SAP STO outbound delivery
PAT (Advanced Level) Practice - 1087 All Roads Lead to Rome(30 分)
PAT (Advanced Level) Practice - 1087 All Roads Lead to Rome(30 分)
100 0
PAT (Advanced Level) Practice - 1096 Consecutive Factors(20 分)
PAT (Advanced Level) Practice - 1096 Consecutive Factors(20 分)
145 0
【1087】All Roads Lead to Rome (30 分)
【1087】All Roads Lead to Rome (30 分) 【1087】All Roads Lead to Rome (30 分)
120 0
【1096】Consecutive Factors (20 分)
【1096】Consecutive Factors (20 分) 【1096】Consecutive Factors (20 分)
124 0