UVA 125 - Numbering Paths(floyd)

FP16、BF16、INT8、INT4精度模型加载所需显存以及硬件适配的分析 本文主要介绍了FP16、INT8、INT4精度模型加载占用显存大小的分析,希望对学习大语言模型的同学们有所帮助。 文章目录 1. 前言 2. 模型加载显存占用大小 3. 不同精度是否与硬件适配 阅读详情

 Numbering Paths 

Background

Problems that process input and generate a simple ``yes'' or ``no'' answer are called decision problems. One class of decision problems, the NP-complete problems, are not amenable to general efficient solutions. Other problems may be simple as decision problems, but enumerating all possible ``yes'' answers may be very difficult (or at least time-consuming).

This problem involves determining the number of routes available to an emergency vehicle operating in a city of one-way streets.

The Problem

Given the intersections connected by one-way streets in a city, you are to write a program that determines the number of different routes between each intersection. A route is a sequence of one-way streets connecting two intersections.

Intersections are identified by non-negative integers. A one-way street is specified by a pair of intersections. For example, tex2html_wrap_inline30 indicates that there is a one-way street from intersection j to intersection k. Note that two-way streets can be modeled by specifying two one-way streets: tex2html_wrap_inline30 and tex2html_wrap_inline38 .

Consider a city of four intersections connected by the following one-way streets:

    0  1
    0  2
    1  2
    2  3
There is one route from intersection 0 to 1, two routes from 0 to 2 (the routes are tex2html_wrap_inline40 and tex2html_wrap_inline42 ), two routes from 0 to 3, one route from 1 to 2, one route from 1 to 3, one route from 2 to 3, and no other routes.

It is possible for an infinite number of different routes to exist. For example if the intersections above are augmented by the street tex2html_wrap_inline44 , there is still only one route from 0 to 1, but there are infinitely many different routes from 0 to 2. This is because the street from 2 to 3 and back to 2 can be repeated yielding a different sequence of streets and hence a different route. Thus the route tex2html_wrap_inline46 is a different route than tex2html_wrap_inline48 .

The Input

The input is a sequence of city specifications. Each specification begins with the number of one-way streets in the city followed by that many one-way streets given as pairs of intersections. Each pair tex2html_wrap_inline30 represents a one-way street from intersection j to intersection k. In all cities, intersections are numbered sequentially from 0 to the ``largest'' intersection. All integers in the input are separated by whitespace. The input is terminated by end-of-file.

There will never be a one-way street from an intersection to itself. No city will have more than 30 intersections.

The Output

For each city specification, a square matrix of the number of different routes from intersection j to intersection k is printed. If the matrix is denoted M, then M[j][k] is the number of different routes from intersection j to intersection k. The matrix M should be printed in row-major order, one row per line. Each matrix should be preceded by the string ``matrix for city k'' (with k appropriately instantiated, beginning with 0).

If there are an infinite number of different paths between two intersections a -1 should be printed. DO NOTworry about justifying and aligning the output of each matrix. All entries in a row should be separated by whitespace.

Sample Input

7 0 1 0 2 0 4 2 4 2 3 3 1 4 3
5 
0 2 
0 1 1 5 2 5 2 1
9
0 1 0 2 0 3
0 4 1 4 2 1
2 0
3 0
3 1

Sample Output

matrix for city 0
0 4 1 3 2
0 0 0 0 0
0 2 0 2 1
0 1 0 0 0
0 1 0 1 0
matrix for city 1
0 2 1 0 0 3
0 0 0 0 0 1
0 1 0 0 0 2
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
matrix for city 2
-1 -1 -1 -1 -1
0 0 0 0 1
-1 -1 -1 -1 -1
-1 -1 -1 -1 -1
0 0 0 0 0

题意:给定一个图,求点点之间有多少种路径到达,如果有环输出-1.

思路:floyd变形,对于k点 如果f[k][k] != 0,则经过该点会形成环。

代码:

