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. Day 4-2 time & datetime模块

    time模块. import time time.time() #输出: 1523195163.140625 time.localtime() # 获取的是操作系统的时间,可以添加一个时间戳参数 # ...

  2. RPC框架-RMI、RPC和CORBA的区别

    关键词:RMI RPC CORBA简 介:本篇文章重点阐述RMI,附带介绍RPC和CORBA Java远程方法调用(Java RMI)是一组实现了远程方法调用(rmi)的API. java RMI是远 ...

  3. Partition算法以及其应用详解下(Golang实现)

    接前文,除了广泛使用在快速排序中.Partition算法还可以很容易的实现在无序序列中使用O(n)的时间复杂度查找kth(第k大(小)的数). 同样根据二分的思想,每完成一次Partition我们可以 ...

  4. 有时候做JQ动画,鼠标经过,它会不停自己抖动不停,解决方法(此处,是兼容IE ,当鼠标经过,遮罩层从下移到上边的JQ动画效果)

    <style> .x_sdbb { margin: 60px 0 40px 0; } .x_title2{ background: url(../images/hdb_img17.png) ...

  5. 老男孩python学习自修第五天【集合】

    特点: (1)无序 (2)不重复 使用场景: (1)关系测试 (2)去重 x & y 求交集 x | y 求并集 x - y 求差集 x ^ y 求对称差集 x.intersection(y) ...

  6. LodopJS文档式模版的加载和赋值

    Lodop模版有两种方法,一种是传统的JS语句,可以用JS方法里的eval来执行,一种是文档式模版,是特殊格式的base64码,此篇博文介绍文档式模版的加载,文档式模版的生成以及传统JS模版的生成加载 ...

  7. WSS Process On Causal LTI System

    Consider a real LTI system with a WSS process $x(t)$ as input and WSS process $y(t)$ as output. Base ...

  8. Nginx grpc反向代理

    L111 首先Grpc 默认编译进Nginx 但是依赖http_v2模块 需要编译进nginx 具体指令可以参考Nginx http 反向代理 指令都类似 分布式反向代理 server { serve ...

  9. E - Just a Hook HDU - 1698 线段树区间修改区间和模版题

    题意  给出一段初始化全为1的区间  后面可以一段一段更改成 1 或 2 或3 问最后整段区间的和是多少 思路:标准线段树区间和模版题 #include<cstdio> #include& ...

  10. B - I Hate It HDU - 1754 线段树区间最大值板子(单点更新,区间最大)

    第一次打 改了半天  各种小错误 难受 #include<cstdio> #include<iostream> using namespace std; +; int a[ma ...