Is There A Second Way Left ?
Input: standard input
Output: standard output
Time Limit: 3 seconds
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 - 1. no way to complete the task, 2. There is only one way to complete the task, 3. 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
4Sample Output
Case #1 : No second wayCase #4 : No second way
思路:这是一题带重边的次小生成树,用kruskal算法即可。
代码:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
const int inf=0x3f3f3f3f;
const int N=1005;
int n,m;
struct node
{
int x,y,z;
}e[N];
int p[N];
int id[N];
bool cmp(node a,node b)
{
return a.z<b.z;
}
int findx(int x)
{
if(x==p[x])return x;
return p[x]=findx(p[x]);
}
int kr()
{
int num=0;
int i;
for(i=1;i<=n;i++)
{
p[i]=i;
}
int ans=0,k=0;
for(i=0;i<m;i++)
{
int a=findx(e[i].x);
int b=findx(e[i].y);
if(a!=b)
{
id[k++]=i;
p[a]=b;
ans+=e[i].z;
}
}
for(i=1;i<=n;i++)
{
if(i==findx(i))
{
num++;
}
}
if(num>1)return inf;
else return ans;
}
int kr1(int x)
{
int num=0;
int k=0,ans=0,i;
for(i=1;i<=n;i++)
{
p[i]=i;
}
for(i=0;i<m;i++)
{
if(i==x)continue;
int a=findx(e[i].x);
int b=findx(e[i].y);
if(a!=b)
{
p[a]=b;
ans+=e[i].z;
}
}
for(i=1;i<=n;i++)
{
if(i==findx(i))
{
num++;
}
}
if(num>1)return inf;
else return ans;
}
int main()
{
int T;
scanf("%d",&T);
int k=1;
while(T--)
{
scanf("%d%d",&n,&m);
int i;
for(i=0;i<m;i++)
{
scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].z);
}
sort(e,e+m,cmp);
int ans=kr();
int ans1=inf;
printf("Case #%d : ",k++);
if(ans==inf)
{
printf("No way\n");
continue;
}
for(i=0;i<n-1;i++)
{
int x=id[i];
ans1=min(ans1,kr1(x));
}
if(ans1==inf)printf("No second way\n");
else printf("%d\n",ans1);
}
return 0;
}