python 最短路径

摘要:
贾格尔在节点1,宝藏在节点n。当他运行最短路径算法以找到宝藏的最短路径时,他突然发现,除了他的起始节点和宝藏的位置之外,每个节点都有一个怪物。当且仅当x≥ 例如,Jagger可以通过节点u。Jagger现在想知道最小x是多少,这样他就可以找到从1到n的路径。Jaggerfindsamapt指示了大量推理机的位置,并希望找到这些推理机。用户可以将多个位置作为节点,并将多个链接链接到两个位置。这两个节点和多个链接位于节点1,推理机位于节点1。当运行最短路径算法时,他发现每个节点都没有超出起始节点和区域位置的距离≥例如,Jaggerisnow想知道从1到输入的最小值可能有多个测试用例。第一行包含一个整数T,表示测试用例的数量。建议您尝试处理不同的测试用例。每个测试用例由多行组成。前一个测试用例:第一行包含两个整数和一个整数。第二行包含一个整数,其中有一个节点的强度2≤我≤n-1.每个外部管道都包含两个整数;vi表示ui和vi之间存在差异。备注:每个测试用例都连接了图形。保证测试用例中的最小值小于要求的105,测试用例中最小值大于要求的2×105。同一对节点之间可能存在多个需求。在没有给定齿轮的情况下,可能存在自润滑。输出输出最小值为1到Jagger。SampleInput1221001244050100012132434SampleOutput1050SampleInput24612067410066413365312226216136514147808773307335155671661307349031246156514231611412363635109067855808848084468851269821093873SampleOutput20308的想法是,它实际上是Dijkstra的应用程序,是否可以将此点添加到集合中需要满足以下要求:x˃=s[u]。

贾格尔(Jagger)找到一张地图,该地图指示大量宝藏的位置,并希望找到它们。
该地图将几个位置标记为节点和几个边缘,这表示两个位置直接相连。 总共有n个节点和m个边。 贾格尔(Jagger)位于节点1,宝物位于节点n。
当他运行最短路径算法以找出通往宝藏的最短路径时,他突然发现除了他的起始节点和宝藏的位置以外,每个节点都有一个怪物。 节点u上的怪物具有力量su。 贾格尔的力量为x。 当且仅当x≥su时,Jagger才能通过节点u。
Jagger现在想知道最小x是多少,以便他可以找到从1到n的路径。

Jagger finds a map that indicates the location of a large number of
treasure and would like to find them.
The map marks several locations as nodes and several edges which indicates
that two locations are connected directly. There are n nodes and m edges in
total. Jagger is located at node 1 and the treasure is located at node n.
When he is running shortest path algorithm in his head to find out the
shortest path to the treasure, he suddenly finds out that every node has a
monster except his starting node and the location of the treasure. A monster at
node u has strength su. Jagger has strength x. Jagger can pass node u if and
only if x su.
Jagger is now wondering what’s the minimum x so that he can find a path
to from 1 to n.

Input
For this problem there are multiple test cases. The first line contains a single
integer T (1 T 100000) indicates the number of test cases. You are
suggested to write a loop to deal with different test cases.
Each test case consists of multiple lines. For each test case:
The first line contains two integer n(1 n 20000) and m(1 m 20000).
The second line contains n integer si(0 si106; s1 = 0; sn = 0) where si is
the strength of the monster at node i for 2 i n - 1.
Each of the next m lines contains two integers ui; vi(1 ui; vin) indicates that there is an edge between ui and vi.
Remark:
• The graph is connected for each testcase.
• It is guaranteed that the sum of all n among the test cases is smaller
than or equal to 105 and the sum of all m among the test cases is smaller
than or equal to 2 × 105.
• There may be multiple edges between same pair of nodes.
• There may be self-loop edges where two end of edge are same.
Output
Output minimum x so that there is path from 1 to n for Jagger.
Sample Input 1
2
2 1
0 0
1 2
4 4
0 50 100 0
1 2
1 3
2 4
3 4
Sample Output 1
0
50
Sample Input 2
4
6 12
0 6 7 4 10 0
6 4
1 3
3 6
5 3
1 2
2 2
6 2
1 6
1 3
6 5
1 4
1 4
7 8
0 8 7 7 3 3 0
7 3
3 5
1 5
5 2
3 5
6 7
4 7
1 6
6 13
0 7 3 4 9 0
3 1
2 4
6 1
5 6
5 1
4 2
3 1
6 1
1 4
1 2
3 6
3 6
3 5
10 9
0 6 7 8 5 5 8 8 4 0
8 4
6 8
8 5
1 2
6 9
8 2
10 9
3 8
7 3
Sample Output 2
0

