直接用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 分)(最短路径)的更多相关文章

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

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

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

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

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

  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. 1087. All Roads Lead to Rome (30)

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

  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[图论][迪杰斯特拉+dfs]

    1087 All Roads Lead to Rome (30)(30 分) Indeed there are many different tourist routes from our city ...

  8. pat1087. All Roads Lead to Rome (30)

    1087. All Roads Lead to Rome (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...

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

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

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

随机推荐

  1. Angularjs实例4

    <!DOCTYPE html><html lang="zh-cn" ng-app=""><head><meta htt ...

  2. JS JavaScript中的this

    this是JavaScript语言中的一个关键字 它是函数运行时,在函数体内部自动生成的一个对象,只能在函数体内部使用. function test() { this.x = 1; } 上面代码中,函 ...

  3. Redis集群的安装和使用

  4. Openresty最佳案例 | 第8篇:RBAC介绍、sql和redis模块工具类

    转载请标明出处: http://blog.csdn.net/forezp/article/details/78616738 本文出自方志朋的博客 RBAC介绍 RBAC(Role-Based Acce ...

  5. 菜鸟崛起 DB Chapter 3 MySQL 5.6的基本操作

    3   MySQL的基本操作 上面我们学习一如何安装数据库,那么这节我们来认识一下数据库: 我们在MySQL安装后,在data目录下会自动生成几个必须的数据库,可以使用SHOW DATABASES语句 ...

  6. element组件dialog关闭时Message消息提示抖动问题

    在页面内容较多,出现滚动条时使用element组件里的dialog组件,当关闭dialog组件的同时弹出Message消息提示时,Message会抖动一下. 在页面有滚动条的情况先打开dialog时, ...

  7. 如何在linux中创建虚拟环境

    安装虚拟环境的命令 : sudo pip install virtualenv sudo pip install virtualenvwrapper 安装完虚拟环境后,如果提示找不到mkvirtual ...

  8. 利用百度地图API实现地址和经纬度互换查询

    import json import requests def baiduMap(input_para): headers = { 'User-Agent': 'Mozilla/5.0 (Window ...

  9. Mysql 查看连接数,状态,最大并发数

    MySQL: ERROR 1040: Too many connections”的异常情况,造成这种情况的一种原因是访问量过高,MySQL服务器抗不住,这个时候就要考虑增加从服务器分散读压力:另一种原 ...

  10. Python实现多属性排序

    Python实现多属性排序 多属性排序:假如某对象有n个属性,那么先按某规则对属性a进行排序,在属性a相等的情况下再按某规则对属性b进行排序,以此类推. 现有对象Student: class Stud ...