当前位置: 首页 > news >正文

PAT 1087 All Roads Lead to Rome

在这里插入图片描述
在这里插入图片描述
题目大意为:有N个城市(N个点),K个边,起始点的所有的点都有一个幸福值,现在让我们求从开始城市到ROM这座城市的最小花费(也就是最短距离)在最小花费的基础上,保证沿途经过城市的幸福值总和最大,在幸福值最大的基础上找平均幸福值最大的路径。
很明显涉及最短距离,用dijkstra算法来实现,保证沿途经过城市的幸福值总和最大涉及保存最优路径,当有多条最短路径的时候,要找其幸福值最大的路径,因此涉及DFS

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <unordered_map>
#include <limits.h>
#include <queue>
#include <string.h>
using namespace std;
// 123456789
// 105
//
int N;
int K;
string startcity;
struct node
{int cost;int happness;string name;
}city[205];
struct edge
{int w;int to;int next;
}e[40005];
int head[205];
int cut; 
unordered_map<string,int> mp;
void add(int x,int y,int w)
{e[cut].w=w;e[cut].to=y;e[cut].next=head[x];head[x]=cut;cut++;
}
int dist[205];
bool flag[205];
typedef pair<int,int> pii;
priority_queue<pii,vector<pii>,greater<pii> >q;
vector<int> path[205];void dijkstra(int s)
{for(int i=0;i<=N;i++){dist[i]=INT_MAX;}dist[s]=0;q.push({dist[s],s});while(!q.empty()){pii z=q.top();q.pop();int u=z.second;if(flag[u]==0){flag[u]=1;for(int i=head[u];i!=-1;i=e[i].next){int v=e[i].to;if(flag[v]==0&&dist[v]>dist[u]+e[i].w){dist[v]=dist[u]+e[i].w;path[v].clear();path[v].push_back(u);q.push({dist[v],v});}else if(dist[v]==dist[u]+e[i].w){path[v].push_back(u);}}}else{continue;}}}
int ans;
vector<int> anspath;
vector<int> temppath;
int anshappness;
int ansavge;
void dfs(int end,int temphappness)
{if(end==0){ans++;if(temphappness>anshappness){anshappness=temphappness;anspath=temppath;}else if(temphappness==anshappness){double temp=(double)temphappness/(temppath.size()-1);double end=(double)anshappness/(anspath.size()-1);if(temp>end){anspath=temppath;}}return ;}else{for(int i=0;i<path[end].size();i++){temppath.push_back(path[end][i]);dfs(path[end][i],temphappness+city[path[end][i]].happness);temppath.pop_back();}}
}
int main()
{//ios::sync_with_stdio(0),cin.tie(0),cout.tie(cin>>N;cin>>K;cin>>startcity;int cnt=0;mp[startcity]=cnt;city[0].name=startcity; for(int i=1;i<=N-1;i++){cin>>city[i].name>>city[i].happness;cnt++;mp[city[i].name]=cnt;}memset(head,-1,sizeof(head));for(int i=0;i<K;i++){string tempx;cin>>tempx;int x;x=mp[tempx];int y;string tempy;cin>>tempy;y=mp[tempy];int w;cin>>w;add(x,y,w);add(y,x,w);}dijkstra(mp[startcity]);//保存从startcity到ROM的所有路径temppath.push_back(mp["ROM"]);int temphappness=0;	dfs(mp["ROM"],temphappness+city[mp["ROM"]].happness);cout<<ans<<" ";cout<<dist[mp["ROM"]]<<" ";cout<<anshappness<<" ";double ansavgeend=(double)anshappness/(anspath.size()-1);cout<<(int)ansavgeend;cout<<endl;for(int i=anspath.size()-1;i>=0;i--){if(i==0){cout<<city[anspath[i]].name;}else{cout<<city[anspath[i]].name<<"->";}}return 0;} 

我们用vector path[205];保存每一个顶点的最近前缀,在跑dijkstra的过程中可以存储所有的最近前缀。
然后用DFS从终点开始一个个的遍历每一个点,一直到起始点结束,这样就走完了所有的可能的最短路径,在dfs的过程可以找到最优的路径(最小花费(也就是最短距离)在最小花费的基础上,保证沿途经过城市的幸福值总和最大,在幸福值最大的基础上找平均幸福值最大的路径。)
总结:dijkstra+dfs
PAT有一道题是类似的:

PAT 1018 Public Bike Management

思路差不多,我对找所有的符合条件的路径这里掌握不熟。

http://www.dtcms.com/a/353339.html

相关文章:

  • 嵌入式学习资料分享
  • java中的数据类型
  • 《FastAPI零基础入门与进阶实战》第14篇:ORM之第一个案例改善-用户查询
  • 【图文介绍】PCIe 6.0 Retimer板来了!
  • 快速上手对接币安加密货币API
  • 《Linux 网络编程四:TCP 并发服务器:构建模式、原理及关键技术(以select )》
  • 3 无重复字符的最长子串
  • Windows系统之不使用第三方软件查看电脑详细配置信息
  • 基于linux系统的LIRC库学习笔记
  • Ubuntu 的磁盘管理
  • [java] 控制三个线程按顺序交替输出数字1、2、3
  • 【新版发布】Apache DolphinScheduler 3.3.1 正式上线:更稳、更快、更安全!
  • TensorFlow 面试题及详细答案 120道(21-30)-- 模型构建与神经网络
  • 数据结构:创建堆(或者叫“堆化”,Heapify)
  • 增强CD47检查点免疫治疗:高通量发现增强巨噬细胞吞噬作用的小分子协同剂
  • nestjs 连接redis
  • HIVE的Window functions窗口函数【一】
  • 手写题(面试)
  • LeetCode算法日记 - Day 24: 颜色分类、排序数组
  • LeetCode - 155. 最小栈
  • Python Imaging Library (PIL) 全面指南:PIL基础入门-跨平台安装与环境配置
  • Redis 数据结构
  • Linex系统网络管理(二)
  • 【yocto】Yocto Project 核心:深入了解.inc文件
  • Java中使用Spring Boot+Ollama构建本地对话机器人
  • Maven 依赖传递与排除基础逻辑
  • Astah UML 中,状态机(State Machine)的建模最合适使用「UML 状态图(State Diagram)」
  • 轻量级自动驾驶多视图视觉问答模型-EM-VLM4AD
  • 鸿蒙HarmonyOS状态管理装饰器详解
  • perccli 工具