1087 All Roads Lead to Rome
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 N−1 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
题意:
题目大体上是一个求最短路的问题,只不过又在此基础上增加了一些其他的内容,比如说:找出最短路的条数,以及路途过程中happinese的最大值和average happinese的最大值问题。
思路:
最短路径涉及到图论,所以就要表示出图,这里我用的是连接矩阵来表示图,所以先用map将城市的名字改成数字,最后输出的时候再改回来。然后就是用Dijkstra来求最短路了。
Code:
#include<iostream>
#include<map>
#include<string>
#include<vector>
#include<stack> using namespace std;
const int Inf = 0x7fffffff; int findMinDist(vector<int> &dist, vector<bool> &collected) {
int ret = Inf, j = -1;
int len = dist.size();
for (int i = 1; i < len; ++i) {
if (!collected[i] && ret > dist[i]) {
ret = dist[i];
j = i;
}
}
return j;
} int main() {
int n, k;
string start; cin >> n >> k >> start; map<string, int> EnToNum;
map<int, string> NumToEn;
EnToNum[start] = 0;
NumToEn[0] = start;
vector<int> happiness(n);
string name;
int value;
for (int i = 1; i < n; ++i) {
cin >> name >> value;
EnToNum[name] = i;
NumToEn[i] = name;
happiness[i] = value;
} vector<vector<int> > martix(n, vector<int>(n, -1));
vector<bool> collected(n, false);
vector<int> path(n, -1);
string name1, name2;
int num1, num2, cost;
for (int i = 0; i < k; ++i) {
cin >> name1 >> name2 >> cost;
num1 = EnToNum[name1];
num2 = EnToNum[name2];
martix[num1][num2] = cost;
martix[num2][num1] = cost;
} vector<int> dist(n, Inf);
dist[0] = 0;
collected[0] = true;
for (int i = 1; i < n; ++i) {
if (martix[0][i] > 0) {
dist[i] = martix[0][i];
path[i] = 0;
}
} int end = EnToNum["ROM"];
vector<pair<int, int> > backtrack;
// Dijkstra算法
while (1) {
int minVertex = findMinDist(dist, collected);
if (minVertex == -1) break;
collected[minVertex] = true;
for (int i = 0; i < n; ++i) {
if (martix[minVertex][i] > 0 && !collected[i]) {
if (dist[minVertex] + martix[minVertex][i] <= dist[i]) {
dist[i] = dist[minVertex] + martix[minVertex][i];
path[i] = minVertex;
if (i == end) {
backtrack.push_back({dist[i], minVertex});
}
}
}
}
} int count = 0;
vector<int> Vbacktrack;
for (int i = 0; i < backtrack.size(); ++i) {
if (backtrack[i].first == dist[end]) {
count++;
Vbacktrack.push_back(backtrack[i].second);
}
} int maxHappinese = -1, aveHappinese = -1;
int maxHappVertex;
for (int i = 0; i < Vbacktrack.size(); ++i) {
int begin = Vbacktrack[i], temp = happiness[end], numOfVertex = 1;
while (path[begin] != -1) {
temp += happiness[begin];
numOfVertex++;
begin = path[begin];
}
if (temp > maxHappinese) {
maxHappinese = temp;
aveHappinese = temp / numOfVertex;
maxHappVertex = Vbacktrack[i];
} else if (temp == maxHappinese && aveHappinese < temp/numOfVertex) {
aveHappinese = temp / numOfVertex;
maxHappVertex = Vbacktrack[i];
}
} cout << dist[end] << " " << count << " " << maxHappinese << " " << aveHappinese << endl; stack<int> pathToRom;
pathToRom.push(end);
while (path[maxHappVertex] != -1) {
pathToRom.push(maxHappVertex);
maxHappVertex = path[maxHappVertex];
}
cout << start;
while (!pathToRom.empty()) {
cout << "->" << NumToEn[pathToRom.top()];
pathToRom.pop();
}
cout << endl; return 0;
}
搞了半天就通过了一组数据。
1087 All Roads Lead to Rome的更多相关文章
- PAT 1087 All Roads Lead to Rome[图论][迪杰斯特拉+dfs]
1087 All Roads Lead to Rome (30)(30 分) Indeed there are many different tourist routes from our city ...
- [图的遍历&多标准] 1087. All Roads Lead to Rome (30)
1087. All Roads Lead to Rome (30) Indeed there are many different tourist routes from our city to Ro ...
- PAT 1087 All Roads Lead to Rome
PAT 1087 All Roads Lead to Rome 题目: Indeed there are many different tourist routes from our city to ...
- PAT甲级1087. All Roads Lead to Rome
PAT甲级1087. All Roads Lead to Rome 题意: 确实有从我们这个城市到罗马的不同的旅游线路.您应该以最低的成本找到您的客户的路线,同时获得最大的幸福. 输入规格: 每个输入 ...
- PAT 甲级 1087 All Roads Lead to Rome(SPFA+DP)
题目链接 All Roads Lead to Rome 题目大意:求符合题意(三关键字)的最短路.并且算出路程最短的路径有几条. 思路:求最短路并不难,SPFA即可,关键是求总路程最短的路径条数. 我 ...
- 1087. All Roads Lead to Rome (30)
时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Indeed there are many different ...
- PAT 甲级 1087 All Roads Lead to Rome
https://pintia.cn/problem-sets/994805342720868352/problems/994805379664297984 Indeed there are many ...
- 1087 All Roads Lead to Rome (30)(30 分)
Indeed there are many different tourist routes from our city to Rome. You are supposed to find your ...
- 1087 All Roads Lead to Rome (30 分)(最短路径)
直接用Dijkstra做 #include<bits/stdc++.h> using namespace std; int n,m; map<string,int>si; ma ...
随机推荐
- 看完我的笔记不懂也会懂----MarkDown使用指南
目录 语法 [TOC] 自动生成目录 1. 标题 2. 文本强调 3. 列表 4. 图片 5. 超链接 6. 文本引用 7. 分割线 8. 代码 9. 任务列表 (MPE专属) 10. 表格 11. ...
- 学习java之基础语法(三)
学习java之基础语法(三) java运算符 计算机的最基本用途之一就是执行数学运算,作为一门计算机语言,Java也提供了一套丰富的运算符来操纵变量.我们可以把运算符分成以下几组: 算术运算符 关系运 ...
- 让人头疼的AI bug (随想)
虽然概念上,人工智能和机器学习不等同.但是本文提及的AI,指的是基于机器学习的AI. 一个软件产品,出了错误叫bug,bug需要修.那一个机器学习的模型,准确率在那摆着呢,大伙心知肚明是有一定的犯 ...
- Linux速通 大纲
1.Linux操作系统安装及初始化配置(熟悉) 2.Linux操作系统目录组成结构及文件级增删改查操作(重点) 3.Linux操作系统用户.权限管理(重点) 4.开源软件及Linux下软件包的管理(重 ...
- 01----python入门----python安装与初识
----python入门----python安装与初识 python是一门编程语言,作为学习python的开始,需要事先搞明白:编程的目的是什么?什么是编程语言?什么是编程? 这是一个很好的开头!但是 ...
- CF482E ELCA
一.题目 点此看题 二.解法 题目的提示已经足够明显了吧,肯定是要写一个 \(\tt link-cut-tree\) .我们只需要求出总和,再除以方案数就是期望.然后可以算每个点为 \(\tt lca ...
- MySQL入门(3)——数据类型
MySQL入门(3)--数据类型 数字类型 整数数据类型: 数据类型 取值范围 说明 单位 TINYINT 符号值:-127~127无符号值:0~255 最小的整数 1字节 BIT 符号值:-127~ ...
- IPFS挖矿靠谱吗?
IPFS是一个旨在创建持久且分布式存储和共享文件的网络传输协议,前景广阔且实用意义大,为区块链行业提供了一种新的可能.而IPFS挖矿挖出的FIL,则是在IPFS技术的基础上,对维护IPFS网络的用户的 ...
- 2018ICPC南京 A. Adrien and Austin
题目: 题意:1-N个石子每次只能取连续的1-K个问输赢.(一开始以为只是个简简单单的巴什游戏,激动的提交了一发wr了,再读了一遍题才发现是只能取连续的) 题解:当n==0或者k==1&&am ...
- 策略枚举:消除在项目里大批量使用if-else的正确姿势
文/朱季谦 想起刚开始接触JAVA编程的时候,若遇到大量流程判断语句,几乎满屏都是if-else语句,多得让自己都忘了哪里是头,哪里是尾,但是,纵然满屏是if-else,但彼时也没有觉得多别扭.等到编 ...