首页 > 其他分享 >day57-graph theory-part07-8.28

day57-graph theory-part07-8.28

时间:2024-08-28 19:22:53浏览次数:5  
标签:__ theory weight graph father range edges 8.28

tasks for today:

1. prim算法 53.寻宝

2. kruskal算法 53.寻宝

----------------------------------------------------------------------------

1. prim算法 53.寻宝

In this practice, we see how prim algorithm is used. The essence of this practice is: there are n points, m edges, select n-1 edges from the m edges, making the total weight of the edges are minimized. n-1 edges can link all n points.

This practice is led by the 3-step structure of prim algorithm.

need to pay attention to the 10001, which is the 1 + 10000, 10000 is the maximum points number in this practice's configuration;

please be noted that the graph's entries assignment, graph[x][y] and graph[y][x] should be assigned with the same weight.

def main():
    v, e = map(int, input().split())
    graph = [[10001] * (v+1) for _ in range(v+1)]
    for _ in range(e):
        x, y, w = map(int, input().split())
        graph[x][y] = w
        graph[y][x] = w
    
    visited = [False] * (v+1)
    minDis = [10001] * (v+1)
    
    for i in range(1, v+1):
        min_Val = 10002
        cur = -1

        # step 1
        for j in range(1, v+1):
            if visited[j] == False and minDis[j] < min_Val:
                cur = j
                min_Val = minDis[j]

        # step 2
        visited[cur] = True

        # step 3
        for k in range(1, v+1):
            if visited[k] == False and minDis[k] > graph[cur][k]:
                minDis[k] = graph[cur][k]
                
    res = 0
    for i in range(2, v+1):
        res += minDis[i]
    
    print(res)
    
    return

if __name__ == "__main__":
    main()

2. kruskal算法

Based on the same practice, the kruskai algorithm is discussed here. The difference is that, the prim algo is maintaining a set of points, whereas the kruskai maintains a set of edges.

This method may involve using the method discussed in day 55 & 56.

class Edge:
    def __init__(self, l, r, weight):
        self.l = l
        self.r = r
        self.weight = weight

def find(u, father):
    if father[u] == u:
        return u
    else:
        return find(father[u], father)

def join(u, v, father):
    u = find(u, father)
    v = find(v, father)
    if u == v: return
    father[v] = u

def isSame(u, v, father):
    u = find(u, father)
    v = find(v, father)
    return u == v

def main():
    v, e = map(int, input().split())
    edges = []
    for _ in range(e):
        x, y, w = map(int, input().split())
        edges.append(Edge(x, y, w))
    edges.sort(key = lambda edge: edge.weight)
    
    father = list(range(v+1))
    result = 0
    for i in range(e):
        if not isSame(edges[i].l, edges[i].r, father):
            result += edges[i].weight
            join(edges[i].l, edges[i].r, father)
    
    print(result)
    
    return

if __name__ == "__main__":
    main()

标签:__,theory,weight,graph,father,range,edges,8.28
From: https://blog.csdn.net/bbrruunnoo/article/details/141643350

相关文章

  • bnds 8.28
    csp模拟赛。A.暴力枚举就行。B.中序遍历,然后就变为了给定一个序列\(p\),求最少修改几次能让\(p\)变的单调递增,并且满足\(p_i-p_j\gei-j(i>j)\),变换一下就是\(p_i-i\gep_j-j\),所以中序遍历完了之后\(p_i\)减去\(i\),后答案即为\(ans-lis\)。#include......
  • 题解 [ABC199F] Graph Smoothing(中文/English)
    本题解提供英文版,位于示例代码之后。Englishversionofthiseditorialisprovidedafterthesamplecode.设行向量:\[A^{(k)}=\begin{bmatrix}a_1^{(k)}&a_2^{(k)}&\cdots&a_n^{(k)}\end{bmatrix}\]表示\(k\)次操作后每个节点点权的期望。特别地,\(A^{(0)}\)表......
  • 8.28 模拟赛
    比赛复盘浏览所有题后发现所有题都是普及难度。A。数据范围这么小,暴力DP就行。不对\(10^{40}\)的答案……要高精度!!尝试了vector写高精乘发现异常简单。B。一年前我就能不看题解独立切。很快写完了。我清晰地记着分数加分数时分子分母要开__int128。C。又是小\({\Omega......
  • CMake构建学习笔记8-OpenSceneGraph库的构建
    1.概论在连续构建了zlib、libpng、libjpeg、libtiff、giflib以及freetype这几个库之后,接下来我们就要来一个大的,构建OpenSceneGraph这样大型库。OpenSceneGraph(简称OSG)是一个高性能、跨平台的三维图形应用程序框架,广泛应用于科学可视化、模拟仿真、游戏开发等领域。理论上来说,......
  • C# split big picture into small pieces via graphics
    usingSystem;usingSystem.Collections.Generic;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows;usingSystem.Windows.Controls;usingSystem.Windows.Data;usingSystem.Windows.Documents;using......
  • 探索 graphrag-local-ollama:项目优势及实战应用
    目录引言一、项目背景与意义二、项目核心特性与优势三、详细的安装与使用步骤1.环境准备2.安装ollama3.下载所需模型4.克隆项目并安装依赖5.数据准备与初始化6.配置与构建索引7.执行查询四、项目的应用场景与未来展望结语引言在当今科技飞速发展的时代,人工......
  • 题解:UVA1479 Graph and Queries
    分析先看删边操作。由于并不保证是森林,所以我们没有好的方法来在线维护删边相关的操作。所以,我们可以套路地把询问离线,然后倒着操作。删边变成加边。需要注意的是权值的修改,记录时要把当前权值和修改的权值反过来。然后我们发现这个操作很经典,维护方式和[HNOI2012]永无乡......
  • Graphics2D绘图方法总结
    一、简介在开发中可能会遇到这样一类场景,业务复杂度不算太高,技术难度不算太深,但是做起来就很容易把人整破防,伤害很高侮辱性很强的:绘图。绘图最怕有人挑刺:这里变形,那里不对,全图失真。最近在处理这样一个场景,使用Java的Graphics2D类,绘制业务需要的图形模板,然后在具体流程中填充数......
  • 帮助我们从曲线图中获取数据的软件分享——GetData Graph Digitizer
    在科技论文写作和数据分析过程中,我们常常需要将自己的数据与前人的研究成果进行对比。然而,有时我们只能从别人的论文中获得一张包含坐标轴的曲线图,而无法直接获取原始数据。在这种情况下,GetDataGraphDigitizer软件就显得尤为重要。今天,我将详细介绍这款软件,帮助大家轻松......
  • Neo-GNNs: Neighborhood Overlap-aware Graph Neural Networks for Link Prediction
    目录概符号说明MotivationNeo-GNN代码Neo-GNNs:Neighborhoodoverlap-awaregraphneuralnetworksforlinkprediction.NeurIPS,2021.概一种计算上相对高效的,同时利用结构信息和特征信息的链接预测模型.符号说明\(\mathcal{G}=(\mathcal{V},\mathcal{E})\),gra......