Is There A Second Way Left?
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
题意:求连通图的最小和次小长度,三种情况:1.不存在最小 2.不存在次小 3.最小和次小不同。
kruskal算法
和之前做过的一篇很像,求有么有次短路,把最短路的边破坏(删除),
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
#define INF 0x3f3f3f3f
int n,m,f,sum,summ;
int fa[502],book[20000];
void inti()
{
for(int i=1;i<=n;i++)
fa[i]=i;
}
struct node
{
int u,v,w;
}a[30000];
bool cmp(node x,node y)
{
return x.w<y.w;
}
int find(int x)
{
if(x==fa[x])
return x;
return fa[x]=find(fa[x]);
}
int join(int x,int y)
{
int r1=find(x);
int r2=find(y);
if(r1!=r2)
{
fa[r2]=r1;
return 1;
}
return 0;
}
int main()
{
int t;
scanf("%d",&t);
int k=1;
while(t--)
{
memset(book,0,sizeof(book));
scanf("%d%d",&n,&m);
inti();
for(int i=1;i<=m;i++)
scanf("%d%d%d",&a[i].u,&a[i].v,&a[i].w);
printf("Case #%d : ",k++);
sort(a+1,a+m+1,cmp);
f=0;
sum=0;
for(int i=1;i<=m;i++)
{
if(join(a[i].u,a[i].v))
{
book[++f]=i;
}
if(f==n-1)
break;
}
int minn=INF;
if(f!=n-1)
{
printf("No way\n");
continue;
}
for(int i=1;i<n;i++)
{
inti();
f=0;
summ=0;
for(int j=1;j<=m;j++)
{
if(j==book[i])
continue;
if(join(a[j].u,a[j].v))
{
f++;
summ+=a[j].w;
}
if(f==n-1)
break;
}
if(f==n-1)
minn=min(minn,summ);
}
if(minn==INF)
printf("No second way\n");
else
printf("%d\n",minn);
}
return 0;
}