标签:
10 8 1 2 5 1 3 2 2 3 11 2 4 6 2 4 4 6 7 10 6 10 5 10 7 2 5 2 3 1 4 3 7 6 7 8 3
5 5 -1 5 -1
思路其实很简单 。按照危险度从小到达排序 。然后使用并查集判断A B是否在一个集合。
听说深搜也能过。。没写
#include <stdio.h>
#include <algorithm>
using namespace std;
int fa[105];
struct node
{
int a,b,cost;
}c[505];
bool cmp(node x,node y)
{
return x.cost<y.cost;
}
void init(int n)
{
for(int i=0;i<=n;i++)
fa[i]=i;
}
int find(int x)
{
if(fa[x]!=x) fa[x]=find(fa[x]);
return fa[x];
}
int main()
{
int n,m;
scanf("%d %d",&n,&m);
for(int i=0;i<m;i++)
scanf("%d %d %d",&c[i].a,&c[i].b,&c[i].cost);
sort(c,c+m,cmp);
int q;
scanf("%d",&q);
while(q--)
{
int st,ed;
int ok=-1;
scanf("%d %d",&st,&ed);
for(int i=0;i<m;i++)
{
init(n);
for(int j=i;j<m;j++)
{
int xx=find(c[j].a);
int yy=find(c[j].b);
if(xx!=yy)
fa[xx]=yy;
if(find(st)==find(ed))
{
ok=c[j].cost;
break;
}
}
if(ok!=-1)
break;
}
printf("%d\n",ok);
}
return 0;
} 标签:
原文地址:http://blog.csdn.net/su20145104009/article/details/51519865