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<=N<=200), 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 recommended. 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 recommended 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<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
using namespace std;
int G[][], happy[];
int visit[], dst[];
vector<int> pre[];
const int INF = ;
map<string, int> str2num;
map<int, string> num2str;
int N, K;
string start, fina = "ROM";
int start_, fina_, cnt = ;
void dijkstra(int s){
fill(visit, visit + , );
fill(dst, dst + , INF);
dst[s] = ;
for(int i = ; i < N; i++){
int u = -, minLen = INF;
for(int j = ; j < N; j++){
if(visit[j] == && dst[j] < minLen){
minLen = dst[j];
u = j;
}
}
if(u == -)
return;
visit[u] = ;
for(int j = ; j < N; j++){
if(visit[j] == && G[u][j] != INF){
if(G[u][j] + dst[u] < dst[j]){
dst[j] = G[u][j] + dst[u];
pre[j].clear();
pre[j].push_back(u);
}else if(G[u][j] + dst[u] == dst[j]){
pre[j].push_back(u);
}
}
}
}
} vector<int> path, ans;
int pathNum = , maxHapp = -, maxNum = -;
void DFS(int vt){
path.push_back(vt);
if(vt == start_){
pathNum++;
int sumTemp = ;
for(int i = path.size() - ; i >= ; i--){
sumTemp += happy[path[i]];
}
if(sumTemp > maxHapp){
maxNum = path.size() - ;
maxHapp = sumTemp;
ans = path;
}else if(sumTemp == maxHapp && path.size() < maxNum){
maxNum = path.size() - ;
maxHapp = sumTemp;
ans = path;
}
path.pop_back();
return;
}
int Len = pre[vt].size();
for(int i = ; i < Len; i++){
DFS(pre[vt][i]);
}
path.pop_back();
return;
}
int strNum(string ss){
if(str2num.count(ss) == ){
str2num[ss] = cnt;
num2str[cnt] = ss;
return cnt++;
}else{
return str2num[ss];
}
}
int main(){
cin >> N >> K >> start;
string temps, temps2;
fill(G[], G[] + *, INF);
int tempL;
for(int i = ; i < N - ; i++){
cin >> temps >> tempL;
happy[strNum(temps)] = tempL;
}
for(int i = ; i < K; i++){
cin >> temps >> temps2 >> tempL;
int u1 = strNum(temps), u2 = strNum(temps2);
G[u1][u2] = G[u2][u1] = tempL;
}
start_ = strNum(start);
fina_ = strNum(fina);
dijkstra(start_);
int costSum = dst[fina_];
DFS(fina_);
printf("%d %d %d %d\n%s", pathNum, costSum, maxHapp, maxHapp / maxNum, num2str[start_].c_str());
for(int i = ans.size() - ; i >= ; i--){
printf("->%s", num2str[ans[i]].c_str());
}
cin >> N;
return ;
}

总结:

1、题意:求出最短路,如果有多条,则求出一路上点权之和(开始节点不计入)最大的一条,如果还有多条,则求出点权之和的均值最大的一条(依然不计入开始节点)。

2、输入的城市名字通过map转化为整数节点,使用一个全局变量cnt分配节点编号。

3、pre[K]表示K节点的所有前驱节点。整个pre数组需要从目的地节点回溯到开始节点。

A1087. All Roads Lead to Rome的更多相关文章

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

  2. PAT_A1087#All Roads Lead to Rome

    Source: PAT A1087 All Roads Lead to Rome (30 分) Description: Indeed there are many different tourist ...

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

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

  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. PAT甲级1087. All Roads Lead to Rome

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

  7. PAT1087. All Roads Lead to Rome

    PAT1087. All Roads Lead to Rome 题目大意 给定一个图的边权和点权, 求边权最小的路径; 若边权相同, 求点权最大; 若点权相同, 则求平均点权最大. 思路 先通过 Di ...

  8. pat1087. All Roads Lead to Rome (30)

    1087. All Roads Lead to Rome (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...

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

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

随机推荐

  1. python爬虫-1

    import resquests #import urllib.request from bs4 import BeautifulSoup from collections import Ordere ...

  2. Spring JDBC模版以及三种数据库连接池的使用

    jar包版本有点乱,直接忽略版本号,将就一下. 这里引了aop包是因为在spring3版本之后用模版对数据库库操作时会出现问题,但是不会报错,也没有提示. 所以这里直接引入,以及之后会用到的DBCP与 ...

  3. linux查看端口是否开放

    在讨论这个问题前,我们先来了解一下物理端口.逻辑端口.端口号等计算机概念. 端口相关的概念: 在网络技术中,端口(Port)包括逻辑端口和物理端口两种类型.物理端口指的是物理存在的端口,如ADSL M ...

  4. 三、ASP.NET Core 部署Linux

    预备工作 1.删除dotnet core sdk sudo yum erase libunwind libicu 2.删除链接 sudo rm -rf /usr/local/bin 3.sudo yu ...

  5. 转 freemarker macro(宏)的使用

    有人说用freemarker,但没有用到它的宏(macro),就=没有真正用过freemarker.说的就是宏是freemarker的一大特色. 宏的定义可以查看相关的文档,里面介绍得很清楚,下面来看 ...

  6. 如何消除原生Android网络状态上的惊叹号

    喜欢使用原生Android系统的朋友可能会发现自己的状态栏信号图标上经常有一个惊叹号标志. 这是怎么回事呢?原因是Android为了对网络状态进行检测,采用了一种叫做captive detection ...

  7. 1.ansible基本参数介绍

    想使用ansible 先--help学习下基本的options吧小兄弟1: -m 指定模块名称只有一个模块command 可以省略:-M 指出模块路径来加载2: -a 指定模块参数就是模块的内容你知道 ...

  8. Android 下载App

    转载:http://blog.csdn.net/aicpzl/article/details/52993074 通过DownloadManager来下载APK到本地,下载完成后收到广播再安装APK,可 ...

  9. Django 缓存、序列化、信号

    一,缓存 由于Django是动态网站,所有每次请求均会去数据进行相应的操作,当程序访问量大时,耗时必然会更加明显,最简单解决方式是使用:缓存,缓存将一个某个views的返回值保存至内存或者memcac ...

  10. LOJ6053 简单的函数 【Min_25筛】【埃拉托斯特尼筛】

    先定义几个符号: []:若方括号内为一个值,则向下取整,否则为布尔判断 集合P:素数集合. 题目分析: 题目是一个积性函数.做法之一是洲阁筛,也可以采用Min_25筛. 对于一个可以进行Min_25筛 ...