网络探测

2024-05-30 19:48
文章标签 网络 探测

本文主要是介绍网络探测,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Problem Description
When the network runs into trouble, we often use the command “ping” to test whether the computer is well connected to others.
For example, if we want to test whether our computer is well connected to the host of Zhongshan University, we would use the command “ping www.zsu.edu.cn”. Then if the network works well, we would get some replies such as:
Reply from 202.116.64.9: bytes=32 time=1ms TTL=126
Reply from 202.116.64.9: bytes=32 time<1ms TTL=126
But how can we get a reply with the whole information above, especially the time used? As follows are some details about the “Ping” process, and please note that this explanation is for this problem only and may be different from the actual “Ping” command:
First of all, there are two kinds of message related to this “Ping” process, the echo request and echo reply. 
The echo request message contains a TimeElapsed field to record the time used since it’s sent out from the source. We can assume that in the network, each host (router, switcher, computer, and so on) is so “polite” that if the incoming message does not aim to itself, then it will update the TimeElapsed field in the message with the time used to transfer the message from the direct sender to itself, and send the message to all the other hosts that is connected to them directly. So after we send out an echo request packet, the network can “automatically” transfer it to the target host. 
Once the target host receives an echo request message, it replies with an echo reply message. The echo reply message also contains a TimeElapsed field, which is filled by the target host using the TimeElapsed field in the echo request message, and will not be changed by those intermediate computers. Then the network “automatically” transfers the reply to us again. That’s why we know whether the network is well connected or not, and the shortest time it takes to transfer a message from our computer to the target host. 
Actually, there is still one problem with the “Ping” process above. Suppose intermediate computers A and B are directly connected to each other, and a message aiming to C reaches A, then A will transfer the message to B since A is “polite”, and then B will send the message back to A again since B is “polite” too, then A and B are trapped into an infinite loop and keep on sending message to each other. To solve this problem, a host would throw away an incoming message if the message has been transferred for more than ten hops (Each time a host A sends a message to another host B is called one hop).
The problem is, given the details of the network, what is the reply time we will get. Please note that this reply time will equal to the shortest time to transfer a message from the source to the target host if possible.
Input
The input will contain multiple test cases. In each test case, the first line is three integers n (n<=1000), m and t (0<=t<n), n is the number of hosts in the network (including our computer), m is the number of pairs of directly connected computers, and t is the target host that we would like to ping. (We always assume our computer to be host 0).
Then there are m lines followed, each of which has three integers a (0<=a<n), b (0<=b<n) and c (0<c<=1000), indicating that host a is directly connected to b, and the time required to transfer a message from a to b or vice versa is equal to c. 
The input will be terminated by a case with n=0, which should not be processed.
Output
For each case, if our computer can get the reply from the target computer, print the reply time in one line. Otherwise print “no”.
Sample Input
3 2 2
0 1 2
1 2 3
3 1 2
0 1 2
0 0 0
Sample Output
5
no
//题意:给出一个无向连通图G,起点是0,终点是t,求不超过10步的最短路径的花费是多少。
//关键字: SPFA + DP
//标程:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
const int inf = 100000000;
struct node
{int v, w;node(int x,int y){v = x;w = y;}
};
vector<node> vec[1010];
int n, m, t, vis[1010], dis[1010][11];
void spfa(int s)
{queue<int> q;while(!q.empty()) q.pop();q.push(s);for(int i = 0; i < n; ++ i)for(int j = 0; j <= 10; ++ j)dis[i][j] = inf;dis[s][0] = 0;memset(vis,0,sizeof(vis));while(!q.empty()){int u = q.front();q.pop();vis[u]=0;for(int i = 0;i < vec[u].size();i++){int v = vec[u][i].v;for(int j = 1; j <= 10; j ++){if(dis[v][j] > dis[u][j-1] + vec[u][i].w){dis[v][j] = dis[u][j-1] + vec[u][i].w;if(!vis[v]){q.push(v);vis[v]=1;}}}}}
}
int  main()
{
//    freopen("a.txt","r",stdin);int  i;while(cin >> n >> m >> t,n+m+t){for(i = 0; i <= n; ++ i) vec[i].clear();int a, b, w;for(i = 1; i <= m; ++ i){cin >> a >> b >> w;vec[a].push_back(node(b,w));vec[b].push_back(node(a,w));}spfa(0);int ans = inf;for(i = 0; i <= 10; ++ i)if(ans > dis[t][i])ans = dis[t][i];if(ans < inf) cout << ans << endl;else cout << "no" << endl;}return 0;
}


                                    

