近期一次pat考试中的最后一题。事实上玩算法这东西就像打魔兽。不能光有思想上的高度,微操必须实打实。就这么个迪杰斯特拉算法。多少教科书上都讲烂了。

可是现场又有多少人是敲对的呢?不能光停留在理解上。必须能用自己的方式表达出来才算过关。

题目:

1087. All Roads Lead to Rome (30)

时间限制
200 ms
内存限制
32000 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

----------------------------------------------------------------------------------------------------------------------------------------------------

题目的意思非常明白,给定图的点和边。起点。终点,求最短路径而且打印。假设有同样的情况,推断总的幸福指数。假设还是同样,推断平均幸福指数。

本题唯一的难点就在于统计同样路径的条数。

我们用数组存储到达每一个点的同样路径的条数。初始值都是1。

实际上就仅仅有两种情况,第一:这个点A被其它点B更新了,那么到达A的同样的最短路径条数就是B点的同样最短路径条数。

第二:起点到A点和B点的长度相等。那么A点的最短路径条数就是A本身的条数加上B点的条数。

// pat-1087.cpp : 定义控制台应用程序的入口点。

//

#include "stdafx.h"

#include"algorithm"
#include"map"
#include"vector"
#include"string"
#include"iostream"
#include"stack"
using namespace std; #define max 300
#define inf 2100000000//!!!
int dis[max][max]={0};
int n=0;
int visited[max]={0};
int father[max]={0};
int same[max]={0};
int happies[max]={0};
int finalhappies[max]={0};
int precnt[max]={0};
map<int,string> cityname;
map<string,int> citynum;
stack<string> ans;
void dijstrak(int s)
{
same[0]=1;
visited[s]=1;
for(int i=0;i<n;i++)
{
int min=inf;
int mark=-1;
for(int j=0;j<n;j++)
{//find min
if(visited[j]==0&&dis[s][j]<min)
{
min=dis[s][j];
mark=j;
}
}
if(mark==-1)
return ;
visited[mark]=1;//marked
for(int k=0;k<n;k++)
{//updata
if(visited[k]==1)
continue;
int raw=dis[s][k];
int another=dis[s][mark]+dis[mark][k];
if(raw>another)
{ same[k]=same[mark];
dis[s][k]=dis[s][mark]+dis[mark][k];
father[k]=mark;
finalhappies[k]=finalhappies[mark]+happies[k];
precnt[k]=precnt[mark]+1;
}
else if(raw==another)
{
same[k]+=same[mark];
if(finalhappies[k]<finalhappies[mark]+happies[k])
{
father[k]=mark;
finalhappies[k]=finalhappies[mark]+happies[k];
precnt[k]=precnt[mark]+1;
}
else if(finalhappies[k]==finalhappies[mark]+happies[k])
{
if(precnt[k]>(precnt[mark]+1))
{
father[k]=mark;
precnt[k]=precnt[mark]+1;
}
}
} } }
} int main()
{
for(int i=0;i<max;i++)
for(int j=0;j<max;j++){
dis[i][j]=inf;
dis[j][i]=inf;
dis[i][i]=inf;
dis[j][j]=inf;
}
int k=0;
string s;
cin>>n>>k>>s;
cityname[0]=s;
string citystr;
int happy=0;
for(int i=1;i<n;i++)
{
cin>>citystr>>happy;
cityname[i]=citystr;
citynum[citystr]=i;
happies[i]=happy;
finalhappies[i]=happy;
precnt[i]=1;
same[i]=1;
}
string city1,city2;
int cost=0;
for(int i=0;i<k;i++){
cin>>city1>>city2>>cost;
int j=citynum[city1];
int b=citynum[city2];
dis[j][b]=cost;
dis[b][j]=cost;
} dijstrak(0); int romanum=citynum["ROM"];
int index=romanum;
string path;
path+=s;//"HZH"
ans.push("ROM");
while(father[index]!=0)
{
string pathcity=cityname[father[index]];
ans.push(pathcity);
index=father[index];
}
while(!ans.empty()){
path+="->";
path+=ans.top();
ans.pop();
}
cout<<same[romanum]<<" "<<dis[0][romanum]<<" "<<finalhappies[romanum]<<" "<<finalhappies[romanum]/precnt[romanum]<<endl;
cout<<path<<endl;
return 0;
}

提交的时候把预编译头

#include "stdafx.h"

去掉就可以。

