当前位置:网站首页>minimum spanning tree
minimum spanning tree
2022-07-01 13:15:00 【jigsaw_ zyx】
List of articles
Plain version prim Algorithm
key word : A dense picture , Time complexity O(n^2)
Each time, select a point that is not at the minimum distance of the connected block , Then update the data 
Title Description
Given a n A little bit m Striped Undirected graph , There may be Double edge and self ring , The edge weight may be negative .
Find the sum of tree edge weights of the minimum spanning tree , If the minimum spanning tree does not exist, output impossible.
Given an undirected graph with weights G=(V, E), among V Represents the set of points in the graph ,E Represents a set of edges in a graph ,n=|V|,m=|E|.
from V All of n A vertex and E in n-1 An undirected connected subgraph composed of edges is called G A spanning tree of , The spanning tree in which the sum of the weights of the edges is the smallest is called an undirected graph G The minimum spanning tree of .
Input format
The first line contains two integers n and m.
Next m That's ok , Each line contains three integers u,v,w, Indication point u Sum point v There is a weight of w The edge of .
Output format
All in one line , If there is a minimum spanning tree , Then an integer is output , Represents the sum of tree edge weights of the minimum spanning tree , If the minimum spanning tree does not exist, output impossible.
Data range
1≤n≤5001≤n≤500,
1≤m≤1051≤m≤105,
The absolute values of the edge weights of the edges involved in the graph do not exceed 10000.
sample input :
4 5
1 2 1
1 3 2
1 4 3
2 3 2
3 4 4
sample output :
6
Code
#include<bits/stdc++.h>
using namespace std;
// A dense picture
const int N=510,M=10010,INF=0x3f3f3f3f;
int n,m;
int g[N][N];
int d[N];
bool v[N];
int prim(){
memset(d,0x3f,sizeof d);
int res=0;
for(int i=0;i<n;i++){
int t=-1;
for(int j=1;j<=n;j++)
if(!v[j]&&(t==-1||d[t]<d[j]))
t=j;
if(i&&d[t]==INF) return -1;// Cannot generate minimum spanning tree
v[t]=true;
if(i) res+=d[t];
for(int j=1;j<=n;j++)
d[j]=min(d[j],g[t][j]);// Update distance from point to set
}
return res;
}
int main(){
cin>>n>>m;
memset(g,0x3f,sizeof g);
while(m--){
int u,v,w;
cin>>u>>v>>w;
g[u][v]=g[v][u]=min(g[u][v],w);
}
int t=prim();
if(t==-1) cout<<"impossible"<<endl;
else cout<<t<<endl;
return 0;
}
边栏推荐
- 华为HMS Core携手超图为三维GIS注入新动能
- Three questions about scientific entrepreneurship: timing, pain points and important decisions
- Feign & Eureka & Zuul & Hystrix 流程
- Manage nodejs with NVM (downgrade the high version to the low version)
- 内容审计技术
- C language learning
- Fundamentals of number theory and its code implementation
- Shangtang technology crash: a script written at the time of IPO
- Have you ever encountered the problem that flynk monitors the PostgreSQL database and checkpoints cannot be used
- 我选的热门专业,四年后成了“天坑”
猜你喜欢

Simple two ball loading

一款Flutter版的记事本

硬件开发笔记(九): 硬件开发基本流程,制作一个USB转RS232的模块(八):创建asm1117-3.3V封装库并关联原理图元器件

Terminal identification technology and management technology

Google Earth Engine(GEE)——全球人类居住区网格数据 1975-1990-2000-2014 (P2016)

Look at the sky at dawn and the clouds at dusk, and enjoy the beautiful pictures

Logstash error: cannot reload pipeline, because the existing pipeline is not reloadable

ZABBIX 6.0 source code installation and ha configuration

数据库之MHA高可用集群部署及故障切换

软件测试中功能测试流程
随机推荐
逆向调试入门-PE结构-输入表输出表05/07
Report on the "14th five year plan" and scale prospect prediction of China's laser processing equipment manufacturing industry Ⓢ 2022 ~ 2028
In the next stage of digital transformation, digital twin manufacturer Youyi technology announced that it had completed a financing of more than 300 million yuan
数据库之MHA高可用集群部署及故障切换
Ustime wrote a bug
Report on the current situation and development trend of bidirectional polypropylene composite film industry in the world and China Ⓟ 2022 ~ 2028
ROS2 Foxy depthai_ ROS tutorial
Run PowerShell script prompt "because running script is prohibited on this system" solution
During Oracle CDC data transmission, the CLOB type field will lose its value during update. There is a value before update, but
股票开户要认识谁?实际上网上开户安全么?
Wang Xing's infinite game ushers in the "ultimate" battle
MySQL statistical bill information (Part 2): data import and query
Jenkins+webhooks-多分支参数化构建-
Flow management technology
codeforces -- 4B. Before an Exam
La taille de la pile spécifiée est petite, spécifiée à la sortie 328k
有人碰到过这种情况吗,oracle logminer 同步的时候,clob字段的值丢失
leetcode 322. Coin Change 零钱兑换(中等)
Declare an abstract class vehicle, which contains the private variable numofwheel and the public functions vehicle (int), horn (), setnumofwheel (int) and getnumofwheel (). Subclass mot
Asp.netcore利用dynamic简化数据库访问