这篇关于网络探测的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1016783

相关文章

Python实现简单封装网络请求的示例详解

《Python实现简单封装网络请求的示例详解》这篇文章主要为大家详细介绍了Python实现简单封装网络请求的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录安装依赖核心功能说明1. 类与方法概览2.NetHelper类初始化参数3.ApiResponse类属性与方法使用实

Debian 13升级后网络转发等功能异常怎么办? 并非错误而是管理机制变更

《Debian13升级后网络转发等功能异常怎么办?并非错误而是管理机制变更》很多朋友反馈,更新到Debian13后网络转发等功能异常,这并非BUG而是Debian13Trixie调整... 日前 Debian 13 Trixie 发布后已经有众多网友升级到新版本,只不过升级后发现某些功能存在异常,例如网络转

Python开发简易网络服务器的示例详解(新手入门)

《Python开发简易网络服务器的示例详解(新手入门)》网络服务器是互联网基础设施的核心组件,它本质上是一个持续运行的程序,负责监听特定端口,本文将使用Python开发一个简单的网络服务器,感兴趣的小... 目录网络服务器基础概念python内置服务器模块1. HTTP服务器模块2. Socket服务器模块

Go语言网络故障诊断与调试技巧

《Go语言网络故障诊断与调试技巧》在分布式系统和微服务架构的浪潮中,网络编程成为系统性能和可靠性的核心支柱,从高并发的API服务到实时通信应用,网络的稳定性直接影响用户体验,本文面向熟悉Go基本语法和... 目录1. 引言2. Go 语言网络编程的优势与特色2.1 简洁高效的标准库2.2 强大的并发模型2.

Linux中压缩、网络传输与系统监控工具的使用完整指南

《Linux中压缩、网络传输与系统监控工具的使用完整指南》在Linux系统管理中,压缩与传输工具是数据备份和远程协作的桥梁,而系统监控工具则是保障服务器稳定运行的眼睛,下面小编就来和大家详细介绍一下它... 目录引言一、压缩与解压:数据存储与传输的优化核心1. zip/unzip:通用压缩格式的便捷操作2.

Linux网络配置之网桥和虚拟网络的配置指南

《Linux网络配置之网桥和虚拟网络的配置指南》这篇文章主要为大家详细介绍了Linux中配置网桥和虚拟网络的相关方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 一、网桥的配置在linux系统中配置一个新的网桥主要涉及以下几个步骤:1.为yum仓库做准备,安装组件epel-re

python如何下载网络文件到本地指定文件夹

《python如何下载网络文件到本地指定文件夹》这篇文章主要为大家详细介绍了python如何实现下载网络文件到本地指定文件夹,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下...  在python中下载文件到本地指定文件夹可以通过以下步骤实现,使用requests库处理HTTP请求,并结合o

Linux高并发场景下的网络参数调优实战指南

《Linux高并发场景下的网络参数调优实战指南》在高并发网络服务场景中,Linux内核的默认网络参数往往无法满足需求,导致性能瓶颈、连接超时甚至服务崩溃,本文基于真实案例分析,从参数解读、问题诊断到优... 目录一、问题背景:当并发连接遇上性能瓶颈1.1 案例环境1.2 初始参数分析二、深度诊断:连接状态与

Qt实现网络数据解析的方法总结

《Qt实现网络数据解析的方法总结》在Qt中解析网络数据通常涉及接收原始字节流,并将其转换为有意义的应用层数据,这篇文章为大家介绍了详细步骤和示例,感兴趣的小伙伴可以了解下... 目录1. 网络数据接收2. 缓冲区管理(处理粘包/拆包)3. 常见数据格式解析3.1 jsON解析3.2 XML解析3.3 自定义

Linux系统配置NAT网络模式的详细步骤(附图文)

《Linux系统配置NAT网络模式的详细步骤(附图文)》本文详细指导如何在VMware环境下配置NAT网络模式,包括设置主机和虚拟机的IP地址、网关,以及针对Linux和Windows系统的具体步骤,... 目录一、配置NAT网络模式二、设置虚拟机交换机网关2.1 打开虚拟机2.2 管理员授权2.3 设置子