1087. All Roads Lead to Rome (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
HZH->PRS->ROM
#include<stdio.h>
#include<map>
#include<string>
#include<string.h>
#include<stack>
using namespace std;
#define MAX 210
int INF = ;
int HappyVal[];
int visit[MAX];
int Grap[MAX][MAX];
int d[MAX];
int h[MAX];
int num[MAX];
int pre[MAX];
int Count[MAX]; void Dijkstra(int Begin,int NodeNum)
{
d[Begin] = ;
h[Begin] = HappyVal[Begin];
num[Begin] = ;
Count[Begin] = ;
for(int i = ;i < NodeNum ;i++)
{
int index = -;
int MIN = INF;
for(int j = ;j <NodeNum ;j++)
{
if(!visit[j] && d[j] < MIN)
{
index = j;
MIN = d[j];
}
} if(index == -) return ;
visit[index] = true;
for(int v = ;v <NodeNum ;v++)
{
if(!visit[v] && Grap[index][v]!=INF)
{
if(d[index]+Grap[index][v]<d[v])
{
d[v] = d[index]+Grap[index][v];
num[v] = num[index];
h[v] = h[index] + HappyVal[v];
pre[v] = index;
Count[v] = Count[index] +;
}
else if(d[index]+Grap[index][v]==d[v])
{
num[v] = num[v] + num[index]; if(h[v] < h[index] + HappyVal[v])
{
h[v] = h[index] + HappyVal[v];
Count[v] = Count[index] +;
pre[v] = index;
}
else if( h[v] == h[index] + HappyVal[v] && (double)(h[index] + HappyVal[v])/(Count[index]+) > (double)h[v]/Count[v])
{
Count[v] = Count[index] +;
pre[v] = index;
}
}
}
}
} } int main()
{
int i,j,N,K,happy,ROM;
char Begin[],tem[];
scanf("%d%d%s",&N,&K,Begin);
map<string,int> mm;
map<int,string> mm2;
mm[Begin] = ;
mm2[] = Begin ;
HappyVal[mm[Begin]] = ;
for(i = ; i < N ;i++)
{
scanf("%s%d",tem,&happy);
if(strcmp("ROM",tem)==) ROM = i;
mm[tem] = i;
mm2[i] = tem;
HappyVal[i] = happy;
} char x[],y[]; for(i = ; i < N ;i++)
{
for(j = ; j < N ;j++)
{
Grap[i][j] = INF;
}
d[i] = h[i] = INF;
pre[i] = -;
Count[i] = ;
} for(i = ; i < K ;i++)
{
scanf("%s%s",x,y);
scanf("%d",&Grap[mm[x]][mm[y]]);
Grap[mm[y]][mm[x]] = Grap[mm[x]][mm[y]];
} Dijkstra( mm[Begin] , N); printf("%d %d %d %d\n",num[mm["ROM"]],d[mm["ROM"]],h[mm["ROM"]],h[mm["ROM"]]/Count[mm["ROM"]]); stack<int> ss;
i= mm["ROM"];
while(i != -)
{
ss.push(i);
i = pre[i];
}
int fir = ;
while(!ss.empty())
{
if(fir == )
{
fir = ;
printf("%s",mm2[ss.top()].c_str());
}
else printf("->%s",mm2[ss.top()].c_str());
ss.pop();
} printf("\n"); return ;
}
1087. All Roads Lead to Rome (30)的更多相关文章
- [图的遍历&多标准] 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 ...
- 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 ...
- PAT甲级练习 1087 All Roads Lead to Rome (30分) 字符串hash + dijkstra
题目分析: 这题我在写的时候在PTA提交能过但是在牛客网就WA了一个点,先写一下思路留个坑 这题的简单来说就是需要找一条最短路->最开心->点最少(平均幸福指数自然就高了),由于本题给出的 ...
- PAT (Advanced Level) 1087. All Roads Lead to Rome (30)
暴力DFS. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...
- 【PAT甲级】1087 All Roads Lead to Rome (30 分)(dijkstra+dfs或dijkstra+记录路径)
题意: 输入两个正整数N和K(2<=N<=200),代表城市的数量和道路的数量.接着输入起点城市的名称(所有城市的名字均用三个大写字母表示),接着输入N-1行每行包括一个城市的名字和到达该 ...
- 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 ...
- pat1087. All Roads Lead to Rome (30)
1087. All Roads Lead to Rome (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
- 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 题意: 确实有从我们这个城市到罗马的不同的旅游线路.您应该以最低的成本找到您的客户的路线,同时获得最大的幸福. 输入规格: 每个输入 ...
随机推荐
- oracle11g asm standalone 单实例重建
原文地址:oracle11g asm单实例重建has 作者:datapeng 最近到客户那里处理故障,客户说,他们修改了一下hostname,导到has出现了问题,当然,他们的数据库也就无法再启动,把 ...
- javaweb学习总结七(XML语言作用、语法)
一:XML语言的概念以及作用 1:xml概念:extensible Markup language,可扩展行标记语言,因为html的语法比较混乱,不够严谨. 用html写的系统不好维护,所以w3c组织 ...
- java下实现调用oracle的存储过程和函数
在Oracle下创建一个test的账户,然后 1.创建表:STOCK_PRICES --创建表格 CREATE TABLE STOCK_PRICES( RIC VARCHAR() PRIMARY KE ...
- 使用git ftp发布我个人的hexo博客内容
自己虚拟主机中的博客是由hexo3 + next主题,因为我想将 hexo 编译生成的文件可以通过ftp命令发布到ftp服务器上面. 发布使用的工具是git-ftp: 按照Use Jenkins an ...
- c#语法笔记
书写代码需要注意的地方: 1.代码中出现的所有标点都是英文半角 shift键快速切换中文半角和英文半角 shift+空格 切换全角/半角 2.在c#代码中,每行代码的结束,我们都以分号结束,注意:这个 ...
- js中定义对象的方式有哪些?
1.对象直接量 2.构造函数 3.原型方法 4.动态原型方法
- C#编写windows服务程序
Windows Service这一块并不复杂,但是注意事项太多了,网上资料也很凌乱,偶尔自己写也会丢三落四的.所以本文也就产生了,本文不会写复杂的东西,完全以基础应用的需求来写,所以不会对Window ...
- 程序生成SiteMapPath文件
//创建站点地图 private void CreateSiteMap(DataSet ds) { XmlDeclaration declareation; declareation = xmlDoc ...
- 第二篇、微信程序尺寸rpx
微信小程序尺寸单位rpx以及样式相关介绍rpx单位是微信小程序中css的尺寸单位,rpx可以根据屏幕宽度进行自适应.规定屏幕宽为750rpx.如在 iPhone6 上,屏幕宽度为375px,共有750 ...
- HTML+CSS学习笔记 (6) - 开始学习CSS
HTML+CSS学习笔记 (6) - 开始学习CSS 认识CSS样式 CSS全称为"层叠样式表 (Cascading Style Sheets)",它主要是用于定义HTML内容在浏 ...