当前位置:网站首页>N - Is There A Second Way Left? (minimum spanning tree, Kruskal)
N - Is There A Second Way Left? (minimum spanning tree, Kruskal)
2022-06-30 15:01:00 【Rabbit doesn't like radish】
Nasa, being the most talented programmer of his time, can’t think things to be so simple. Recently all
his neighbors have decided to connect themselves over a network (actually all of them want to share
a broadband internet connection ). But he wants to minimize the total cost of cable required as he
is a bit fastidious about the expenditure of the project. For some unknown reasons, he also wants a
second way left. I mean, he wants to know the second best cost (if there is any which may be same as
the best cost) for the project. I am sure, he is capable of solving the problem. But he is very busy with
his private affairs(?) and he will remain so. So, it is your turn to prove yourself a good programmer.
Take the challenge (if you are brave enough)…
Input
Input starts with an integer t ≤ 1000 which denotes the number of test cases to handle. Then follows
t datasets where every dataset starts with a pair of integers v (1 ≤ v ≤ 100) and e (0 ≤ e ≤ 200). v
denotes the number of neighbors and e denotes the number of allowed direct connections among them.
The following e lines contain the description of the allowed direct connections where each line is of the
form ‘start end cost’, where start and end are the two ends of the connection and cost is the cost for
the connection. All connections are bi-directional and there may be multiple connections between two
ends.
Output
There may be three cases in the output
- No way to complete the task,
- There is only one way to complete the task,
- There are more than one way.
Output ‘No way’ for the first case, ‘No second way’ for the second case and an integer c for the
third case where c is the second best cost. Output for a case should start in a new line.
Sample Input
4
5 4
1 2 5
3 2 5
4 2 5
5 4 5
5 3
1 2 5
3 2 5
5 4 5
5 5
1 2 5
3 2 5
4 2 5
5 4 5
4 5 6
1 0
Sample Output
Case #1 : No second way
Case #2 : No way
Case #3 : 21
Case #4 : No second way
The question :
Output three results :
No shortest circuit , Output No way
There is only one output No second way
There are multiple paths to output the second shortest circuit ,
And the o Basically the same , hold o The code of the question has been changed , Just accept 了
#include <cstdio>
#include <cmath>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N=888;
int MAX=0x3f3f3f3f;
int n,m;
int dist[N],map[N][N];
int biao[N];
int sum[N];
int x[N],y[N];
int mm[N];
int pre[N];
struct node{
int u,v,w;
}a[N*(N-1)/2];
bool cmp(node a,node b)
{
return a.w<b.w;
}
int find(int x)
{
if(pre[x]==x)
return x;
else
return pre[x]=find(pre[x]);
}
int judge(int u,int v)
{
int fx=find(u);
int fy=find(v);
if(fx!=fy)
{
pre[fy]=fx;
return 1;
}
return 0;
}
void unit()
{
for(int i=1;i<=n;i++)
{
pre[i]=i;
}
}
int main()
{
int t;
cin>>t;
int sum=0;;
while(t--)
{
sum++;
cin>>n>>m;
if(n==1&&m==0)// A special case , Discuss alone
cout<<"Case #"<<sum<<" : No second way"<<endl;
else
{
unit();
memset(biao,0,sizeof(biao));
for(int i=0;i<m;i++)
{
cin>>a[i].u>>a[i].v>>a[i].w;
}
sort(a,a+m,cmp);
int ans=0,num=0;
int flag=0;// Mark , Used to determine whether a complete path can be formed
for(int i=0;i<m;i++)
{
if(judge(a[i].u,a[i].v))
{
ans+=a[i].w;
num++;
biao[i]=1;
if(num==n-1)
{
flag=1;// There is a minimum spanning tree ,
break;
}
}
}
int minn=ans;
cout<<"Case #"<<sum<<" : ";
if(flag==0)
cout<<"No way"<<endl;
else
{
flag=0;// Restart marking
int minnn=MAX;
for(int i=0;i<m;i++)
{
ans=0,num=0;
if(biao[i]==1)
{
unit();
for(int j=0;j<m;j++)
{
if(i==j)
continue;
if(judge(a[j].u,a[j].v))
{
ans+=a[j].w;
num++;
if(num==n-1)
{
flag=1;// Is there any tree smaller than the minimum spanning tree
minnn=min(minnn,ans);// Find a small spanning tree
break;
}
}
}
}
}
if(flag==0)
cout<<"No second way"<<endl;
else
cout<<minnn<<endl;
}
}
}
return 0;
}
边栏推荐
- Three ways and differences of defining functions in JS
- 1130: find the first character that appears only once
- In situ merging of two arrays with two pointers
- Matlab judge palindrome number (only numbers)
- Win10 one click Reset win10 to solve all system bugs without deleting any files and Applications
- Sum of CCF digits (full mark code + problem solving idea) 201512-1
- 1062 talent and virtue (25 points)
- V3 02——What‘s new in Chrome extensions
- ES6 notes
- Quick sort (C language)
猜你喜欢
![[matlab] 2D drawing summary](/img/de/6bb5385f440a2997dbf9cbb9a756eb.jpg)
[matlab] 2D drawing summary

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

Lihongyi machine learning 2020 homework summary

Solve the problem that codeblocks20.03 on win11 cannot run for the first time

Matlab function for limit, definite integral, first-order derivative, second-order derivative (classic examples)

Error $(...) size is not a function

Detailed explanation of settimeout() and setinterval()

Sum of CCF digits (full mark code + problem solving idea) 201512-1

Matlab judge palindrome number (only numbers)

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
随机推荐
1027 colors in Mars (20 points)
CCF elimination games (Full Score code + problem solving ideas + skill summary) February 2, 2015
1134: Legal C identifier query
Matlab draws the image of the larger function value of the two functions (super simple)
V3 02——What‘s new in Chrome extensions
1105 spiral matrix (25 points)
Knowledge learned from the water resources institute project
Matlab finds prime numbers within 100
For loop and promise to solve the problem of concurrent callback
Matlab function for limit, definite integral, first-order derivative, second-order derivative (classic examples)
How to realize selective screen recording for EV screen recording
高精度CNC加工中心为什么会出现误差?这4个原因你要注意!
1131: genetic correlation
1130: find the first character that appears only once
Minimum covering substring of two pointers
CCF Z-scan (full mark code + problem solving ideas) 201412-2
Is pioneer futures safe? What are the procedures for opening futures accounts? How to reduce the futures commission?
[extensive reading of papers] a delicious recipe analysis framework for exploring multi modal recipes with variable attributes
1058 a+b in Hogwarts (20 points)
2021-05-12