当前位置:网站首页>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
边栏推荐
- Binary rotation array (1)
- One dimensional and two dimensional array addresses
- 1130: find the first character that appears only once
- jsPlumb. Deleteeveryconnection is not a function & jsplumb clear canvas jsplumb delete all nodes and all connections
- An error is reported when installing dataspherestudio doc: invalid default value for 'update_ time‘
- Component communication mode
- Effect of shadow around the block after mouse over
- Sum of CCF digits (full mark code + problem solving idea) 201512-1
- Solve the problem that codeblocks20.03 on win11 cannot run for the first time
- Average and maximum values of MATLAB matrix
猜你喜欢

Database connection to company database denied

After the MySQL service on the local computer is started and stopped, some services will automatically stop when they are not used by other services or programs

val_ Loss decreases first and then increases or does not decrease but only increases

Determine the number of digits of an integer in MATLAB (one line of code)

Computer screenshot how to cut the mouse in

PS cutting height 1px, Y-axis tiling background image problem

JS to realize simple lottery function
[email protected][])"/>NoViableAltException([email protected][])

CCF date calculation (Full Score code + skill summary) February 2, 2015

CCF access control system (Full Score code + problem solving idea) 201412-1
随机推荐
HD mechanical principle · classic dynamic drawing of mechanical design
[extensive reading of papers] analyzing connections between user attributes, images, and text
[extensive reading of papers] sentimental analysis of online reviews with a hierarchical attention network
Lihongyi machine learning 2020 homework summary
IO interview questions
Minimum covering substring of two pointers
Matlab finds prime numbers within 100
[extensive reading of papers] multimodal attribute extraction
CCF numerical sorting (Full Score code + problem solving ideas + skill summary) 201503-2
CCF window (Full Score code + problem solving idea) March 2, 2014
1058 a+b in Hogwarts (20 points)
Matlab construction operation example
机械工程师面试的几个问题,你能答上来几个?
V3 01_ Welcome
浅析卧式加工中心上不规则台阶孔存在问题
How to realize selective screen recording for EV screen recording
The kth largest element in the sorted array
Lost connection to the flow server (0 retries remaining): |Out of retries, exiting! Error reporting solution (flow)
文本匹配——【NAACL 2021】AugSBERT
1066 root of AVL tree (25 points)