#include <stdio.h>
#include <string.h>
#define max(a,b) (a)>(b)?(a):(b)
const int N = 35;

int m, f[N][N], cas = 0;
int a, b, n;

void init() {
	n = 0;
	memset(f, 0, sizeof(f));
	for (int i = 0; i < m; i ++) {
		scanf("%d%d", &a, &b);
		f[a][b] = 1;
		n = max(max(a, b), n);
	}
}

void floyd() {
	for (int k = 0; k <=n; k ++)
		for (int i = 0; i <= n; i ++)
			for (int j = 0; j <= n; j ++) {
				f[i][j] += f[i][k] * f[k][j];
			}
	for (int k = 0; k <=n; k ++) {
		if (f[k][k])
		for (int i = 0; i <= n; i ++)
			for (int j = 0; j <= n; j ++) {
				if (f[i][k] && f[k][j])
					f[i][j] = -1;
			}
	}
}

void solve() {
	init();
	floyd();
	printf("matrix for city %d\n", cas ++);
	for (int i = 0; i <= n; i ++) {
		for (int j = 0; j < n; j ++)
			printf("%d ", f[i][j]);
		printf("%d\n", f[i][n]);
	}
}

int main() {
	while (~scanf("%d", &m)) {
		solve();
	}
	return 0;
}


知识图谱(Knowledge Graph)原理与代码实战案例讲解 知识图谱(Knowledge Graph)原理与代码实战案例讲解 作者:禅与计算机程序设计艺术 / Zen and the Art of Computer Programming 1. 背景介绍 1.1 问题的由来 阅读详情

相关推荐

如何利用URDF文件表示并联结构(仅适用于支持SDF格式的仿真环境)

介绍了一种使用URDF文件在Gazebo中表示并联结构的方法,该方法使用了URDF的SDF拓展。在一个具有并联结构的双足机器人上应用并演示了此方法的有效性。

RobotFreak的博客 3856

uva 125 Numbering Paths(warshall算法)

uva 125 Numbering PathsDescription Download as PDF Background Problems that process input and generate a simple yes'' orno” answer are called decision problems. One class of decision problems, the N

总有那么一群人喜欢盯着我的博客看,估计这是他们见过最长的博客名,我必须得告诉他们博客名最长可以写多长 724

python数据处理(一)——如何用python实现excel功能

python如何实现excel的数据提取、指标构建,进行透视分析。

一名数据分析师 2715

hdu 1625 Numbering Paths 最短路的变形,使用Floyd 外加判环

Problem Description Problems that process input and generate a simple ``yes'' or ``no'' answer are called decision problems. One class of decision problems, the NP-complete problems, are not amenable to general efficient solutions. Other problems may be si

Lionel 1117

Python Trimesh库实战:5分钟搞定STL文件解析与3D模型可视化

本文介绍了如何使用Python的Trimesh库快速解析STL文件并进行3D模型可视化。通过简洁的代码示例,演示了从环境搭建、模型加载、几何数据提取到基础变换和批量处理的核心流程,帮助开发者和工程师在5分钟内高效完成3D数据处理与可视化任务。

weixin_32005771的博客 163

JAVA:Spring Boot 集成 LibreOffice 实现文档在线转换服务

本文介绍了在企业级应用开发中使用LibreOffice实现文档格式转换的技术方案。通过对比分析商业库、纯Java方案和云服务API的优缺点,重点阐述了LibreOffice在格式兼容性、转换质量和零成本方面的优势。文章详细说明了环境准备步骤,包括Windows/Linux系统安装LibreOffice、解决中文乱码问题以及Docker部署方案。最后提供了Spring Boot项目的基本配置,包括Maven依赖和application.yml配置文件,为构建稳定高效的文档转换服务奠定基础。

木头 529

UVA125 - Numbering Paths(floyd)

