当前位置:网站首页>K - or unblocked project (minimum spanning tree)
K - or unblocked project (minimum spanning tree)
2022-06-30 15:00:00 【Rabbit doesn't like radish】
Investigation of rural traffic in a province , The resulting table shows the distance between any two villages . provincial government “ Unimpeded works ” Our goal is to achieve road traffic between any two villages in the province ( But it doesn't have to be directly connected by road , As long as it can be reached indirectly by road ), And the total length of the road is required to be the minimum . Please calculate the minimum total length of the highway .
Input
The test input contains several test cases . The... Of each test case 1 Row gives the number of villages N ( < 100 ); And then N(N-1)/2 The row corresponds to the distance between villages , Each line gives a pair of positive integers , They are the numbers of the two villages , And the distance between the two villages . For the sake of simplicity , Village from 1 To N Number .
When N by 0 when , End of input , The use case is not processed .
Output
For each test case , stay 1 The minimum total length of the highway output in the line .
Sample Input
3
1 2 1
1 3 2
2 3 4
4
1 2 1
1 3 4
1 4 1
2 3 3
2 4 2
3 4 5
0
Sample Output
3
5
Huge input, scanf is recommended.
Hint
Hint
// The template questions
#include <cstdio>
#include <cmath>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N=200,MAX=0x3f3f3f3f;
int n;
int from,to,w;
int dist[N],map[N][N];
bool biao[N];// Mark
int prim()
{
memset(dist,0x3f,sizeof(dist));
memset(biao,0,sizeof(biao));
int t=1,sum=0;
dist[1]=0,biao[1]=1;
for(int i=0;i<n-1;i++)
{
for(int j=1;j<=n;j++)
dist[j]=min(dist[j],map[t][j]);
t=-1;
for(int j=1;j<=n;j++)
{
if(!biao[j]&&(t==-1||dist[t]>dist[j]))
t=j;
}
sum+=dist[t];
biao[t]=1;
}
return sum;
}
int main()
{
while(~scanf("%d",&n))
{
if(n==0)
break;
int m=n*(n-1)/2;
memset(map,0x3f,sizeof(map));
while(m--)
{
// cin>>from>>to>>w;
scanf("%d%d%d",&from,&to,&w);
map[from][to]=map[to][from]=min(map[from][to],w);
}
int ans=prim();
cout<<ans<<endl;
}
return 0;
}
Detailed description and explanation of minimum spanning tree
边栏推荐
- jsPlumb. Deleteeveryconnection is not a function & jsplumb clear canvas jsplumb delete all nodes and all connections
- 1151 LCA in a binary tree (30 points)
- 左旋梯形螺纹的编程
- [buuctf] [actf2020 freshman competition]include
- 2021-05-12
- NoViableAltException([email protected][])
- 文本匹配——【NAACL 2021】AugSBERT
- Lost connection to the flow server (0 retries remaining): |Out of retries, exiting! Error reporting solution (flow)
- Quick sort (C language)
- 1058 a+b in Hogwarts (20 points)
猜你喜欢
Lihongyi machine learning 2020 homework summary
V3 03_ Getting started
PS tip: the video frame to Layer command cannot be completed because dynamiclink is not available
CCF call auction (full mark code + problem solving ideas + skill summary) 201412 - 3
Querywrapper in mybaits plus
CCF image rotation (Full Score code + problem solving idea) 201503-01
CCF string matching (Full Score code + problem solving ideas + skill summary) March 3, 2014
CCF sequence segmentation (Full Score code + problem solving idea) 201509 -1
CCF drawing (full mark code + problem solving ideas + skill summary) February 2, 2014
[buuctf] [actf2020 freshman competition]exec1
随机推荐
【BUUCTF】 EasySql
How many questions can you answer for the interview of Mechanical Engineer?
CCF window (Full Score code + problem solving idea) March 2, 2014
CCF string matching (Full Score code + problem solving ideas + skill summary) March 3, 2014
CCF call auction (full mark code + problem solving ideas + skill summary) 201412 - 3
[buuctf] [actf2020 freshman competition]exec1
2021-05-12
Using member variables and member functions of a class
分布式--OpenResty+lua+Redis
Greedy interval problem (5)
Machine learning feature selection
Matlab judge palindrome number (only numbers)
IO interview questions
左旋梯形螺纹的编程
Distributed -- openresty+lua+redis
2021-08-05 leetcode notes
V3_ Chrome extended Chinese translation document V3 directory
Sum of squares of two pointers
1015 reversible primes (20 points)
[extensive reading of papers] analyzing connections between user attributes, images, and text