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 题意: 确实有从我们这个城市到罗马的不同的旅游线路.您应该以最低的成本找到您的客户的路线,同时获得最大的幸福. 输入规格: 每个输入 ...
随机推荐
- 【luogu P2746 [USACO5.3]校园网Network of Schools】 题解
题目链接:https://www.luogu.org/problemnew/show/P2812 注意:判断出入度是否为0的时候枚举只需到颜色的数量. 坑点:当只有一个强连通分量时,不需要再添加新边. ...
- ListItem Updating事件监视有没有上传附件
using System; using System.Collections.Generic; using System.Text; using Microsoft.SharePoint; using ...
- 使用transfor让图片旋转
材料:Transform,onmouseout,onmouseover css: html: js:
- TIDB2 —— 三篇文章了解 TiDB 技术内幕 - 说存储
原文地址:https://pingcap.com/blog-cn/tidb-internal-1/ 引言 数据库.操作系统和编译器并称为三大系统,可以说是整个计算机软件的基石.其中数据库更靠近应用层, ...
- js省市区级联选择联动
<!DOCTYPE html> <html lang="zh-cn"> <head> <meta http-equiv="Con ...
- ABAP术语-EDI (Electronic Data Interchange)
EDI (Electronic Data Interchange) 原文:http://www.cnblogs.com/qiangsheng/archive/2008/01/29/1057386.ht ...
- Maven命令参数
命令参数 备注 mvn -v --version 显示版本信息; mvn -V --show-version 显示版本信息后继续执行Maven其他目标; mvn -h --help 显示帮助信息; m ...
- ExceL按记录导出Txt 工具
根据客户要求,开发此工具,每一条记录改出一个Txt文本,文本名取其中一字段数据
- python3爬虫之开篇
写在前面的话: 折腾爬虫也有一段时间了,从一开始的懵懵懂懂,到现在的有一定基础,对于这一路的跌跌撞撞,个人觉得应该留下一些文字性的东西,毕竟好记性不如烂笔头,而且毕竟这是吃饭的家伙,必须用心对待才可以 ...
- JavaSE基础复习---1---2018/9/27
2018/9/27 JavaSE学习笔记-1 目录: Java的起源 Java语言概述 1.Java的起源 现代编程语言的发展,大致可以理解为,机器码语言---汇编语言---C语言---C++语言-- ...