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> # ...
随机推荐
- 怎样给filter加入自己定义接口
.在Cfilter类的定义中实现Interface接口的函数的定义: //-----------------------Interface methods----------------------- ...
- Android UI开源框架
1.Side-Menu.Android 分类側滑菜单,Yalantis 出品. 项目地址:https://github.com/Yalantis/Side-Menu.Android 2.Context ...
- 自己定义ViewGroup控件(二)----->流式布局进阶(二)
main.xml <?xml version="1.0" encoding="utf-8"? > <com.example.SimpleLay ...
- kotlin 语言入门指南(一)--基础语法
基于官网的Getting Start的基础语法教程部分,一共三节,这篇是第一节,翻译如下: 基础语法 定义一个包 包的声明必须放在文件头部: package my.demo import java.u ...
- python学习(五)列表
#!/usr/bin/python # 列表的学习, 列表的概念不陌生, 就是熟悉一下python中的列表是如何操作的 # 1. 序列的操作 L = [ 123, 'spam', 1.23] # 里面 ...
- eclipse maven 刷新报错
问题描述: An internal error occurred during: "Loading descriptor for cmbc_wms.".java.lang.Null ...
- Option可选值可选值(二)
//: Playground - noun: a place where people can play import Cocoa var str1 = "供选链接和强制拆包的不同. &qu ...
- EasyNVR无插件IPC摄像机直播方案前端构建之:如何区分PC端和移动端
EasyNVR前端为了更好的用户体验,不仅仅设有PC客户端,还适应移动客户端: EasyNVR的客户端中PC端和移动端差异有很多.例如: 由于PC端.移动端自身硬件的差异,所需要展示的样式也会存在一定 ...
- C#中GroupBox控件的使用(转)
GroupBox(框架)控件是C#中用来组织其他控件形成一个控件组,它的使用方法为[工具箱]->[所有Windows窗体](或者是[容器]列表中)->[GroupBox],拖拽到窗体界面中 ...
- new 和 make 均是用于分配内存
the-way-to-go_ZH_CN/06.5.md at master · Unknwon/the-way-to-go_ZH_CN https://github.com/Unknwon/the-w ...