数据结构第13周 :( 迪杰斯特拉最短路径 + 弗洛伊德求最短路径 + 欧拉回路 + Invitation Cards)

news/2024/4/20 1:26:19/文章来源:https://blog.csdn.net/qq_51800570/article/details/129178858

目录

  • 迪杰斯特拉最短路径
  • 弗洛伊德求最短路径
  • 欧拉回路
  • Invitation Cards

迪杰斯特拉最短路径

【问题描述】

在带权有向图G中,给定一个源点v,求从v到G中的其余各顶点的最短路径问题,叫做单源点的最短路径问题。

在常用的单源点最短路径算法中,迪杰斯特拉算法是最为常用的一种,是一种按照路径长度递增的次序产生最短路径的算法。
在本题中,读入一个有向图的带权邻接矩阵(即数组表示),建立有向图并按照以上描述中的算法求出源点至每一个其它顶点的最短路径长度。

【输入形式】

输入的第一行包含2个正整数n和s,表示图中共有n个顶点,且源点为s。其中n不超过50,s小于n。

以后的n行中每行有n个用空格隔开的整数。对于第i行的第j个整数,如果大于0,则表示第i个顶点有指向第j个顶点的有向边,且权值为对应的整数值;如果这个整数为0,则表示没有i指向j的有向边。当i和j相等的时候,保证对应的整数为0。

【输出形式】

只有一行,共有n-1个整数,表示源点至其它每一个顶点的最短路径长度。如果不存在从源点至相应顶点的路径,输出-1。

请注意行尾输出换行。

【样例输入】

4 1
0 3 0 1
0 0 4 0
2 0 0 0
0 0 1 0

【样例输出】

6 4 7

【样例说明】

在本题中,需要按照题目描述中的算法完成迪杰斯特拉算法,并在计算最短路径的过程中将每个顶点是否可达记录下来,直到求出每个可达顶点的最短路径之后,算法才能够结束。

迪杰斯特拉算法的特点是按照路径长度递增的顺序,依次添加下一条长度最短的边,从而不断构造出相应顶点的最短路径。

另外需要注意的是,在本题中为了更方便的表示顶点间的不可达状态,可以使用一个十分大的值作为标记。

