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> # ...
随机推荐
- substring,subsequence,charAt执行效率的不同
package com.java.tencent; public class T_2_longestPalindrome { public String test1(String s){ long s ...
- 关于 php 调用 其他语言写的Web Service SOAP 接口的参数传递问题
关于 php 调用 其他语言写的Web Service SOAP 接口的参数传递问题,有需要的朋友可以参考下. php调用java写的soap接口经验: 场景一: java是以数组的形式接收参数的 ...
- 为CentOS配置网易163的yum源
Yum (Yellow dog Updater, Modified)是一个基于 RPM 包管理的字符前端软件包管理器.能够从指定的服务器自动下载 RPM 包并且安装,可以处理依赖性关系,并且一次安装所 ...
- View数据呈现相关技术
一.了解Razor语法 1.Razor基本语法 a)输出单一变量时不需要加分号做结尾.如: <p>现在时刻:@DateTime.Now</p> b)输出一段含有空白字元或运算子 ...
- EntityFramework走马观花之CRUD(中)
如果是独立的实体对象,在底层数据库中它对应一张独立的表,那么,对它进行新建.删除和修改没有任何难度,实在不值浪费笔墨在它上头. 在现实项目中,完全独立的对象少之又少,绝大多数情况都是对象之间有着紧密的 ...
- python 基础 2.7 range与xrange的区别
#/usr/bin/python #coding=utf-8 #@Time :2017/10/25 19:22 #@Auther :liuzhenchuan #@File :range与xrange的 ...
- 用Delphi实现网络视频编程
在MSN.QQ等聊天类的应用程序中,都应用到了网络视频技术.Delphi使用Object Pascal语言是一种完全面向对象语言,可以开发出灵活强大的程序,开发网络视频程序也不在话下.一个完整的网络视 ...
- 【BZOJ3630】[JLOI2014]镜面通道 几何+最小割
[BZOJ3630][JLOI2014]镜面通道 Description 在一个二维平面上,有一个镜面通道,由镜面AC,BD组成,AC,BD长度相等,且都平行于x轴,B位于(0,0).通道中有n个外表 ...
- MANIFEST.MF 文件内容完全详解(转)
打开Java的JAR文件我们经常可以看到文件中包含着一个META-INF目录, 这个目录下会有一些文件,其中必有一个MANIFEST.MF,这个文件描述了该Jar文件的很多信息,下面将详细介绍MANI ...
- iOS 蓝牙开发之(mutipeerConnectivity)
蓝牙 mutipeerConnectivity iOS7 引入的一个全新框架 替代GameKit框架 多用于文件传输 iOS设备不联网也能给附近的人聊天 搜索和传输的方式 * 双方WIFI和蓝牙都没有 ...