pat-1087【最短路径】的更多相关文章

  1. PAT 1087 All Roads Lead to Rome

    PAT 1087 All Roads Lead to Rome 题目: Indeed there are many different tourist routes from our city to ...

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

  3. PAT 1087 有多少不同的值(20)(STL-set代码)

    1087 有多少不同的值(20 分) 当自然数 n 依次取 1.2.3.--.N 时,算式 ⌊n/2⌋+⌊n/3⌋+⌊n/5⌋ 有多少个不同的值?(注:⌊x⌋ 为取整函数,表示不超过 x 的最大自然数 ...

  4. PAT 1087 有多少不同的值(20)(STL—set)

    1087 有多少不同的值(20 分) 当自然数 n 依次取 1.2.3.--.N 时,算式 ⌊n/2⌋+⌊n/3⌋+⌊n/5⌋ 有多少个不同的值?(注:⌊x⌋ 为取整函数,表示不超过 x 的最大自然数 ...

  5. PAT 1087 有多少不同的值

    https://pintia.cn/problem-sets/994805260223102976/problems/1038429191091781632 当自然数 n 依次取 1.2.3.…….N ...

  6. PAT 1087【二级最短路】

    二级最短路+二级最短路,就是DP过程吧. 代码稍微注释一些,毕竟贴代码不好.. #include<bits/stdc++.h> using namespace std; typedef l ...

  7. PAT甲级1087. All Roads Lead to Rome

    PAT甲级1087. All Roads Lead to Rome 题意: 确实有从我们这个城市到罗马的不同的旅游线路.您应该以最低的成本找到您的客户的路线,同时获得最大的幸福. 输入规格: 每个输入 ...

  8. PAT乙级:1087 有多少不同的值 (20分)

    PAT乙级:1087 有多少不同的值 (20分) 当自然数 n 依次取 1.2.3.--.N 时,算式 ⌊n/2⌋+⌊n/3⌋+⌊n/5⌋ 有多少个不同的值?(注:⌊x⌋ 为取整函数,表示不超过 x ...

  9. PAT 1003. Emergency (25) dij+增加点权数组和最短路径个数数组

    1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...

  10. PAT A1111 Online Map (30 分)——最短路径,dijkstra

    Input our current position and a destination, an online map can recommend several paths. Now your jo ...

随机推荐

  1. ThinkPHP---TP功能类之联表查询

    [一]介绍 在原生的sql中使用join 语法进行数据的联表查询, 在ThinkPHP里支持联表查询操作,但是可以归纳成两种方式:table方法.join方法 (1)table方法:在TP中对应SQL ...

  2. 15Oracle Database 索引

    Oracle Database 索引 索引 索引的目的是加快查询速度,就像一本数据的目录一样.建立索引的原则:非常少的DML操作:经常出现在where语句中的字段 2.20.1.建立索引 l  对t_ ...

  3. 使用lombok注解@Getter @Setter方法代码编译成功,但是没有生成get,set方法

    现象描述: 在对应类对象中,添加lombok的@Getter或@Setter注解,编译没有问题,但是在使用类对象时,没有出现对应的get或set方法. 配置且编译ok,但是没有对应的get或set方法 ...

  4. 【转载】文件上传命令rz和下载命令sz的安装

    一.xshell工具简介 Xshell 是一个强大的安全终端模拟软件,它支持SSH1, SSH2, 以及Microsoft Windows 平台的TELNET 协议.其可以在Windows界面下用来访 ...

  5. 数字图像处理技术在TWaver可视化中的应用

    数字图像处理(Digital Image Processing)又称为计算机图像处理,它是指将图像信号转换成数字信号并利用计算机对其进行处理的过程.常用的图像处理方法有图像增强.复原.编码.压缩等,数 ...

  6. The content of element type "resultMap" must match ...

    mybatis中的mapper文件错误 ①错误原因: <resultMap>标签中需要按照一下顺序编写: <id> <result> <association ...

  7. 记VS2008安装及使用及卸载的艰辛历程!!!(2018/11/6-2018/11/14)

    此文为了纪念我对VS2008“孜孜不倦的”无数次的安装及卸载,以及解决使用过程中出现的问题所花费的人力物力和财力!成功之后再作补充.

  8. 爬虫-----HTML解析

    对HTML的解析: 在解析复杂的HTML的页面时,需要避免一些问题,好让爬虫工作变得得心应手. • 寻找“打印此页”的链接,或者看看网站有没有HTML样式更友好的移动版(把自己 的请求头设置成处于移动 ...

  9. ISO7220M芯片调试总结

    3.3V或者5V供电        速度可以达到150Mbps      有25年的寿命 调试问题总结: 在调试中发现,芯片焊接的时候很容易损坏,甚至350度焊接,时间在5s的时间都会坏掉.当坏掉的时 ...

  10. PM2 & chmod +x

    PM2 https://www.npmjs.com/package/pm2 https://github.com/Unitech/pm2 docs https://pm2.io/doc/en/runt ...