1087. All Roads Lead to Rome (30)

时间限制
200 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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)的更多相关文章

  1. [图的遍历&多标准] 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 ...

  2. PAT1087. All Roads Lead to Rome

    PAT1087. All Roads Lead to Rome 题目大意 给定一个图的边权和点权, 求边权最小的路径; 若边权相同, 求点权最大; 若点权相同, 则求平均点权最大. 思路 先通过 Di ...

  3. 1087. All Roads Lead to Rome (30)

    时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Indeed there are many different ...

  4. 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 ...

  5. PAT甲级练习 1087 All Roads Lead to Rome (30分) 字符串hash + dijkstra

    题目分析: 这题我在写的时候在PTA提交能过但是在牛客网就WA了一个点,先写一下思路留个坑 这题的简单来说就是需要找一条最短路->最开心->点最少(平均幸福指数自然就高了),由于本题给出的 ...

  6. PAT (Advanced Level) 1087. All Roads Lead to Rome (30)

    暴力DFS. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...

  7. 【PAT甲级】1087 All Roads Lead to Rome (30 分)(dijkstra+dfs或dijkstra+记录路径)

    题意: 输入两个正整数N和K(2<=N<=200),代表城市的数量和道路的数量.接着输入起点城市的名称(所有城市的名字均用三个大写字母表示),接着输入N-1行每行包括一个城市的名字和到达该 ...

  8. 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 ...

  9. PAT_A1087#All Roads Lead to Rome

    Source: PAT A1087 All Roads Lead to Rome (30 分) Description: Indeed there are many different tourist ...

随机推荐

  1. 21天网站建设实录 (雨辰资讯) 高清pdf扫描版​

    <21天网站建设实录>以网页设计师的项目开发为背景,以“阿里里在线购物”商业网站的开发过程为流程,通过21天的任务期限,以一天一项任务.一天掌握一项技能项目实战的学习模式,全面讲解了一个网 ...

  2. Unobrusive Ajax使用

    mark一下:[ASP.NET MVC 小牛之路]14 - Unobtrusive Ajax篇文章,果断记下来,网址: http://www.cnblogs.com/willick/p/3418517 ...

  3. JavaScript面向切面编程入门

    来源极客网学习视频 关键词Javascript AOP编程 例子1: function test() { alert(2); } //理解,所谓的传入一个"回调",该怎样设计bef ...

  4. sqlserver的索引创建

    随着系统数据的增多,一些查询逐渐变慢,这时候我们可以根据sqlserver的执行计划,查看sql的开销,然后根据开销创建索引. 索引有聚集索引与非聚集索引. 聚集索引:聚集索引在存储上是按照顺序存储的 ...

  5. Glib学习笔记(一)

    你将学到什么 如何使用GObject实现一个新类 类头文件 声明一个类型的方法选择取决于类型是可被继承的还是不可被继承的. 不可被继承的类型(Final类型)使用G_DECLARE_FINAL_TYP ...

  6. kuangbin专题16H(next数组)

    题目链接: https://vjudge.net/contest/70325#problem/H 题意: 输入字符串 str, 求 str 子串中既是 str 前缀又是 str 后缀的的字符串长度, ...

  7. Oracle基本函数即字段拆分

    --创建用户 CREATE USER jim IDENTIFIED BY changeit; --给用户赋登陆连接权限 GRANT CONNECT TO jim; --给用户赋资源权限 GRANT R ...

  8. P3369 【模板】普通平衡树(权值线段树)

    原来线段树还有这种操作(开成一个桶) 用区间维护在这个区间内元素的个数,离散化一下,居然能达到splay的效果 不仅码量大大减少,而且跑的飞快!!! 6种操作  200多ms 插入 xx 数 删除 x ...

  9. kuangbin专题十二 POJ1661 Help Jimmy (dp)

    Help Jimmy Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14214   Accepted: 4729 Descr ...

  10. HashMap 1.8的源码分析三

    线程安全问题: 在添加时候并没有进行安全考虑,枷锁 所以是线程不安全的,接下来进行代码测试; package com.mmall.concurrency.example.commonUnsafe; i ...