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. Python中的map_reduce

      原教程地址: map/reduce-廖雪峰   将数值型字符串转换成数值,解释map, reduce的使用: #!/usr/bin/env python #-*- coding:utf-8 -*- ...

  2. 为什么选择Angular 2?

    没有选择是痛苦的,有太多的选择却更加痛苦.而后者正是目前前端领域的真实写照.新的框架层出不穷:它难吗?它写得快吗?可维护性怎样?运行性能如何?社区如何?前景怎样?好就业吗?好招人吗?组建团队容易吗? ...

  3. 计算机上配置 IP地址,子网掩码,默认网关

    The Internet Assigned Numbers Authority (IANA) has reserved the following three blocks of the IP add ...

  4. jQuery的ajax实现文件上传大小限制

    用jquery的ajax实现简单的文件上传功能,并且限制文件大小,先上代码. <!DOCTYPE html> <html> <head> <meta char ...

  5. Spring IOC机制之使用注解配置bean

    一. 通过注解配置bean 1.1       概述 相对于XML方式而言,通过注解的方式配置bean更加简洁和优雅,而且和MVC组件化开发的理念十分契合,是开发中常用的使用方式. 1.2       ...

  6. 二分+最小生成树【bzoj2654】: tree

    2654: tree 给你一个无向带权连通图,每条边是黑色或白色.让你求一棵最小权的恰好有need条白色边的生成树. 题目保证有解. 二分答案,然后跑最小生成树判断. 注意优先跑白色边. code: ...

  7. BA 认证

    IIBA-CBAP https://www.iiba.org/standards-and-resources/core-standard/ PMI-PBA https://www.pmi.org/ce ...

  8. sublime侧边栏管理sidebarEnhancements浏览器设置

    sidebarEnhancements是为了增强sublime Text侧边栏功能的一个插件,但是同时也可以实现设置浏览器浏览当前文件的功能. Ctrl+Shift+p 输入package contr ...

  9. 【Leetcode】Remove Duplicates from Sorted List II

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  10. AD属性常量类

    参考:http://www.selfadsi.org/user-attributes.htm namespace Common { /// <summary> /// AD中的属性,没有出 ...