用LUA(和C++)刷PAT (Advanced Level) ——1087 All Roads Lead to Rome

2021/9/4 17:06:06

本文主要是介绍用LUA(和C++)刷PAT (Advanced Level) ——1087 All Roads Lead to Rome,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <cstring>
#include <cmath>
#include <algorithm>

using namespace std;

struct city{
    string name;
    int happiness = 0, cumulative_happiness = 0;
    int cost = 0, route_number = 1;
    vector<int> route;
};

city citys[201];
int road[201][201];
map<string, int> name_id;
vector<int> dij;
bool citys_visited[201];

bool comp(int a_index, int b_index){ return citys[a_index].cost < citys[b_index].cost;}

int main()
{
    int N, K;
    string c;
    cin>>N>>K>>c;
    
    memset(road, -1, sizeof(int) *201 * 201);
    memset(citys_visited, false, sizeof(bool) * 201);
        //初始化城市
    citys[1].name = c;
    name_id[c] = 1;
    for(int i = 2; i <= N; i++){
        cin>>citys[i].name>>citys[i].happiness;
        name_id[citys[i].name] = i;
    }
    
        //初始化道路
    for(int i = 0; i < K; i++){
        string c1, c2;
        int cost;
        cin>>c1>>c2>>cost;
        road[name_id[c1]][name_id[c2]] = cost;
        road[name_id[c2]][name_id[c1]] = cost;
    }
        //初始化队列
    
    dij.push_back(1);
        //计算
    while(citys[dij[0]].name != "ROM"){
        int current_city_index = dij[0];
        city current_city = citys[current_city_index];
        citys_visited[current_city_index] = true;
        dij.erase(dij.begin());
        
        for(int i = 1; i < 201; i++){
            if(road[current_city_index][i] > 0){
                if (citys_visited[i])
                    continue;
                city& target_city = citys[i];
                if (target_city.cost > 0 && target_city.cost < current_city.cost + road[current_city_index][i])
                    continue;
                if (target_city.cost == current_city.cost + road[current_city_index][i])
                {
                    target_city.route_number += current_city.route_number;
                    if( target_city.cumulative_happiness < target_city.happiness + current_city.cumulative_happiness || 
                        (target_city.cumulative_happiness == target_city.happiness + current_city.cumulative_happiness &&
                        target_city.route.size() > current_city.route.size() + 1)){
                        target_city.route = current_city.route;
                        target_city.route.push_back(current_city_index);
                        target_city.cumulative_happiness = target_city.happiness + current_city.cumulative_happiness;
                    }
                    continue;
                }
                if(target_city.cost <= 0)
                    dij.push_back(i);
                target_city.cost = current_city.cost + road[current_city_index][i];
                target_city.route_number = current_city.route_number;
                target_city.route = current_city.route;
                target_city.route.push_back(current_city_index);
                target_city.cumulative_happiness = target_city.happiness + current_city.cumulative_happiness;
            }
        }
        sort(dij.begin(), dij.end(), comp);
    }
    city ROM = citys[name_id["ROM"]];
    cout<<ROM.route_number<<" "<<ROM.cost<<" "<<ROM.cumulative_happiness<<" "<<floor(ROM.cumulative_happiness/ROM.route.size())<<endl;
    for(auto itr = ROM.route.begin(); itr != ROM.route.end(); itr++)
        cout<<citys[*itr].name<<"->";
    cout<<"ROM"<<endl;
}


这篇关于用LUA(和C++)刷PAT (Advanced Level) ——1087 All Roads Lead to Rome的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程