3

0

8

思路:其实就是迪杰斯特拉的应用,只是稍微需要修改一下更新条件,即该点(u)能不能加入集合需要满足:x >= s[u]。

def djst(sour, cost, n, m, s, x):
    distance = [99999999] + [cost[sour][i] for i in range(1, n+1)]
    distance[sour] = 0
    used = [False for _ in range(1+n)]
    used[sour] = True
    # print(distance)
    max_x = max(s) + 1
    # x = 8
    # while x <= max_x:
    while True:
        v = -1
        # 从未使用过的顶点中选择一个距离最小的顶点
        for u in range(1, 1+n): # x >= s[u] and
            if x >= s[u] and not used[u] and (v == -1 or distance[u] < distance[v]):
                # print(u)
                v = u
        if v == -1:
            break
        # 将选定的顶点加入到S中, 同时进行距离更新
        used[v] = True
        # 更新U中各个顶点到起点s的距离。之所以更新U中顶点的距离,是由于上一步中确定了k是求出最短路径的顶点,从而可以利用k来更新其它顶点的距离;例如,(s,v)的距离可能大于(s,k)+(k,v)的距离。
        for u in range(1, 1+n):
            if x >= s[u]:
                distance[u] = min(distance[u], distance[v] + cost[v][u])
    if distance[n] != 99999999:
        return True
    else:
        return False

t = eval(input())
for i in range(t): # 样例数量
    x = list(map(int, input().split()))
    n, m = x[0], x[1]  # 获取节点数量和边数
    s = list(map(int, input().split())) # 读入怪物限制
    cost = [[99999999] * (n+1) for i in range(n+1)] # 初始化每个点间的距离
    for j in range(m): # 读边
        temp = list(map(int, input().split()))
        a, b = temp[0], temp[1]
        # print(a, b)
        cost[a][b] = cost[b][a] = 0
    # print(cost)
    s = [0] + s
    max_x = max(s) + 1
    for x in range(max_x+1):
        r = djst(1, cost, n, m, s, x)
        if r:
            print(x)
            break

  

免责声明:文章转载自《python 最短路径》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇[Git] 谷歌的代码管理Android播放音乐时跳动的屏谱demo下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

python集成安装环境——Anaconda 3.5的安装

一、下载并安装 anaconda 先到https://www.continuum.io/downloads 下载anaconda, 现在的版本有python2.7版本和python3.5版本,下载好对应版本、对应系统的anaconda,它实际上是一个sh脚本文件,大约280M左右。 本系列以windows7+python3.5为例,因此我们下载如下图红框里...

windows下设置GPU加速tensorflow运算(GT1050)

一、自己的环境 操作系统:win10   GPU:GTX1050+CUDA9.0+cuDNN    IDE:Pycharm    框架:tensorflow-gpu    解释器:Python3.6(强烈建议anaconda3,又方便又强大,下载太慢可以找镜像) 二、安装顺序 1、首先安装CUDA9.0。下载地址:https://developer.nvi...

Python用27行代码绘制一幅满天星

前言 每一个孩子都像星空中的一颗星星,散发着自己所特有的光芒照亮着整个夜空。今天就带大家用27行Python代码绘制一幅满天星吧。 全局设置 在绘制满天星的过程中要运用到turtle工具,它是Python的标准库,也可以形象的称它为海龟库,它可以描绘绘图的轨迹,操作简单、快捷。首先,我们要做一些有关全局的设置 这一步主要是对turtle的画笔大小、绘...

python基础之读取xml

python怎么操作xml文件详细介绍链接:https://www.jb51.net/article/50812.htm 从结构上来说,xml很像常见的HTML超文本标记语言。不过超文本语言被设计用来显示数据,其焦点是数据的外观。xml被设计用来传输和存储数据,其焦点是数据的内容。 特征: 1. 标签对组成:<TEST></TEST>...

Python中Scapy网络嗅探模块的使用

目录 Scapy scapy的安装和使用 发包 发包和收包 抓包 将抓取到的数据包保存 查看抓取到的数据包 格式化输出 过滤抓包 Scapyscapy是python中一个可用于网络嗅探的非常强大的第三方库,可以用它来做 packet 嗅探和伪造 packet。scapy已经在内部实现了大量的网络协议。如DNS、ARP、IP、TCP、UDP等等,可以用它来...

python 读取串口数据常用函数及实例分析

前记: 人生苦短,我用python,python在做一些算法验证和接口验证方面,的确是非常的好用。读取串口经常用到,这里就做个总结,给自己和周围的人做个备忘吧。 函数解析: 初始化串口数据: importserial #Serial takes two parameters: serial device and baudrate ser = serial....