用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-06-06Package Easy(基于 NSIS 的打包exe安装包工具)使用方法-icode9专业技术文章分享
- 2024-06-06基于 casdoor 的 ELK 开源登录认证解决方案: elk-auth-casdoor-icode9专业技术文章分享
- 2024-05-29Elasticsearch慢查询日志配置
- 2024-05-29揭秘华为如此多成功项目的产品关键——Charter模板
- 2024-05-29海外IDC业务拓展的7大挑战
- 2024-05-29InLine Chat功能优化对标Github Copilot,CodeGeeX带来更高效、更直观的编程体验!
- 2024-05-29CodeGeeX 智能编程助手 6 项功能升级,在Visual Studio插件市场霸榜2周!
- 2024-05-29AutoMQ 生态集成 Apache Doris
- 2024-05-292024年IDC行业的深度挖掘:机遇、挑战与未来展望
- 2024-05-29五款扩展组件齐发 —— Volcano、Keda、Crane-scheduler 等,邀你体验