当前位置:网站首页>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
边栏推荐
- [extensive reading of papers] sentimental analysis of online reviews with a hierarchical attention network
- One dimensional and two dimensional array addresses
- [extensive reading of papers] analyzing connections between user attributes, images, and text
- Pseudocode writing specification
- 1137: encrypted medical record
- IO interview questions
- val_ Loss decreases first and then increases or does not decrease but only increases
- Average and maximum values of MATLAB matrix
- Searching for single element in dichotomy
- CCF date calculation (Full Score code + skill summary) February 2, 2015
猜你喜欢

CCF drawing (full mark code + problem solving ideas + skill summary) February 2, 2014

PS dynamic drawing
![[buuctf] [actf2020 freshman competition]exec1](/img/af/22051a5feb3c1f6d7201a483bde127.jpg)
[buuctf] [actf2020 freshman competition]exec1
[email protected][])"/>NoViableAltException([email protected][])

CCF window (Full Score code + problem solving idea) March 2, 2014

LIS error: this configuration section cannot be used in this path

Win10 one click Reset win10 to solve all system bugs without deleting any files and Applications

August 24, 2021 deque queue and stack

Learn about data kinship JSON format design from sqlflow JSON format

val_ Loss decreases first and then increases or does not decrease but only increases
随机推荐
这种零件该怎么编程加工?
Text matching - [naacl 2022] GPL
Greedy interval problem (5)
Matlab construction operation example
Judgment of deep learning experiment results
Average and maximum values of MATLAB matrix
[extensive reading of papers] multimodal joint attribute prediction and value extraction for e-commerce product
1066 root of AVL tree (25 points)
[extensive reading of papers] sentimental analysis of online reviews with a hierarchical attention network
Programming of left-hand trapezoidal thread
@PathVariable
CCF string matching (Full Score code + problem solving ideas + skill summary) March 3, 2014
Programming exercises: special numbers (problem solving ideas + code implementation)
Basic learning notes of C language
1135: paired base chain
Clear the route cache in Vue
K high frequency elements before sorting
Examples of bubble sorting and matrix element screening in MATLAB
CCF elimination games (Full Score code + problem solving ideas + skill summary) February 2, 2015
catkin_ Make reports an error, transfers the location of the workspace, and uses other people's workspace files to cause compilation errors