1087 All Roads Lead to Rome (30 分)(最短路径)

直接用Dijkstra做
#include<bits/stdc++.h> using namespace std;
int n,m;
map<string,int>si;
map<int,string>is;
const int N=;
int weight[N];
int mp[N][N];
const int inf=0x3f3f3f3f;
int dis[N];
bool vis[N];
int num[N];
int w[N];
int past[N];
int path[N];
void Dijkstra(int v)
{
fill(vis,vis+N,false);
fill(dis,dis+N,inf);
fill(w,w+N,);
fill(num,num+N,);
fill(past,past+N,);
fill(path,path+N,);
//for(int i=0;i<=n-1;i++) dis[i]=mp[v][i];
dis[v]=;
num[v]=;
for(int i=;i<n;i++){
int u=-;
int minn=inf;
for(int j=;j<n;j++){
if(!vis[j]&&minn>dis[j]){
u=j;
minn=dis[j];
}
}
if(u==-) return;
vis[u]=true;
for(int j=;j<n;j++){
if(!vis[j]&&mp[u][j]!=inf){
if(dis[j]>dis[u]+mp[u][j]){//第一标尺: 最短路径
dis[j]=dis[u]+mp[u][j];
num[j]=num[u];
w[j]=w[u]+weight[j];
past[j]=past[u]+;//s->j顶点个数等于s->u顶点个数+1
path[j]=u;//更新路径
}
else if(dis[j]==dis[u]+mp[u][j]){
num[j]+=num[u];
if(w[u]+weight[j]>w[j]){//第二标尺:结点的值
w[j]=w[u]+weight[j];
past[j]=past[u]+;
path[j]=u;
}
else if(w[u]+weight[j]==w[j]){
double uAvg=1.0*(w[u]+weight[j])/(past[u]+);
double vAvg=1.0*w[j]/past[j];
if(uAvg>vAvg){//第三标尺:平均结点值
past[j]=past[u]+;
path[j]=u;
}
}
}
}
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie();
cout.tie();
fill(mp[],mp[]+N*N,inf);
fill(weight,weight+N,);
cin>>n>>m;
string t;
cin>>t;
si[t]=;
is[]=t;
for(int i=;i<=n-;i++){
string temp;
cin>>temp;
cin>>weight[i];
si[temp]=i;
is[i]=temp;
}
for(int i=;i<m;i++){
string a,b;
cin>>a>>b;
int num;
cin>>num;
int a1=si[a];
int b1=si[b];
mp[a1][b1]=mp[b1][a1]=num;
}
Dijkstra();
int rom=si["ROM"];
cout<<num[rom]<<" "<<dis[rom]<<" "<<w[rom]<<" "<<w[rom]/past[rom]<<endl;
int tt=rom;
vector<int>vec;
while(tt!=){
vec.push_back(tt);
tt=path[tt];
}
reverse(vec.begin(),vec.end());
cout<<t<<"->";
for(int i=;i<vec.size();i++){
if(i) cout<<"->";
cout<<is[vec[i]];
}
cout<<endl;
return ;
}
用Dijkstra + dfs
这样做会更好理解
#include<bits/stdc++.h> using namespace std;
int n,m;
map<string,int>si;
map<int,string>is;
const int N=;
int weight[N];
int mp[N][N];
const int inf=0x3f3f3f3f;
int dis[N];
bool vis[N];
vector<int>path[N]; void Dijkstra(int v)
{
fill(vis,vis+N,false);
fill(dis,dis+N,inf);
for(int i=;i<n;i++) dis[i]=mp[v][i];
dis[v]=;
for(int i=;i<n;i++){
int u=-;
int minn=inf;
for(int j=;j<n;j++){
if(!vis[j]&&minn>dis[j]){
minn=dis[j];
u=j;
}
}
if(u==-) return;
vis[u]=true;
for(int j=;j<n;j++){
if(!vis[j]&&mp[u][j]!=inf){
if(dis[j]>mp[u][j]+dis[u]){
dis[j]=mp[u][j]+dis[u];
path[j].clear();
path[j].push_back(u);
}
else if(dis[j]==mp[u][j]+dis[u]){
path[j].push_back(u);
}
}
}
}
}
vector<int>t,tt;
int minDis=inf;
int maxValue=-;
int sum=;
int num=;
void dfs(int v)
{
if(v==){
sum++;
t.push_back(v);
int sumDis=;
int sumValue=;
sumValue=weight[t[t.size()-]];
for(int i=t.size()-;i>=;i--){
// sumDis+=mp[t[i+1]][t[i]];
sumValue+=weight[t[i]];
}
//注意这里不再需要管最短路径这个第一标尺 因为我们用Dijkstra求出来了
// 这几条路径
//的长度是一样的并且是都是最小的
if(maxValue<sumValue){
maxValue=sumValue;
tt=t;
}
else if(sumValue==maxValue){
double a=1.0*sumValue/tt.size();
double b=1.0*maxValue/t.size();
if(a<b){
tt=t;
}
} t.pop_back();
return;
}
t.push_back(v);
for(int i=;i<path[v].size();i++){
dfs(path[v][i]);
}
t.pop_back();
} int main()
{
ios::sync_with_stdio(false);
cin.tie();
cout.tie();
fill(mp[],mp[]+N*N,inf);
fill(weight,weight+N,);
cin>>n>>m;
string t;
cin>>t;
si[t]=;
is[]=t;
for(int i=;i<=n-;i++){
string temp;
cin>>temp;
cin>>weight[i];
si[temp]=i;
is[i]=temp;
}
for(int i=;i<m;i++){
string a,b;
cin>>a>>b;
int num;
cin>>num;
int a1=si[a];
int b1=si[b];
mp[a1][b1]=mp[b1][a1]=num;
}
Dijkstra();
int rom=si["ROM"];
dfs(rom);
cout<<sum<<" "<<dis[rom]<<" "<<maxValue<<" "<<maxValue/(tt.size()-)<<endl;
reverse(tt.begin(),tt.end());
for(int i=;i<tt.size();i++){
if(i) cout<<"->";
cout<<is[tt[i]];
}
cout<<endl;
return ;
}
1087 All Roads Lead to Rome (30 分)(最短路径)的更多相关文章
- PAT甲级练习 1087 All Roads Lead to Rome (30分) 字符串hash + dijkstra
		
题目分析: 这题我在写的时候在PTA提交能过但是在牛客网就WA了一个点,先写一下思路留个坑 这题的简单来说就是需要找一条最短路->最开心->点最少(平均幸福指数自然就高了),由于本题给出的 ...
 - 【PAT甲级】1087 All Roads Lead to Rome (30 分)(dijkstra+dfs或dijkstra+记录路径)
		
题意: 输入两个正整数N和K(2<=N<=200),代表城市的数量和道路的数量.接着输入起点城市的名称(所有城市的名字均用三个大写字母表示),接着输入N-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 ...
 - 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 ...
 - 1087. All Roads Lead to Rome (30)
		
时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Indeed there are many different ...
 - 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[图论][迪杰斯特拉+dfs]
		
1087 All Roads Lead to Rome (30)(30 分) Indeed there are many different tourist routes from our city ...
 - pat1087. All Roads Lead to Rome (30)
		
1087. All Roads Lead to Rome (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
 - 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 ...
 - PAT甲级1087. All Roads Lead to Rome
		
PAT甲级1087. All Roads Lead to Rome 题意: 确实有从我们这个城市到罗马的不同的旅游线路.您应该以最低的成本找到您的客户的路线,同时获得最大的幸福. 输入规格: 每个输入 ...
 
随机推荐
- Redis-cluster详解
			
redis集群结构 特点: 1 所有redis节点(包括主和从)彼此互联(两两通信),底层使用内部的二进制传输协议,优化传输速度;(所有功能特点的基础) 2 集群中也有主从,也有高可用的 ...
 - Python常用模块之json、pickle、random、hashlib、collections
			
1.json和pickle json用于字符串和Python数据类型间进行转换pickle用于python特有的类型和python的数据类型间进行转换json和pickle均提供了四种方法dumps, ...
 - iOS | XIB简单应用和注意点
			
2018开篇第一篇文章,本文分享一点关于XIB的小知识,对于iOS开发新人来说或许有用. XIB 是 Interface Builder 的图形界面设计文档. 从Xcode 3.0 开始,苹果提供Xi ...
 - vue+nodejs+express+mysql 建立一个在线网盘程序
			
vue+nodejs+express+mysql 建立一个在线网盘程序 目录 vue+nodejs+express+mysql 建立一个在线网盘程序 第一章 开发环境准备 1.1 开发所用工具简介 1 ...
 - SpringMVC中session使用&&拦截器&&乱码处理&&异常处理
			
### 1. 使用Session 通常,会在Session中存放: 1. 客户端(用户)的身份标识,通常是用户的id:2. 使用频率非常高的数据,例如显示在页面中的用户名.头像等:3. 其它的不便于使 ...
 - This system is registered to Red Hat Subscription Management, but is not receiving updates. You can use subscription-manager to assign subscriptions.
			
Wrong date and time, reset the date and time in the system properly. It may also happen that system ...
 - 【解决】MacOS下 Python3.7 使用 pyinstaller 打包后执行报错 Failed to execute script pyi_rth__tkinter
			
Fix tcl/tk libs inclusion in tkinter with Python3.7 under MacOS 使用 Pyinstaller 打包时候报错 3027 ERROR: Tc ...
 - Ubuntu 16.04 Server 版安装过程图文详解
			
进入系统安装的第一个界面,开始系统的安装操作.每一步的操作,左下角都会提示操作方式!! 1.选择系统语言-English 2.选择操作-Install Ubuntu Server 3.选择安装过程和系 ...
 - SI - 硬件 - 服务器 - 知识科普
			
服务器对每个从事IT工作的人来说并不陌生,但是服务器所涉及的各种知识细节,并非大家都十分清楚,为了让大家深入了解服务器的关键知识点,笔者特意抽时间总结了这篇科普文章,旨在帮助读者全面了解服务器.今天内 ...
 - rhel7-Samba服务搭建
			
服务检查: [root@localhost ~]# systemctl status smb.service● smb.service - Samba SMB Daemon Loaded: loa ...