UVA125 - Numbering Paths(floyd) UVA125 - Numbering Paths 题目大意:  给m条有方向的边,然后要求你给出N * N的矩阵,矩阵G【i】【j】代表的是i到j之间的总路径数,如果i到j之间存在着环,那么G【i】【j】 = -1. 解题思路:  i到j的路径数目等于i到k乘以k到j(经过k到达的话)。用floyd可以求出i到j

925

热力学仿真软件:ANSYS CFX_(15).旋转机械仿真

创建旋转域在中选择需要设置为旋转域的区域。选择选项。设置旋转速度在设置中,指定旋转速度(单位为弧度/秒)。也可以选择选项,并输入具体值。设置旋转轴在设置中,指定旋转轴的方向和位置。通常旋转轴的方向可以使用单位向量来表示,例如[0, 0, 1]表示沿Z轴旋转。旋转轴的位置可以通过指定一个点的坐标来确定,例如[0, 0, 0]表示旋转轴通过原点。旋转机械的仿真在ANSYS CFX中是一个复杂但重要的过程。

kkchenjj的博客 1160

UVA 125 Numbering Paths

题目大意:给定n条单向边,求图中任意两点的连通路径的数目。其中点是从0-输入中出现的最大的点。 可以用floyd-warshall算法或者dfs. for(int k = 0; k for(int i = 0; i for(int j = 0; j dp[i][j] += dp[i][k] * dp[k][j]; 这里这样写是不会重复的,原因在于k循环时,可能产生重复情况的 点对

rootial的专栏 485

qq邮箱发错误:The “From“ header is missing or invalid. Please follow RFC5322, RFC2047, RFC822 standard pro

如果还报错 (550, b’The mail may contain inappropriate words or content.'),可能是你message[‘From’]中的< senderxxx@qq.com >发送邮箱填错了,必须要和self.sender = ’ senderxxx@qq.com '一样。可能是之前qq邮箱发送协议存在漏洞,可隐藏发送者邮箱。现在换了新协议,导致与原来协议规则、编码出现了一些问题。"python自动发送""python自动发送"

云霄IT的博客 3673

c#上位机最强教程之环境安装

本文介绍了.NET 6开发环境的安装配置指南。主要内容包括:1)安装.NET 6 SDK和Runtime的方法及验证步骤;2)Visual Studio 2022的安装流程;3)创建第一个.NET 6控制台项目;4)项目结构解析和配置文件说明。文章还提供了实践练习和常见问题解答,帮助初学者快速搭建开发环境并运行简单程序。适用于零基础开发者,学习时间约10分钟。

飞上云层 321

UVa 125 - Numbering Paths

題目:已知一些單向道路,求個城市間的路徑有多少種,存在環衛-1(無窮)。 分析:圖論、最短路。利用floyd算法求出路徑條數,然後判斷無窮。             如果從a城市可以到達b城市,並且從b城市也可以到達a城市,則存在環;             再利用floyd更新所有存在環的路徑。 說明:吐槽一下沒有給節點數目(⊙o⊙)… #include #include #inc

小白菜又菜 846

【MATLAB代码】雷达观测目标跟踪模型|H-infinity Filter(H无穷大滤波)与EKF(扩展卡尔曼滤波)的对比。附完整代码,订阅专栏后可直接查看

对比了经典扩展卡尔曼滤波(EKF)与H∞优化的EKF(H∞-EKF)在目标跟踪中的性能。通过MATLAB仿真实验,结果显示:在存在模型误差和测量噪声的情况下,H∞-EKF相比经典EKF在位置误差上降低

985工科博士毕业,专攻定位、导航和滤波等算法研究。从业10年,主要使用MATLAB 247

UVa 125 Numbering Paths / floyd

求i到j有多少条路径 回路输出-1 路径floyd处理 成环的话 判断是否 a[i][i]!= 0是的话在做一次floyd 改成-1 #include #include #include using namespace std; int a[110][110]; int n; void floyd() { int i,j,k; for(k = 0; k <= n; k++) {

嗯。 998

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

UVA - 125 Numbering Paths floyd的变形

