hdu 1598 find the most comfortable road

思路:快排+并查集

#include<stdio.h>
#include<algorithm>
#define inf 999999

using namespace std;
struct node
{
int s,e,w;
}road[1005];
int xx[1005];
int cmp(node a,node b)
{
return a.w<b.w;
}
int find(int x)
{
while(x!=xx[x]) x=xx[x];
return x;
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
for(int i=0;i<m;i++)
scanf("%d%d%d",&road[i].s,&road[i].e,&road[i].w);
sort(road,road+m,cmp);

int p,x,y,j;
scanf("%d",&p);
while(p--)
{
int min=inf;
scanf("%d%d",&x,&y);
for(int i=0;i<m;i++)
{
for(j=0;j<m;j++)
xx[j]=j;

for(j=i;j<m;j ++ )
{
int a=find(road[j].s);
int b=find(road[j].e);
if(a!=b) xx[a]=xx[b];
if(find(x)==find(y))
{
int t=road[j].w-road[i].w;
if(min>t) min=t;break;
}

}
if(j==m) break;
}
if(min==inf ) puts("-1");
else printf("%d\n",min);
}
}
return 0;
}



Just a little, maybe change the world
原文地址:https://www.cnblogs.com/skyming/p/2265335.html