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 97
dijkstra算法,需要记录路径,到某点最小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> # ...
随机推荐
- hashCode与equals的作用与区别及应当注意的细节
最近去面试了几家公司,被问到hashCode的作用,虽然回答出来了,但是自己还是对hashCode和equals的作用一知半解的,所以决定把它们研究一下. 以前写程序一直没有注意hashCode的作用 ...
- request 防盗链
package request; import java.io.IOException;import javax.servlet.ServletException;import javax.servl ...
- 安卓ADT离线安装
http://jingyan.baidu.com/article/3aed632e66858770108091bf.html
- 字符串查找strpos()函数用法
#如果id=3 在字符串中查找出3是否存在.$str="2,12,33,22,55"; if(strpos(','.$id.',',','.$str.',')!==FALSE){ ...
- 基于Apache POI 从xlsx读出数据
[0]写在前面 0.1) these codes are from 基于Apache POI 的从xlsx读出数据 0.2) this idea is from http://cwind.iteye. ...
- python跳坑手记
解决python同目录报错:import util 改成 from . import util
- hadoop基础----hadoop理论(四)-----hadoop分布式并行计算模型MapReduce具体解释
我们在前一章已经学习了HDFS: hadoop基础----hadoop理论(三)-----hadoop分布式文件系统HDFS详细解释 我们已经知道Hadoop=HDFS(文件系统,数据存储技术相关)+ ...
- 【BZOJ4569】[Scoi2016]萌萌哒 倍增+并查集
[BZOJ4569][Scoi2016]萌萌哒 Description 一个长度为n的大数,用S1S2S3...Sn表示,其中Si表示数的第i位,S1是数的最高位,告诉你一些限制条件,每个条件表示为四 ...
- 发送邮件 Email(java实现)
//发送邮件 private static void sendMail(String mail, String mailContext) { try { //获取文本中html的内容 并动态替换并显示 ...
- [持续集成]Jenkins 自动化部署 Maven 工程
一.Jenkins 持续部署原理图 基础服务: 1 SVN 服务 SVN是Subversion的简称,是一个开放源代码的版本控制系统.说得简单一点SVN就是用于多个人共同开发同一个项目,共用资源的目的 ...