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. Scala学习之路 (二)使用IDEA开发Scala

    目前Scala的开发工具主要有两种:Eclipse和IDEA,这两个开发工具都有相应的Scala插件,如果使用Eclipse,直接到Scala官网下载即可http://scala-ide.org/do ...

  2. Git系列七之备份迁移 升级 恢复管理

    0.Gitlab安装 1.安装和配置必要的依赖关系在CentOS7,下面的命令将在系统防火墙打开HTTP和SSH访问. yum install curl openssh-server postfix ...

  3. vagrant设置虚拟机的名字

    如果我们不在vagrant init 命令生成的vagrantfile文件中声明虚拟机的名字的话,一般会默认给我们指定一个名字,指定的方法: config.vm.provider "virt ...

  4. ArcGIS 9.3下载,包含ArcGIS Desktop、ArcGIS Engine、ArcGIS Server、ArcSDE、workstation

    KeyWord:ESRI ArcGIS 9.3 Desktop Server Engine ArcEngine ArcIMS ArcSDE Workstation ECP Lisence Crack ...

  5. PAT B1050 螺旋矩阵 (25 分)

    本题要求将给定的 N 个正整数按非递增的顺序,填入“螺旋矩阵”.所谓“螺旋矩阵”,是指从左上角第 1 个格子开始,按顺时针螺旋方向填充.要求矩阵的规模为 m 行 n 列,满足条件:m×n 等于 N:m ...

  6. JAVA 对基础知识的加强

    一.我们在写类的时候.设置字段的时候含义: 简单的javabean类: package jd.com.vo; import jd.com.mybaitstest.account; public cla ...

  7. Jmeter—实现识别验证码登录

    在做自动化测试或压力测试时,验证码总是一个问题.在以往的压力测试经历中,测试一般在独立的测试环境中进行,可以放心禁用验证码或使用万能验证码,这个是最实用的.但是,这两天我尝试了一个使用第三方的图形图像 ...

  8. W25Q128BV W25Q128FV W25Q128JV 什么区别?

    W25Q128BV  W25Q128FV  W25Q128JV  什么区别?    官网没有找到  BV 的相关信息, 倒是有 FV,FW,JV,JW 信息..2019年1月19日19:09:55 F ...

  9. 有关java(初学笔记)

    JAVA的主要优势:跨平台性,可以在Linux,windows,mac三个系统上运行. 跨平台的核心:JAVA虚拟机--JVM 原理就是将Java语言在这个系统上翻译.JAVA在jvm上运行,jvm进 ...

  10. Linux CentOS7系统中phpMyAdmin安装配置

    今天介绍的是如何在Linux CentOS7系统中配置phpMyAdmin. 目录 环境准备 安装包 基本设置 网站预览 环境准备 linux centos7系统 ssh软件 php语言环境 mysq ...