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

PTA 1030 Travel Plan

个人学习记录,代码难免不尽人意。

A traveler’s map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a program to help a traveler to decide the shortest path between his/her starting city and the destination. If such a shortest path is not unique, you are supposed to output the one with the minimum cost, which is guaranteed to be unique.

Input Specification:
Each input file contains one test case. Each case starts with a line containing 4 positive integers N, M, S, and D, where N (≤500) is the number of cities (and hence the cities are numbered from 0 to N−1); M is the number of highways; S and D are the starting and the destination cities, respectively. Then M lines follow, each provides the information of a highway, in the format:

City1 City2 Distance Cost

where the numbers are all integers no more than 500, and are separated by a space.

Output Specification:
For each test case, print in one line the cities along the shortest path from the starting point to the destination, followed by the total distance and the total cost of the path. The numbers must be separated by a space and there must be no extra space at the end of output.

Sample Input:
4 5 0 3
0 1 1 20
1 3 2 30
0 3 4 10
0 2 2 20
2 3 1 20
Sample Output:
0 2 3 3 40

#include<cstdio>
#include<algorithm>
using namespace std;
int N,M,C1,C2;
const int maxn=510;const int INF=1000000000;
bool vis[maxn]={false};
int d[maxn],c[maxn],pre[maxn];
struct node{int d;int cost;
};
node Node[maxn][maxn];
void dijkstra(){fill(d,d+maxn,INF);fill(c,c+maxn,INF);for(int i=0;i<N;i++) pre[i]=i;d[C1]=0;c[C1]=0;for(int i=0;i<N;i++){int u=-1;int min=INF;for(int j=0;j<N;j++){if(vis[j]==false&&d[j]<min){u=j;min=d[j];}}if(u==-1) return;vis[u]=true;for(int v=0;v<N;v++){if(vis[v]==false&&Node[u][v].d!=0){if(d[v]>Node[u][v].d+d[u]){d[v]=Node[u][v].d+d[u];c[v]=Node[u][v].cost+c[u];pre[v]=u;}else if(d[v]==Node[u][v].d+d[u]){if(c[v]>Node[u][v].cost+c[u]){c[v]=Node[u][v].cost+c[u];pre[v]=u;}}}}}
}
void DFS(int s){if(s==C1){printf("%d",s);return;}else{DFS(pre[s]);printf(" %d",s);}
}
int main(){scanf("%d%d%d%d",&N,&M,&C1,&C2);for(int i=0;i<M;i++){int cost,d;int c1,c2;scanf("%d%d%d%d",&c1,&c2,&d,&cost);node* n=new node;n->cost=cost;n->d=d;Node[c1][c2]=Node[c2][c1]=*n;} dijkstra();DFS(C2);printf(" %d %d\n",d[C2],c[C2]);
} 

本题参考了《算法笔记》上面的dijkstra算法,对于求解这一类的最小值问题可以将dijkstra算法的模板背过,然后根据题意修改其内容即可。
除了上面这种做法还可以将第二类距离的求解从dijkstra算法中剥离出来,采用DFS方法来处理,比较简单,我觉得两种方法掌握一种即可。

http://www.lryc.cn/news/109209.html

相关文章:

  • MFC、Qt、WPF?该用哪个?
  • 使用logback记录日志
  • 企业工程项目管理系统源码(三控:进度组织、质量安全、预算资金成本、二平台:招采、设计管理) em
  • 【安装】XMind2022XMind2020安装教程(资源)
  • Windows下QT Creator安装MinGW 32bit编译器
  • Emacs之解决键值绑定冲突问题(一百二十三)
  • 瞄准产业应用,大模型加持的深兰科技AI虚拟数字人落地业务场景
  • 【网络基础进阶之路】基于MGRE多点协议的实战详解
  • Spark、RDD、Hive 、Hadoop-Hive 和传统关系型数据库区别
  • [运维]python 启用http 文件服务
  • electron-builder 打包 exe 异常错误集锦
  • 14-5_Qt 5.9 C++开发指南_基于HTTP 协议的网络应用程序
  • Kotlin委托
  • 分布式协议与算法——CAP理论、ACID理论、BASE理论
  • 接口测试 Jmeter 接口测试 —— 请求 Headers 与传参方式
  • 【redis】redis部署1主2从3哨兵demo搭建示例
  • C++数据结构之平衡二叉搜索树(一)——AVL的实现(zig-zag/左右双旋/3+4重构)
  • 免疫疗法勘察兵——DC细胞
  • Django实现音乐网站 ⑷
  • 2023年华数杯数学建模C题思路 - 母亲身心健康对婴儿成长的影响
  • openGauss学习笔记-30 openGauss 高级数据管理-别名
  • C#实现多线程局域网扫描器的思路与具体代码
  • Redis秒杀:一人一单问题及初步解决
  • python 数据分析面试题:求分组排第n名的记录数据
  • eclipse常用快捷键
  • 什么是OCR?OCR技术详解
  • 【大模型】开源且可商用的大模型通义千问-7B(Qwen-7B)来了
  • SQL分类及通用语法数据类型
  • 亿欧智库:2023中国功效型护肤产品成分解析研究报告(附下载
  • Kubernetes高可用集群二进制部署(一)主机准备和负载均衡器安装