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 ... 
随机推荐
- nfs搭建和挂载
			1.搭建server a.创建共享目录 mkdir /nfs1 b.vim /etc/sysconfig/nfs 固定端口 c.vim /etc/export /nfs1 192.168.10 ... 
- DataGridView增加全选列
			最近的一个winform的项目中,碰到datagridview控件的第一列添加全选的功能,通常这个功能,有两种实现方式:1. 为控件添加DataGridViewCheckBoxColumn来实现,但是 ... 
- Insus Paging Utility Version 2
			Insus.NET对GridView或是DataList分页,都是使用自己的分页组件:http://www.cnblogs.com/insus/archive/2009/03/19/1417102.h ... 
- GitHub下载代码到本地
			1.git clone git@github.com:Sehunwy/test.git(加粗的是下载的地址) 2.下载完成,此时本地是这样的: 参考:https://blog.csdn.net/qq ... 
- Python3中简单的迭代器程序
			1.迭代器程序(实现菲比那次数列并且可以抛出与接收异常) def fib(max): n,a,b = 0,0,1 while n < max: #print(b) yield b a,b = b ... 
- mysql死锁问题解决
			1. 定位问题 http://blog.csdn.net/beiigang/article/details/43228361 2. 解决死锁 http://www.blogbus.com/ri0day ... 
- oracle select非group by的字段
			可以把group by的结果集当作一个表,然后从这里表里取数就可以了. e.g. SELECT A.PROJECT_CODE,A.DIE_NO,E.ONE_CONSUMING FROM (SELECT ... 
- 《Professional JavaScript for Web Developers》day03
			<Professional JavaScript for Web Developers>day03 1.1ECMAScript语法 1.1.1 区分大小写 1.1.2 标识符 按照惯例,E ... 
- php 多语言(UTF-8编码)导出Excel、CSV乱码解决办法之导出UTF-8编码的Excel、CSV
			新项目,大概情况是这样的:可能存在多国.不同语种使用者,比喻有中文.繁体中文,韩文.日本等等,开发时选择了UTF-8编码,开发顺利,没有问题.昨天做了一个csv导出功能,导出的东西完全乱了: 设置mb ... 
- P3943 星空
			传送门 观察题目数据,发现 k ≤ 8 ,可能可以从这里入手解决问题 考虑状态压缩 但是我们每次操作都会让一连串的序列改变,而序列的每个状态都是必须要知道的 很麻烦,所以考虑如何把一段区间表示地简单一 ... 
