https://pintia.cn/problem-sets/994805342720868352/problems/994805379664297984

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

代码:

#include <bits/stdc++.h>
using namespace std; #define inf 0x3f3f3f3f
int N, K;
string st;
map<string, int> mp, cost;
map<int, string> pos;
int maze[220][220], vis[220], dis[220], see[220];
int MinStep = 0, cnt = 0;
vector<vector<int> > ans;
vector<int> v; struct Node{
double ave;
int all = 0;
int num;
int n;
}node[100010]; bool cmp(const Node &a, const Node &b) {
if(a.all != b.all) return a.all > b.all;
return a.ave > b.ave;
} void dfs(int act, int step) {
if(step > MinStep) return; if(act == mp["ROM"]) {
cnt ++;
ans.push_back(v);
return;
} for(int i = 1; i <= N; i ++) {
if(maze[act][i] != inf && dis[act] + maze[act][i] == dis[i] && see[i] == 0) {
v.push_back(i);
see[i] = 1;
dfs(i, step + maze[act][i]);
v.pop_back();
see[i] = 0;
}
}
} void dijkstra(int act) {
dis[act] = 0;
int temp = act; for(int i = 1; i <= N; i ++) {
int minn = inf;
for(int j = 1; j <= N; j ++) {
if(dis[j] < minn && vis[j] == 0) {
minn = dis[j];
temp = j;
}
}
vis[temp] = 1;
for(int k = 1; k <= N; k ++) {
if(vis[k] == 0)
dis[k] = min(dis[k], maze[temp][k] + dis[temp]);
}
}
} int main() {
memset(dis, inf, sizeof(dis));
memset(vis, 0, sizeof(vis));
memset(maze, inf, sizeof(maze));
memset(see, 0, sizeof(see));
scanf("%d%d", &N, &K);
cin >> st;
mp[st] = 1;
pos[1] = st;
for(int i = 0; i < N - 1; i ++) {
string city; int val;
cin >> city >> val;
mp[city] = i + 2;
pos[i + 2] = city;
cost[city] = val;
}
for(int i = 0; i < K; i ++) {
string stt, en; int cos;
cin >> stt >> en >> cos;
maze[mp[stt]][mp[en]] = maze[mp[en]][mp[stt]] = cos;
}
dijkstra(1);
MinStep = dis[mp["ROM"]]; dfs(1, 0); for(int i = 0; i < ans.size(); i ++) {
node[i].num = i;
node[i].n = ans[i].size();
for(int j = 0; j < ans[i].size(); j ++) {
node[i].all += cost[pos[ans[i][j]]];
}
node[i].ave = 1.0 * node[i].all / node[i].n;
} sort(node, node + ans.size(), cmp);
printf("%d %d %d %d\n", cnt, MinStep, node[0].all, (int)node[0].ave);
cout << st;
for(int i = 0; i < ans[node[0].num].size(); i ++) {
cout << "->";
cout << pos[ans[node[0].num][i]];
}
return 0;
}

  渐渐开始不喜欢最短路

PAT 甲级 1087 All Roads Lead to Rome的更多相关文章

  1. PAT甲级1087. All Roads Lead to Rome

    PAT甲级1087. All Roads Lead to Rome 题意: 确实有从我们这个城市到罗马的不同的旅游线路.您应该以最低的成本找到您的客户的路线,同时获得最大的幸福. 输入规格: 每个输入 ...

  2. PAT 甲级 1087 All Roads Lead to Rome(SPFA+DP)

    题目链接 All Roads Lead to Rome 题目大意:求符合题意(三关键字)的最短路.并且算出路程最短的路径有几条. 思路:求最短路并不难,SPFA即可,关键是求总路程最短的路径条数. 我 ...

  3. PAT甲级——A1087 All Roads Lead to Rome【30】

    Indeed there are many different tourist routes from our city to Rome. You are supposed to find your ...

  4. 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 ...

  5. 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 ...

  6. [图的遍历&多标准] 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 ...

  7. 【PAT甲级】1087 All Roads Lead to Rome (30 分)(dijkstra+dfs或dijkstra+记录路径)

    题意: 输入两个正整数N和K(2<=N<=200),代表城市的数量和道路的数量.接着输入起点城市的名称(所有城市的名字均用三个大写字母表示),接着输入N-1行每行包括一个城市的名字和到达该 ...

  8. PAT甲级练习 1087 All Roads Lead to Rome (30分) 字符串hash + dijkstra

    题目分析: 这题我在写的时候在PTA提交能过但是在牛客网就WA了一个点,先写一下思路留个坑 这题的简单来说就是需要找一条最短路->最开心->点最少(平均幸福指数自然就高了),由于本题给出的 ...

  9. PAT (Advanced Level) 1087. All Roads Lead to Rome (30)

    暴力DFS. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...

随机推荐

  1. Mysql表创建外键报错

    数据库表A: CREATE TABLE task_desc_tab ( id INT(11) PRIMARY KEY NOT NULL COMMENT '自增主键' AUTO_INCREMENT, t ...

  2. flask 蓝本(blueprint)

    蓝本(blueprint) 一.基本概念: 将视图方法模块化,既当大量的视图函数放在一个文件中,很明显是不合适,最好的方案是根据功能将路由合理的划分到不同的文件中:而蓝本就是为了解决这个问题而出现的. ...

  3. Android 调用手机上第三方百度地图并传值给地图

    //移动APP调起Android百度地图方式举例 Intent intent = null; try { // intent = Intent.getIntent("intent://map ...

  4. P1726 上白泽慧音

    题目描述 在幻想乡,上白泽慧音是以知识渊博闻名的老师.春雪异变导致人间之里的很多道路都被大雪堵塞,使有的学生不能顺利地到达慧音所在的村庄.因此慧音决定换一个能够聚集最多人数的村庄作为新的教学地点.人间 ...

  5. HDU 3072 Intelligence System(tarjan染色缩点+贪心+最小树形图)

    Intelligence System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  6. Unable to start a VM due to insufficient capacity

    今天cloudstack中的一个普通用户创建虚拟机时,总是报错:Unable to start a VM due to insufficient capacity ,看management and a ...

  7. Linux SSH远程文件/文件夹传输命令scp

    相信各位VPSer在使用VPS时会常常在不同VPS间互相备份数据或者转移数据,大部分情况下VPS上都已经安装了Nginx或者类似的web server,直接将要传输的文件放到web server的文件 ...

  8. 20155333 《网络对抗》 Exp9 Web安全基础

    20155333 <网络对抗> Exp9 Web安全基础 基础问题回答 1.SQL注入攻击原理,如何防御? 原理: 通过在用户名.密码登输入框中输入一些',--,#等特殊字符,实现引号闭合 ...

  9. InkCanvas控件的使用

    原文:InkCanvas控件的使用 ==>InkCanvas设置位置跟Canvas一样.通过InkCanvas.Top之类的设置,需要设置的属性有EditingMode,来自于InkCanvas ...

  10. python 单体模式 的几种实现

    这是本人的一篇学习笔记. 本文用 python 实现单体模式,参考了这里 一.修改父类的 __dict__ class Borg: _shared_state = {} def __init__(se ...