题目大意:给出N个城市,要求求出这每个城市之间有多少条道路可走 解题思路:floyd算法的思想是DP思想,用f[i][j]表示i到j的值,这个值可以是多种多样的,比如前面的UVA 10048的噪音题f[i][j]就表示从i到j通过的点的最大值的最小值,通过中间节点k来过度,这题的话f[i][j]就表示从i到j有多少条线路可走,转换方程是,当f[i][k] && f[k][j]时,也就是i到j时通

静静地码 759

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

Numbering Paths - UVa 125 Floyd求环

Numbering Paths Background Problems that process input and generate a simple ``yes'' or ``no'' answer are called decision problems. One class of decision problems, the NP-complete problems, ar

提比的强行AC 566

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

UVa 125 Numbering Paths (路径统计+Floyd)

#include #include #include #include #include #include #include #include #include #include #include using namespace std; const int maxn=30+5; int d[maxn][maxn]; int n,m; void Floyd() { for(int k=0;k

Slow_Wakler的专栏 532

Numbering Paths (Uva 125 floyd+dp思想)

题意:给出一张图,求每两个点之间的不同路径的条数,按照矩阵输出,若i到j有无数条则mp[i][j]=-1. 思路:关键是怎样判断无数条,考虑:若一个点经过一条路可以回来,即mp[i][i]>0,那么它就构成了一个环,我可以在这一无限的绕圈,知道这个了就好办了。另外一条路径经过这个点的话也是无数条。

@you! 651

UVa 125 Numbering Paths

本文研究了有向图中任意两点间路径数量的计算问题。题目要求统计城市交叉路口间通过单向街道形成的路径数,当存在环路时输出-1。采用Floyd-Warshall算法的变种:首先三重循环累积路径数,再检测环路并标记相关路径为无穷大。算法时间复杂度为O(n³),在n≤30时高效可行。代码实现简洁,先构建邻接矩阵,再执行两轮Floyd-Warshall计算,最后输出结果矩阵。该解法巧妙处理了路径计数和环路检测,是图论中路径统计的典型应用。

寂静山林 626

uva 125 - Numbering Paths

<br />//floyd的变形,亦或者说是dp的统计路径?<br />//如何判断回路呢? 我们可以这么想,假设c[k][k]不等于0 那么由k->k之间一定有回路,,那么我再去找经过k点的所有点(因为能否经过k已经<br />//保存).<br /> #include <iostream> #include <cstdio> #include <vector> #include <memory.h> using namespace std; const int MAX=50; bool

流水不争先 1024

125 - Numbering Paths 解题报告

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=61 floyd的题,稍微有点变化,直接上代码: #include #include using namespace std; int main() {     int map[3

zjwoody的专栏 741

uva 125 Numbering Paths

uva 125 Number Paths

@fei 438

UVA - 125 Numbering Paths

题意:求N个点的关系矩阵,矩阵记录的是路径的个数,N是输入中的最大值加一,如果个数存在无限次,打印-1,至于是否存在无限次通过dp[i][i]!=0判断 #include #include #include #include using namespace std; const int MAXN = 30; int n,m,dp[MAXN][MAXN]; void floyd(){

那些年 864

UVa125 - Numbering Paths

题意:一个有向图,统计每队

A Cube Master 722

uva125 Numbering Paths

DP,无耻地搜了解题报告…… DP又卡住了,看了题目就觉得是DP,自己写了一个怎么过不过,另外解决不了环的问题,最后还是看了解题报告 依然是仿照Floyd来DP。i到j经过k,那么i到j的路径数目等于i到k的路径数目乘以k到j的路径数目 DP结束后扫描一遍所有的点,若d[k][k]不为0,说明从k出发能回到k(而题目说了输入不会存在自己到自己的环),那么从k点出发一定存在至少一条回路,d[k...

weixin_34289744的博客 116
上一篇: 【索引】刘汝佳暴力问题
下一篇: UVA 10803 - Thunder Mountain(floyd)
lab104_yifan
博客等级 码龄13年 410粉丝 1478原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址