好吧,这道题的sample是错误的。。。就是一个求最短路的问题,直接Kruskal的模板搞起
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <string.h>
using namespace std;
const int M = 25003;
int n,m;
int father[M];
int num[M];
struct node{
int s,e,v;
//bool visit;
}edge[M];
bool cmp( node a , node b )
{
return a.v < b.v;
}
int find( int x )
{
return x == father[x] ? x : father[x] = find( father[x] );
}
bool merge( int a , int b )
{
int x,y;
x = find( a );
y = find( b );
if( x == y )
return true;
else
father[x] = y;
return false;
}
int main()
{
while( scanf("%d%d",&n,&m) == 2 ){
for( int i=1 ; i<=n ; i++ )
father[i] = i;
for( int i=1 ; i<=m ; i++ ){
scanf("%d%d%d",&edge[i].s,&edge[i].e,&edge[i].v);
}
sort( edge+1 , edge+1+m , cmp );
int minn,l;
l = minn = 0;
for( int i=1 ; i<=m ; i++ ){
if( merge(edge[i].s , edge[i].e ) )
continue;
else{
minn = edge[i].v;
num[l++] = i;
}
}
printf("%d\n%d\n",minn,l);
for( int i=0 ; i<l ; i++ )
printf("%d %d\n",edge[num[i]].s,edge[num[i]].e);
}
return 0;
}