用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的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-23新能源车企如何通过CRM工具优化客户关系管理,增强客户忠诚度与品牌影响力
- 2024-12-23原创tauri2.1+vite6.0+rust+arco客户端os平台系统|tauri2+rust桌面os管理
- 2024-12-23DevExpress 怎么实现右键菜单(Context Menu)显示中文?-icode9专业技术文章分享
- 2024-12-22怎么通过控制台去看我的页面渲染的内容在哪个文件中呢-icode9专业技术文章分享
- 2024-12-22el-tabs 组件只被引用了一次,但有时会渲染两次是什么原因?-icode9专业技术文章分享
- 2024-12-22wordpress有哪些好的安全插件?-icode9专业技术文章分享
- 2024-12-22wordpress如何查看系统有哪些cron任务?-icode9专业技术文章分享
- 2024-12-21Svg Sprite Icon教程:轻松入门与应用指南
- 2024-12-20Excel数据导出实战:新手必学的简单教程
- 2024-12-20RBAC的权限实战:新手入门教程