热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

07图6旅游规划(25分)

有了一张自驾旅游路线图,你会知道城市间的高速公路长度、以及该公路要收取的过路费。现在需要你写一个程序,帮助前来咨询的游客找一条出发地和目的地之间的最短路

有了一张自驾旅游路线图,你会知道城市间的高速公路长度、以及该公路要收取的过路费。现在需要你写一个程序,帮助前来咨询的游客找一条出发地和目的地之间的最短路径。如果有若干条路径都是最短的,那么需要输出最便宜的一条路径。

输入格式:

输入说明:输入数据的第1行给出4个正整数N、M、S、D,其中N(2N500)是城市的个数,顺便假设城市的编号为0~(N1);M是高速公路的条数;S是出发地的城市编号;D是目的地的城市编号。随后的M行中,每行给出一条高速公路的信息,分别是:城市1、城市2、高速公路长度、收费额,中间用空格分开,数字均为整数且不超过500。输入保证解的存在。

输出格式:

在一行里输出路径的长度和收费总额,数字间以空格分隔,输出结尾不能有多余空格。

输入样例:

4 5 0 3
0 1 1 20
1 3 2 30
0 3 4 10
0 2 2 20
2 3 1 20

输出样例:

3 40


我的答案

1 #include
2 #include
3 #include
4
5 #define ERROR -1
6 #define false 0
7 #define true 1
8 #define MaxVertexNum 100
9 #define INFINITY 65535
10 typedef int Vertex;
11 typedef int WeightType;
12 typedef char DataType;
13 typedef int bool;
14
15
16 typedef struct ENode *PtrToENode;
17 struct ENode {
18 Vertex V1, V2;
19 WeightType Weight;
20 WeightType Cost;
21 };
22 typedef PtrToENode Edge;
23
24 typedef struct GNode *PtrToGNode;
25 struct GNode {
26 int Nv;
27 int Ne;
28 WeightType G[MaxVertexNum][MaxVertexNum];
29 WeightType C[MaxVertexNum][MaxVertexNum];
30 };
31 typedef PtrToGNode MGraph;
32
33 MGraph CreateGraph(int VertexNum);
34 void InsertEdge(MGraph Graph, Edge E);
35 MGraph BuildGraph(int Nv, int Ne);
36 void PrintGraph(MGraph Graph);
37 Vertex FindMinDist(MGraph Graph, int dist[], int collected[]);
38 bool Dijkstra(MGraph Graph, int dist[], int path[], int cost[], Vertex S);
39 void PrintDist(MGraph Graph, int dist[]);
40 void PrintPath(int path[], int N);
41
42 MGraph CreateGraph(int VertexNum)
43 {
44 Vertex V, W;
45 MGraph Graph;
46
47 Graph = (MGraph)malloc(sizeof(struct GNode));
48 Graph->Nv = VertexNum;
49 Graph->Ne = 0;
50
51 for(V=0;VNv;V++)
52 for(W=0;WNv;W++) {
53 Graph->G[V][W] = INFINITY;
54 Graph->C[V][W] = INFINITY;
55 }
56
57 return Graph;
58 }
59
60 void InsertEdge(MGraph Graph, Edge E)
61 {
62 Graph->G[E->V1][E->V2] = E->Weight;
63 Graph->G[E->V2][E->V1] = E->Weight;
64 Graph->C[E->V1][E->V2] = E->Cost;
65 Graph->C[E->V2][E->V1] = E->Cost;
66 }
67
68 MGraph BuildGraph(int Nv, int Ne)
69 {
70 MGraph Graph;
71 Edge E;
72 int i;
73
74 // scanf("%d", &Nv);
75 Graph = CreateGraph(Nv);
76
77 Graph->Ne = Ne;
78 // scanf("%d", &(Graph->Ne));
79 if(Graph->Ne != 0) {
80 E = (Edge)malloc(sizeof(struct ENode));
81 for(i=0;iNe;i++) {
82 scanf("%d %d %d %d\n", &E->V1, &E->V2, &E->Weight, &E->Cost);
83 InsertEdge(Graph, E);
84 }
85 }
86
87 return Graph;
88 }
89
90 void PrintGraph(MGraph Graph)
91 {
92 Vertex V, W;
93 printf("Graph:\n");
94 for(V=0;VNv;V++) {
95 for(W=0;WNv;W++)
96 printf("[%5d %5d]\t" , Graph->G[V][W], Graph->C[V][W]);
97 printf("\n");
98 }
99 printf("-----------------------\n");
100 }
101
102 Vertex FindMinDist(MGraph Graph, int dist[], int collected[])
103 {
104 Vertex MinV, V;
105 int MinDist = INFINITY;
106
107 for(V=0;VNv;V++) {
108 if(collected[V] == false && dist[V]//未被访问并且距离最小
109 MinDist = dist[V];
110 MinV = V;
111 }
112 }
113 if(MinDist < INFINITY)
114 return MinV;
115 else return ERROR;
116 }
117
118 bool Dijkstra(MGraph Graph, int dist[], int path[], int cost[], Vertex S)
119 {
120 int collected[MaxVertexNum];
121 Vertex V, W;
122
123 /* 初始化&#xff1a;此处默认邻接矩阵中不存在的边用INFINITY表示 */
124 for(V&#61;0;VNv;V&#43;&#43;) {
125 dist[V] &#61; Graph->G[S][V];
126 cost[V] &#61; Graph->C[S][V];
127 if(dist[V]<INFINITY)
128 path[V] &#61; S;
129 else
130 path[V] &#61; -1;
131 collected[V] &#61; false;
132 }
133 // PrintPath(path, Graph->Nv);
134 /* 先将起点收入集合 */
135 dist[S] &#61; 0;
136 collected[S] &#61; true;
137
138 while(1) {
139 /* V &#61; 未被收录顶点中dist最小者 */
140 V &#61; FindMinDist(Graph, dist, collected);
141 // printf("[FindMinDist] V:%d\n", V);
142 if(V &#61;&#61; ERROR) /* 若这样的V不存在 */
143 break; /* 算法结束 */
144 collected[V] &#61; true; /* 收录 */
145 for(W&#61;0;WNv;W&#43;&#43;) { /* 对图中的每个顶点W */
146 /* 若W是V的邻接点并且未被收录 */
147 if(collected[W]&#61;&#61;false && Graph->G[V][W]<INFINITY) {
148 if(Graph->G[V][W]<0) /* 若有负边 */
149 return false; /* 不能正确解决&#xff0c;返回错误标记 */
150 /* 若收录V使得dist[W]变小&#xff08;两点距离比一点距离短&#xff09; */
151 if(dist[V]&#43;Graph->G[V][W]<dist[W]) {
152 dist[W] &#61; dist[V] &#43; Graph->G[V][W]; /* 更新dist[W] */
153 // printf("[FindMinDist] dist[W(%d)]&#61;%d\n", W, dist[W]);
154 path[W] &#61; V; /* 更新S到W的路径 */
155 cost[W] &#61; cost[V] &#43; Graph->C[V][W];
156 } else if((dist[V]&#43;Graph->G[V][W] &#61;&#61; dist[W])
157 && (cost[V] &#43; Graph->C[V][W] < cost[W])) {
158 cost[W] &#61; cost[V] &#43; Graph->C[V][W];
159 }
160 }
161 }
162 }
163
164 return true; /* 算法执行完毕&#xff0c;返回正确标记 */
165 }
166
167 void PrintDist(MGraph Graph, int dist[])
168 {
169 Vertex V;
170 for(V&#61;0;VNv;V&#43;&#43;) {
171 printf("%d ", dist[V]);
172 }
173 printf("\n");
174 }
175
176 void PrintPath(int path[], int N)
177 {
178 int i;
179 printf("[Path] ");
180 for(i&#61;0;i)
181 printf("%d ",path[i]);
182 printf("\n");
183 }
184
185 int main()
186 {
187 int N, M;
188 Vertex S, D;
189 int *dist, *path, *cost;
190 MGraph Graph;
191 scanf("%d %d %d %d\n", &N, &M, &S, &D);
192 dist &#61; (int *)malloc(sizeof(int)*N);
193 path &#61; (int *)malloc(sizeof(int)*N);
194 cost &#61; (int *)malloc(sizeof(int)*N);
195 Graph &#61; BuildGraph(N, M);
196 // PrintGraph(Graph);
197 Dijkstra(Graph, dist, path, cost, S);
198 // PrintPath(path, N);
199 printf("%d %d\n", dist[D], cost[D]);
200 return 0;
201 }

 


转:https://www.cnblogs.com/ch122633/p/9001396.html



推荐阅读
  • 本实验主要探讨了二叉排序树(BST)的基本操作,包括创建、查找和删除节点。通过具体实例和代码实现,详细介绍了如何使用递归和非递归方法进行关键字查找,并展示了删除特定节点后的树结构变化。 ... [详细]
  • UNP 第9章:主机名与地址转换
    本章探讨了用于在主机名和数值地址之间进行转换的函数,如gethostbyname和gethostbyaddr。此外,还介绍了getservbyname和getservbyport函数,用于在服务器名和端口号之间进行转换。 ... [详细]
  • 本题涉及一棵由N个节点组成的树(共有N-1条边),初始时所有节点均为白色。题目要求处理两种操作:一是改变某个节点的颜色(从白变黑或从黑变白);二是查询从根节点到指定节点路径上的第一个黑色节点,若无则输出-1。 ... [详细]
  • MySQL索引详解与优化
    本文深入探讨了MySQL中的索引机制,包括索引的基本概念、优势与劣势、分类及其实现原理,并详细介绍了索引的使用场景和优化技巧。通过具体示例,帮助读者更好地理解和应用索引以提升数据库性能。 ... [详细]
  • Explore how Matterverse is redefining the metaverse experience, creating immersive and meaningful virtual environments that foster genuine connections and economic opportunities. ... [详细]
  • 题目描述:给定n个半开区间[a, b),要求使用两个互不重叠的记录器,求最多可以记录多少个区间。解决方案采用贪心算法,通过排序和遍历实现最优解。 ... [详细]
  • CentOS7源码编译安装MySQL5.6
    2019独角兽企业重金招聘Python工程师标准一、先在cmake官网下个最新的cmake源码包cmake官网:https:www.cmake.org如此时最新 ... [详细]
  • 在金融和会计领域,准确无误地填写票据和结算凭证至关重要。这些文件不仅是支付结算和现金收付的重要依据,还直接关系到交易的安全性和准确性。本文介绍了一种使用C语言实现小写金额转换为大写金额的方法,确保数据的标准化和规范化。 ... [详细]
  • 扫描线三巨头 hdu1928hdu 1255  hdu 1542 [POJ 1151]
    学习链接:http:blog.csdn.netlwt36articledetails48908031学习扫描线主要学习的是一种扫描的思想,后期可以求解很 ... [详细]
  • This document outlines the recommended naming conventions for HTML attributes in Fast Components, focusing on readability and consistency with existing standards. ... [详细]
  • Splay Tree 区间操作优化
    本文详细介绍了使用Splay Tree进行区间操作的实现方法,包括插入、删除、修改、翻转和求和等操作。通过这些操作,可以高效地处理动态序列问题,并且代码实现具有一定的挑战性,有助于编程能力的提升。 ... [详细]
  • 2023年京东Android面试真题解析与经验分享
    本文由一位拥有6年Android开发经验的工程师撰写,详细解析了京东面试中常见的技术问题。涵盖引用传递、Handler机制、ListView优化、多线程控制及ANR处理等核心知识点。 ... [详细]
  • 使用Vultr云服务器和Namesilo域名搭建个人网站
    本文详细介绍了如何通过Vultr云服务器和Namesilo域名搭建一个功能齐全的个人网站,包括购买、配置服务器以及绑定域名的具体步骤。文章还提供了详细的命令行操作指南,帮助读者顺利完成建站过程。 ... [详细]
  • 本文详细介绍了C语言中链表的两种动态创建方法——头插法和尾插法,包括具体的实现代码和运行示例。通过这些内容,读者可以更好地理解和掌握链表的基本操作。 ... [详细]
  • 本文详细探讨了VxWorks操作系统中双向链表和环形缓冲区的实现原理及使用方法,通过具体示例代码加深理解。 ... [详细]
author-avatar
果博东方手机版
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有