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 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 97dijkstra算法,需要记录路径,到某点最小cost,最大happiness,经过最小地点数。
HZH->PRS->ROM
代码:
#include <stdio.h>
#include <string.h>
#define inf 0x3f3f3f3f ///终点是ROM 城市名是三个大写字母
int n,k,d,num,destination;
int pos[],happiness[],sumhappiness[],dis[],mp[][],visited[],placenum[],path[],pathnum[];
char loc1[],loc2[],s[][];
int change(const char *t) {
return (t[] - 'A') + (t[] - 'A') * + (t[] - 'A') * * ;
}
void getpath(int t) {
if(t)getpath(path[t]);
if(t)printf("->");
printf("%s",s[t]);
}
int main() {
scanf("%d%d %s",&n,&k,loc1);
int temp = change(loc1);///地点名对应int值
pos[temp] = num ++;///int值对应位置 每安排一个位置,num就+1
strcpy(s[num - ],loc1);///同时记录对应位置 的地点名
for(int i = ;i < n;i ++) {
scanf("%s %d",loc1,&d);
temp = change(loc1);
pos[temp] = num ++;
happiness[num - ] = d;
strcpy(s[num - ],loc1);
}
destination = pos[change("ROM")];
for(int i = ;i < num;i ++) {///初始化
for(int j = ;j < num;j ++) {
mp[i][j] = inf;
}
dis[i] = inf;
path[i] = -;
}
dis[] = ;
pathnum[] = ;///路径数初始为1
for(int i = ;i < k;i ++) {
scanf("%s %s %d",loc1,loc2,&d);
int a = pos[change(loc1)],b = pos[change(loc2)];
mp[a][b] = mp[b][a] = d;
}
while() {///dijkstra
int t = -,mi = inf;
for(int i = ;i < num;i ++) {
if(visited[i])continue;
if(dis[i] < mi)mi = dis[i],t = i;
}
if(t == -)break;
visited[t] = ;
for(int i = ;i < num;i ++) {
if(visited[i] || mp[t][i] == inf)continue;
if(dis[i] > dis[t] + mp[t][i]) {
dis[i] = dis[t] + mp[t][i];
sumhappiness[i] = sumhappiness[t] + happiness[i];
placenum[i] = placenum[t] + ;
path[i] = t;
pathnum[i] = pathnum[t];
}
else if(dis[i] == dis[t] + mp[t][i]) {
pathnum[i] += pathnum[t];
if(sumhappiness[i] < sumhappiness[t] + happiness[i]) {
sumhappiness[i] = sumhappiness[t] + happiness[i];
path[i] = t;
placenum[i] = placenum[t] + ;
}
else if(sumhappiness[i] == sumhappiness[t] + happiness[i] && placenum[i] > placenum[t] + ) {
path[i] = t;
placenum[i] = placenum[t] + ;
}
}
}
}
printf("%d %d %d %d\n",pathnum[destination],dis[destination],sumhappiness[destination],sumhappiness[destination] / placenum[destination]);
getpath(destination);
}
1087 All Roads Lead to Rome (30)(30 分)的更多相关文章
- 1087 All Roads Lead to Rome (30 分)(最短路径)
直接用Dijkstra做 #include<bits/stdc++.h> using namespace std; int n,m; map<string,int>si; ma ...
- [图的遍历&多标准] 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[图论][迪杰斯特拉+dfs]
1087 All Roads Lead to Rome (30)(30 分) Indeed there are many different tourist routes from our city ...
- 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即可,关键是求总路程最短的路径条数. 我 ...
- PAT甲级练习 1087 All Roads Lead to Rome (30分) 字符串hash + dijkstra
题目分析: 这题我在写的时候在PTA提交能过但是在牛客网就WA了一个点,先写一下思路留个坑 这题的简单来说就是需要找一条最短路->最开心->点最少(平均幸福指数自然就高了),由于本题给出的 ...
- 1087. All Roads Lead to Rome (30)
时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Indeed there are many different ...
- PAT (Advanced Level) 1087. All Roads Lead to Rome (30)
暴力DFS. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...
随机推荐
- tf树
tf变换(1) TF库的目的是实现系统中任一个点在所有坐标系之间的坐标变换,也就是说,只要给定一个坐标系下的一个点的坐标,就能获得这个点在其他坐标系的坐标. 使用tf功能包,a. 监听tf变换: ...
- grunt使用一步一步讲解
grunt 是一套前端自动化工具,一个基于nodeJs的命令行工具,一般用于:① 压缩文件② 合并文件③ 简单语法检查 对于其他用法,我还不太清楚,我们这里简单介绍下grunt的压缩.合并文件,初学, ...
- 启动Eclipse时,启不起来JVM terminated. Exit code=-1
启动Eclipse时,启不起来JVM terminated. Exit code=-1 出现错误了,不知道什么原因原本好好的Eclipse,今天早上出问题了,启动不起来还抛出JVM terminate ...
- JDK设置Encoding编码格式
执行JAVA程序报错内容如下: java.lang.IllegalStateException: The Java Virtual Machine has not been configured to ...
- cocos2d-x AssetsManager libcurl使用心得
libcurl使用心得 最新正在写cocosclient更新的逻辑.研究了一下cocos2d-x自带的Libcurl,下面是自己在使用过程中的心得和遇到的未解问题.希望大家一起讨论一下,欢迎大家指导. ...
- python读写数据篇
一.读写数据1.读数据 #使用open打开文件后一定要记得调用文件对象的close()方法.比如可以用try/finally语句来确保最后能关闭文件.file_object = open('thefi ...
- 转义字符\r \n \t \b 截图
- HDFS relaxes a few POSIX requirements to enable streaming access to file system data
https://hadoop.apache.org/docs/r2.7.2/hadoop-project-dist/hadoop-hdfs/HdfsDesign.html Introduction [ ...
- .net 开源框架--转载
Json.NET http://json.codeplex.com/ Json.Net 是一个读写Json效率比较高的.Net框架.Json.Net 使得在.Net环境下使用Json更加简单.通过Li ...
- client网络优化方法
减小图片大小(使用WebP格式的图片) 大部分的Facebook应用数据都是图片,因此降低图片的大小就能够较少数据的下载量.从而加快下载速度,这一点在高延迟的网络环境下很重要.Facebook返回给 ...