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> # ...
随机推荐
- ubuntu16.04----jdk---install----config
1.下载jdk. 2.验证java是否安装,使用java -version命令,如下图所示说明没有安装: 3.在usr目录中创建一个jdk-8目录,如下图所示: 4.配置系统环境变量,编辑/etc/p ...
- 我的ngnix 配置内容
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #erro ...
- session 的工作原理以及使用细节和url编码
/**********************************************模拟页面************************************************* ...
- 果园里有一堆苹果,一共n头(n大于1小于9)熊来分,第一头为小东,它把苹果均分n份后,多出了一个,它扔掉了这一个,拿走了自己的一份苹果,接着第二头熊重复这一过程,即先均分n份,扔掉一个然后拿走一份,以此类推直到最后一头熊都是这样(最后一头熊扔掉后可以拿走0个,也算是n份均分)。问最初这堆苹果最少有多少个。
include "stdafx.h" // ConsoleApplication12.cpp : 定义控制台应用程序的入口点. // #include<iostream> ...
- javascript onclick中post提交
对post提交进行封装: function post(URL, PARAMS) { var temp = document.createElement("form"); temp. ...
- 【selenium+Python unittest】之发送带中文附件的邮箱
完整原码如下: import smtplib from email.mime.text import MIMEText #from email.header import Header from em ...
- PowerBuilder -- 数据窗口
获取数据窗口列数 ls_colnum= integer(this.Describe("DataWindow.Column.Count")) 获取数据窗口列名 ls_colName ...
- Python 深入剖析SocketServer模块(二)(V2.7.11)
五.Mix-In混合类 昨天介绍了BaseServer和BaseRequestHandler两个基类,它们只用与派生,所以贴了它们派生的子类代码. 今天介绍两个混合类,ForkingMix-In 和 ...
- js为Object对象动态添加属性和值 eval c.k c[k]
const appendInfo = () => { const API_SECRET_KEY = 'https://github.com/dyq086/wepy-mall/tree/maste ...
- 我的Java开发学习之旅------>Java经典排序算法之快速排序
一.算法思想 快速排序是C.R.A.Hoare于1962年提出的一种划分交换排序.它采用了一种分治的策略,通常称其为分治法(Divide-and-ConquerMethod).(1) 分治法的 ...