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 全局搜索路径

    在~/.bachrc中添加 export PYTHONPATH=$PATHONPATH:[需要添加的路径]

  2. hadoop streaming 文档

    Hadoop Streaming框架使用(一) Streaming简介 Streaming框架允许任何程序语言实现的程序在Hadoop MapReduce中使用,方便已有程序向Hadoop平台移植.因 ...

  3. build linaro 4.8 on ubuntu 12.04 64bit

    安装必要的软件 sudo apt-get build-dep gcc binutils gdb sudo apt-get install curl gawk sudo apt-get install ...

  4. wffmpeg64.dll调用 尝试读取或写入受保护的内存。这通常指示其他内存已损坏。

    求解中.....

  5. 利用POI读取word文档实例

    package read.document; import java.io.FileInputStream; import java.io.FileNotFoundException; import ...

  6. javaweb访问hdfs的一些错误

    javaweb 与 HDFS 坑 前提:javaweb 项目,hdfs中的数据文件,导入访问hdfs的jar包,eclipse调试 问题:在×××.java代码中正常访问hdfs,浏览jsp时调用×× ...

  7. 等和的分隔子集(DP)

    晓萌希望将1到N的连续整数组成的集合划分成两个子集合,且保证每个集合的数字和是相等.例如,对于N=3,对应的集合{1,2,3}能被划分成{3} 和 {1,2}两个子集合. 这两个子集合中元素分别的和是 ...

  8. 转载文章 MySQL与Oracle的区别

    MySQL与Oracle的区别   1.  Oracle是大型数据库而Mysql是中小型数据库,Oracle市场占有率达40%,Mysql只有20%左右,同时Mysql是开源的而Oracle价格非常高 ...

  9. Flask 程序的基本结构

    1.初始化 所有Flask程序都必须创建一个程序实例.web服务器使用一种名为Web服务器网关借口的协议,把接收自客户端的所有请求都转交给这个对象处理. from flask import Flask ...

  10. 查询mysql单库的修改时间,大小

      select database_name,max(last_update) from mysql.innodb_table_stats group by database_name;   SELE ...