#include<iostream>
#define N 50
#define Max 99999
using namespace std;int NodeNum;
int SourceNum;
int R[N][N];
int dist[N];
int path[N];
bool tag[N];void Dijkstra()
{int i = 0;for(i = 0; i < NodeNum; i ++){dist[i] = R[SourceNum][i];if(i == SourceNum){path[i] = -1;tag[i] = false;}else{path[i] = SourceNum;tag[i] = true;}}int cnt = NodeNum - 1;while(cnt --){int Min = Max;int MinId = 0;for(i = 0; i < NodeNum; i ++) //找到未访问的最小邻接顶点,并入,将tag置为零{if(dist[i] < Min && tag[i] == true){Min = dist[i];MinId = i;}}tag[MinId] = false;for(i = 0; i < NodeNum; i ++){if(i == MinId || i == SourceNum) continue;if(R[MinId][i] + dist[MinId] < dist[i]){dist[i] = R[MinId][i] + dist[MinId];path[i] = MinId;}}}for(i = 0; i < NodeNum; i ++){if(i == SourceNum) continue;if(dist[i] == Max) cout<<-1<<" ";elsecout<<dist[i]<<" ";}
}int main()
{cin>>NodeNum>>SourceNum;int i, j;for(i = 0; i < NodeNum; i ++){for(j = 0; j < NodeNum; j ++){int num;cin>>num;if(num == 0)R[i][j] = Max;elseR[i][j] = num;}}Dijkstra();return 0;
}

弗洛伊德求最短路径

【问题描述】

对于下面一张若干个城市,以及城市之间距离的地图,请采用弗洛伊德算法求出所有城市之间的最短路径。
【输入形式】

顶点个数n,以及n*n的邻接矩阵,其中不可达使用9999代替

【输出形式】

每两个顶点之间的最短路径和经过的顶点

注意:顶点自身到自身的dist值为0,path则为该顶点的编号

【样例输入】

4

9999 4 11 9999

6 9999 2 9999

1 9999 9999 1

9999 3 9999 9999

【样例输出】

from 0 to 0: dist = 0 path:0
from 0 to 1: dist = 4 path:0 1
from 0 to 2: dist = 6 path:0 1 2
from 0 to 3: dist = 7 path:0 1 2 3
from 1 to 0: dist = 3 path:1 2 0
from 1 to 1: dist = 0 path:1
from 1 to 2: dist = 2 path:1 2
from 1 to 3: dist = 3 path:1 2 3
from 2 to 0: dist = 1 path:2 0
from 2 to 1: dist = 4 path:2 3 1
from 2 to 2: dist = 0 path:2
from 2 to 3: dist = 1 path:2 3
from 3 to 0: dist = 6 path:3 1 2 0
from 3 to 1: dist = 3 path:3 1
from 3 to 2: dist = 5 path:3 1 2
from 3 to 3: dist = 0 path:3

#include<iostream>
#define N 50
using namespace std;int dist[N][N];
int path[N][N];
int NodeNum;
void Floyd()
{int i, j, k;for(k = 0; k < NodeNum; k ++){for(i = 0; i < NodeNum; i ++){for(j = 0; j < NodeNum; j ++){if(i == j || k == i || k == j) continue;if(dist[i][j] > dist[i][k] + dist[k][j]){dist[i][j] = dist[i][k] + dist[k][j];path[i][j] = path[i][k];}}}}for(i = 0; i < NodeNum; i ++){for(j = 0; j < NodeNum; j ++){cout<<"from "<<i<<" to "<<j<<": dist = ";if(dist[i][j] == 9999)cout<<0;elsecout<<dist[i][j];cout<<" path:";int t = path[i][j];cout<<i<<" ";if(t == j ){if(i != j)cout<<j;}else{cout<<path[i][j]<<" ";while(t != j){cout<<path[t][j]<<" ";t = path[t][j];}}cout<<endl;}}
}int main()
{cin>>NodeNum;int i, j;for(i = 0; i <NodeNum; i ++){for(j = 0; j <NodeNum; j ++){cin>>dist[i][j];path[i][j] = j;}}Floyd();return 0;
}

欧拉回路

【问题描述】

欧拉回路是指不令笔离开纸面,可画过图中每条边仅一次,且可以回到起点的一条回路。现给定一个图,问是否存在欧拉回路?

【输入形式】

测试输入包含若干测试用例。每个测试用例的第1行给出两个正整数,分别是节点数N ( 1 < N < 1000 )和边数M;随后的M行对应M条边,每行给出一对正整数,分别是该条边直接连通的两个节点的编号(节点从1到N编号)。当N为0时输入结束。

【输出形式】

每个测试用例的输出占一行,若欧拉回路存在则输出1,否则输出0。

【样例输入】

3 3

1 2

1 3

2 3

3 2

1 2

2 3

0

【样例输出】

1

0

【备注】

此题是浙大计算机研究生复试上机考试-2008年真题。

#include<iostream>
using namespace std;
int main()
{while(1){int NodeNum;int RelationNum;cin>>NodeNum;if(NodeNum == 0) break;cin>>RelationNum;int In[NodeNum];while(RelationNum --){int n1;int n2;cin>>n1>>n2;In[n1 - 1] ++;In[n2 - 1] ++;}int flag = 1;int i = 0;for(i = 0; i < NodeNum; i ++){if(In[i] % 2 != 0) flag = 0;}if(flag) cout<<1<<endl;else cout<<0<<endl;}return 0;
}

Invitation Cards

【Problem Description】

In the age of television, not many people attend theater performances. Antique Comedians of Malidinesia are aware of this fact. They want to propagate theater and, most of all, Antique Comedies. They have printed invitation cards with all the necessary information and with the programme. A lot of students were hired to distribute these invitations among the people. Each student volunteer has assigned exactly one bus stop and he or she stays there the whole day and gives invitation to people travelling by bus. A special course was taken where students learned how to influence people and what is the difference between influencing and robbery.

The transport system is very special: all lines are unidirectional and connect exactly two stops. Buses leave the originating stop with passangers each half an hour. After reaching the destination stop they return empty to the originating stop, where they wait until the next full half an hour, e.g. X:00 or X:30, where ‘X’ denotes the hour. The fee for transport between two stops is given by special tables and is payable on the spot. The lines are planned in such a way, that each round trip (i.e. a journey starting and finishing at the same stop) passes through a Central Checkpoint Stop (CCS) where each passenger has to pass a thorough check including body scan.

All the ACM student members leave the CCS each morning. Each volunteer is to move to one predetermined stop to invite passengers. There are as many volunteers as stops. At the end of the day, all students travel back to CCS. You are to write a computer program that helps ACM to minimize the amount of money to pay every day for the transport of their employees.

【Input】

The input consists of N cases. The first line of the input contains only positive integer N. Then follow the cases. Each case begins with a line containing exactly two integers P and Q, 1 <= P,Q <= 1000000. P is the number of stops including CCS and Q the number of bus lines. Then there are Q lines, each describing one bus line. Each of the lines contains exactly three numbers - the originating stop, the destination stop and the price. The CCS is designated by number 1. Prices are positive integers the sum of which is smaller than 1000000000. You can also assume it is always possible to get from any stop to any other stop.

【Output】

For each case, print one line containing the minimum amount of money to be paid each day by ACM for the travel costs of its volunteers.

【Sample Input】

2

2 2

1 2 13

2 1 33

4 6

1 2 10

2 1 60

1 3 20

3 4 10

2 4 5

4 1 50

【Sample Output】

46

210

#include<iostream>
#define N 50
#define Max 99999
using namespace std;int dist[N][N];void MinCost(int limit) //limit可取
{int i, j, k; //通过弗洛伊德的三重循环计算出每两个顶点间的最短距离for(k = 1; k <= limit; k ++){for(i = 1; i <= limit; i ++){for(j = 1; j <= limit; j ++){if(i == j || k == i || k == j)continue;if(dist[i][k] + dist[k][j] < dist[i][j]){dist[i][j] = dist[i][k] + dist[k][j];}}}}int res = 0;for(j = 2; j <= limit; j ++){res += dist[1][j];res += dist[j][1];}cout<<res<<endl;
}int main()
{int cnt = 0;cin>>cnt;while(cnt --){int NodeNum;int RelationNum;cin>>NodeNum>>RelationNum;int i, j;for(i = 0; i < N; i ++){for(j = 0; j < N; j ++){dist[i][j] = Max;}}while(RelationNum --){int n1;int n2;int w;cin>>n1>>n2>>w;dist[n1][n2] = w; //从下标一号开始}MinCost(NodeNum);}return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.luyixian.cn/news_show_72941.aspx

如若内容造成侵权/违法违规/事实不符,请联系dt猫网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

部署跨云容灾的五大难点

为什么企业需要跨云容灾&#xff1f; 据统计&#xff0c;全球已有70%的企业使用云计算服务。上云帮助企业更高效地管理数据资产&#xff0c;但它并非绝对安全。如停电、漏水等机房事故&#xff1b;地震、火灾等自然性灾害&#xff1b;亦或是人为失误&#xff0c;都有可能造成数…

视频技术基础知识

一、视频图像基础 像素&#xff1a;图像的基本单元&#xff0c;即一个带有颜色的小块分辨率&#xff1a;图像的大小或尺寸&#xff0c;用像素个数来表示。原始图像分辨率越高&#xff0c;图像就越清晰位深&#xff1a;存储每位像素需要的二进制位数&#xff1b;位深越大&#…

华为OD机试 C++ 实现 - 第 N 个排列

最近更新的博客 华为OD机试 - 入栈出栈(C++) | 附带编码思路 【2023】 华为OD机试 - 箱子之形摆放(C++) | 附带编码思路 【2023】 华为OD机试 - 简易内存池 2(C++) | 附带编码思路 【2023】 华为OD机试 - 第 N 个排列(C++) | 附带编码思路 【2023】 华为OD机试 - 考古…

模电学习7. 三极管特性曲线与静态工作点

模电学习7. 三极管特性曲线与静态工作点一、三极管的伏安特性曲线1. 三极管的伏安特性曲线2. 三极管的静态工作点二、合适的静态工作点选择1. 合适静态工作点条件2. 静态工作点的确定三、使用立创EDA仿真查看静态工作点1. 搭建如下图所示测试电路2. 点击菜单仿真、仿真设置3. 运…

springboot整合springdata jpa全能书

一&#xff1a;spring data jpa介绍 spring data:其实spring data就是spring提供了一个操作数据的框架。而spirng data jpa只是spring data框架下的一个基于jpa标准操作数据的模块。 spring data jpa&#xff1a;基于jpa的标准对数据进行操作。简化操作持久层的代码。只需要编…

【离线数仓-4-数据仓库设计】

离线数仓-4-数据仓库设计离线数仓-4-数据仓库设计1.数据仓库分层规划2.数据仓库构建流程1.数据调研1.业务调研2.需求分析3.总结2.明确数据域3.构建业务总线矩阵&维度模型设计4.明确统计指标1.指标体系相关概念1.原子指标2.派生指标3.衍生指标2.指标体系对于数仓建模的意义5…

儿童全脑九大能力,3-6岁的家长都应该知道

什么是全脑&#xff1f; 人的大脑分左右两个半球&#xff0c;形态虽然相似&#xff0c;功能却各有不同。其中&#xff0c;左脑负责文字、数学、计算、分析、逻辑、顺序、事实和记忆&#xff0c;掌管右侧肢体的感觉和运动&#xff1b;右脑则负责颜色、音乐、想象、韵律、感觉、…

其它 Composition API

1.shallowReactive 与 shallowRef shallowReactive&#xff1a;只处理对象最外层属性的响应式&#xff08;浅响应式&#xff09;。 shallowRef&#xff1a;只处理基本数据类型的响应式, 不进行对象的响应式处理。 什么时候使用? 如果有一个对象数据&#xff0c;结构比较深, …

vue-print-nb使用

下载 pnpm add vue-print-nb --save 全局注册&#xff0c;使用插件的注册方式 或 局部注册自定义指令 import print from vue-print-nb directives: {print } 绑定到点击按钮上 <button v-print"content">Print!</button> 设置配置项-常用 id和popTi…

总结:NodeJS

一、介绍Nodejs就像是Java中的JVM&#xff0c;是js的运行环境。nodejs不是一个js框架&#xff0c;千万不要认为是类似jquery的框架。nodejs的作用和jvm的一样一样的&#xff0c;也是js的运行环境&#xff0c;不管你是什么操作系统&#xff0c;只要安装对应版本的nodejs&#xf…

华为OD机试真题 用 C++ 实现 - 字符串加密 | 多看题,提高通过率

最近更新的博客 华为OD机试 - 入栈出栈(C++) | 附带编码思路 【2023】 华为OD机试 - 箱子之形摆放(C++) | 附带编码思路 【2023】 华为OD机试 - 简易内存池 2(C++) | 附带编码思路 【2023】 华为OD机试 - 第 N 个排列(C++) | 附带编码思路 【2023】 华为OD机试 - 考古…

angular

1. angular获取不到DOM结点 angular中的ngOnInit钩子函数获取不到DOM节点&#xff1b; 这个钩子函数中&#xff0c;表示组件和指令初始化完成&#xff0c;并不是真正的DOM加载完成&#xff1b; 所以这时候需要利用另外一个钩子函数ngAfterViewInit()&#xff0c;是在视图加载完…

界面组件Kendo UI for Angular——让网格数据信息显示更全面

Kendo UI致力于新的开发&#xff0c;来满足不断变化的需求&#xff0c;通过React框架的Kendo UI JavaScript封装来支持React Javascript框架。Kendo UI for Angular是专用于Angular开发的专业级Angular组件&#xff0c;telerik致力于提供纯粹的高性能Angular UI组件&#xff0c…

Leetcode之消失的数字轮转数组

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录一、消失的数字一、消失的数字 二、旋转数组 提示&#xff1a;以下是本篇文章正文内容&#xff0c;下面案例可供参考 一、消失的数字 这题找出消失的一个数字&#…

(二十三)、实现评论功能(3)【uniapp+uinicloud多用户社区博客实战项目(完整开发文档-从零到完整项目)】

1&#xff0c;删除评论的样式和实现逻辑 1.1 添加删除评论的样式 在comment-item组件中&#xff1a; <view class"username">{{giveName(item)}}<text class"iconfont icon-a-43-guanbi" click.stop"delComment"></text><…

【总结】python3启动web服务引发的一系列问题

背景 在某行的实施项目&#xff0c;需要使用python3环境运行某些py脚本。 由于行内交付的机器已自带python3 &#xff0c;没有采取自行安装python3&#xff0c;但是运行python脚本时报没有tornado module。 错误信息 ModuleNotFoundError&#xff1a;No module named ‘torn…

计算机网络第3章(数据链路层)学习笔记

❤ 作者主页&#xff1a;欢迎来到我的技术博客&#x1f60e; ❀ 个人介绍&#xff1a;大家好&#xff0c;本人热衷于Java后端开发&#xff0c;欢迎来交流学习哦&#xff01;(&#xffe3;▽&#xffe3;)~* &#x1f34a; 如果文章对您有帮助&#xff0c;记得关注、点赞、收藏、…

JVM面试总结

文章目录栈帧中存放的信息&#xff1a;对象的创建过程对象的内存布局&#xff1f;对象的访问定位方式&#xff1f;如何判断对象已死&#xff1f;可以作为GC Root的点&#xff1a;谈一下引用对象再被回收时如何逃脱&#xff1f;回收方法区如何判断常量是否废弃&#xff1f;垃圾回…

Redis的安装部署和配置文件的修改

1、准备安装环境 由于 Redis 是基于 C 语言编写的&#xff0c;因此首先需要安装 Redis 所需要的依赖&#xff1a; yum install -y gcc tcl gcc-c make 2、上传安装文件 将下载好的 redis-6.2.7.tar.gz 安装包上传到虚拟机的任意目录&#xff08;一般推荐上传到 /usr/local/s…

Mysql 索引(三)—— 不同索引的创建方式(主键索引、普通索引、唯一键索引)

了解了主键索引的底层原理&#xff0c;主键索引其实就是根据主键字段建立相关的数据结构&#xff08;B树&#xff09;&#xff0c;此后在使用主键字段作为条件查询时&#xff0c;会直接根据主键查找B树的叶子结点。除了主键索引外&#xff0c;普通索引和唯一键索引也是如此&…