pat1087. All Roads Lead to Rome (30)
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
pat中类似的题目做得比较多,现在总结一下:
1.最大和初始距离为-1。这个条件不管是dis[],还是mindis,在使用Dijstra的时候,更新邻边当前最短路和求当前最短路的点的时候要注意,边不可到初始点的点先剔除。
2.对比string要比对比int慢很多,所要转换映射。
3.相邻点的数学关系要分析清楚。
4.最好加vis,可以更清晰。
5.初始点的初始化不要忘记!!
#include<cstdio>
#include<cstring>
#include<stack>
#include<algorithm>
#include<iostream>
#include<stack>
#include<set>
#include<map>
#include<vector>
using namespace std;
map<string,int> city;//string-int
map<int,string> rcity;
map<int,vector<pair<int,int> > > edge;
int dis[],path[],hcount[],happ[],fstep[],f[];
bool vis[];
int main()
{
//freopen("D:\\INPUT.txt","r",stdin);
int n,k,i,d,s;
string st,u,v;
scanf("%d %d",&n,&k);
memset(dis,-,sizeof(dis));//每个点的最短路长
memset(hcount,-,sizeof(hcount));//幸福指数之和
memset(vis,false,sizeof(vis));//是否计算过最短路径
memset(path,,sizeof(path));//前一个点
memset(fstep,,sizeof(fstep));//到这个点需要几步
cin>>st;//起始城市
city[st]=;//编号
rcity[]=st;//反编号
happ[]=;
dis[]=;
hcount[]=;
fstep[]=;
path[]=;//init
f[]=;
for(i=; i<n; i++)
{
f[i]=i;
cin>>u;
rcity[i]=u;
city[u]=i;
scanf("%d",&happ[i]);
} /*for(i=0;i<n;i++){
cout<<rcity[i]<<endl;
}*/ for(i=; i<k; i++)
{
cin>>u>>v;
scanf("%d",&d);
//cout<<u<<" "<<v<<" "<<d<<endl;
edge[city[u]].push_back(make_pair(city[v],d)); edge[city[v]].push_back(make_pair(city[u],d));
} /*for(i=0;i<n;i++){
vector<pair<int,int> >::iterator it;
cout<<"i: "<<i<<endl;
for(it=edge[i].begin(); it!=edge[i].end(); it++){
cout<<it->first<<" "<<it->second<<endl;
}
}*/ s=;
vector<pair<int,int> >::iterator it;
int next;
while(s!=city["ROM"])
{ //cout<<"s: "<<s<<endl; vis[s]=true;
for(it=edge[s].begin(); it!=edge[s].end(); it++) //update
{
next=it->first;
if(dis[next]==-||dis[next]>dis[s]+it->second)
{
dis[next]=dis[s]+it->second;
hcount[next]=hcount[s]+happ[next];
path[next]=path[s];
fstep[next]=fstep[s]+;
f[next]=s;
}
else
{
if(dis[next]==dis[s]+it->second)
{
path[next]+=path[s];//
if(hcount[next]<hcount[s]+happ[next])
{
hcount[next]=hcount[s]+happ[next];
fstep[next]=fstep[s]+;
f[next]=s;
}
else
{
if(hcount[next]==hcount[s]+happ[next])
{
if(fstep[next]>fstep[s]+)
{
fstep[next]=fstep[s]+;
f[next]=s;
}
}
}
}
}
} /*for(i=1;i<n;i++){
cout<<"i: "<<i<<" "<<dis[i]<<endl;
}*/ int mindis=-,minnum;
for(i=;i<n;i++)//find the min
{
if(dis[i]==-){//如果当前边到不了初始点,直接pass
continue;
}
if(!vis[i]&&(mindis==-||(dis[i]<mindis))){
//cout<<"ii: "<<i<<" "<<dis[i]<<endl;
mindis=dis[i];
minnum=i;
}
} //cout<<"minnum: "<<minnum<<" "<<dis[minnum]<<endl; s=minnum;
}
printf("%d %d %d %d\n",path[s],dis[s],hcount[s],hcount[s]/fstep[s]);
int p=s;
stack<int> ss;
while(p){
ss.push(p);
p=f[p];
}
cout<<rcity[p];
while(!ss.empty()){
cout<<"->"<<rcity[ss.top()];
ss.pop();
}
cout<<endl;
return ;
}
pat1087. 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 ...
- PAT1087. All Roads Lead to Rome
PAT1087. All Roads Lead to Rome 题目大意 给定一个图的边权和点权, 求边权最小的路径; 若边权相同, 求点权最大; 若点权相同, 则求平均点权最大. 思路 先通过 Di ...
- 1087. All Roads Lead to Rome (30)
时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Indeed there are many different ...
- 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 ...
- PAT_A1087#All Roads Lead to Rome
Source: PAT A1087 All Roads Lead to Rome (30 分) Description: Indeed there are many different tourist ...
随机推荐
- Linux curl 常用命令
命令:curl在Linux中curl是一个利用URL规则在命令行下工作的文件传输工具,可以说是一款很强大的http命令行工具.它支持文件的上传和下载,是综合传输工具,但按传统,习惯称url为下载工具. ...
- AU3获取系统激活信息
If IsActivated() = False Then ;InstallProductKey($OSkey) ; installs a product key and also activates ...
- 转载Json和Xml的区别,以及它们的底层是如何处理的
XML:可扩展标记语言 JSON:轻量级的数据交换格式 区别: 1.可读性方面:基本相同,Xml的可读性较好些: 2.可扩展性方面:都有较好的扩展性: 3.编码难度方面:json的编码较容 ...
- 洛谷P2518 [HAOI2010]计数
题目描述 你有一组非零数字(不一定唯一),你可以在其中插入任意个0,这样就可以产生无限个数.比如说给定{1,2},那么可以生成数字12,21,102,120,201,210,1002,1020,等等. ...
- web安全问题-csrf
web安全问题 csrf <script> document.write(` <form name="commentForm" target="csrf ...
- P4011 孤岛营救问题
\(\color{#0066ff}{题目描述}\) 1944 年,特种兵麦克接到国防部的命令,要求立即赶赴太平洋上的一个孤岛,营救被敌军俘虏的大兵瑞恩.瑞恩被关押在一个迷宫里,迷宫地形复杂,但幸好麦克 ...
- 圆环进度css
看效果先:http://sandbox.runjs.cn/show/b6bmksvn 参考: jquery圆环百分比进度条制作 CSS clip:rect矩形剪裁功能及一些应用介绍 CSS clip: ...
- js 遍历tree的一个例子(全遍历)
全遍历 亲测真是有效. 工作中遇到的问题应该算是比较有价值的问题. <!DOCTYPE html> <html lang="en"> <head> ...
- Unity 动画系统 Animation和Animator等常用类
- 学习C/C++需要掌握哪些知识
初级阶段 1.C语言 数据类型.变量.内存布局.指针基础: 字符串.一维数组.二维数组: 一级指针,二级指针,三级指针,N级指针概念,指针数组和数组指针: 结构体.文件的使用: 动态库的封装和设计: ...