舒适的路线

http://wikioi.com/problem/1001/

// File Name: wiki1001.cpp
// Author: bo_jwolf
// Created Time: 2013年08月18日 星期日 08时40分44秒

#include<vector>
#include<list>
#include<map>
#include<set>
#include<deque>
#include<stack>
#include<bitset>
#include<algorithm>
#include<functional>
#include<numeric>
#include<utility>
#include<sstream>
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<ctime>
#define INF 0xffffff
using namespace std;
const int maxn = 5005 ;
struct node
{
	int x , y , v ;
	bool operator < ( const node a ) const 
	{
		return v < a.v ;
	}
}edge[ maxn ] ;

int n , m , s ,t , k , Max , Min ;
int fa[ maxn ] ;

int find( int x )
{
	return fa[ x ] = fa[ x ] == x ? x : find( fa[ x ] ) ;
}

int gcd( int a , int b )
{
	return b == 0 ? a : gcd( b , a % b ) ;
}

int main()
{
	double ans = INF ;
	cin >> n >> m ;
	{
		for( int i = 1 ; i <= m ; ++ i )
		{
			cin >> edge[ i ]. x >> edge[ i ].y >> edge[ i ].v ;
		}
		cin >> s >> t ;
		sort( edge + 1 , edge  + 1 + m ) ;
		for( int i = 1 ; i <= m ; ++i )
		{
			for( int j = 0 ; j <= n ; ++j )
				fa[ j ] = j ;
			for( int j = i ; j <= m ; ++j )
			{
				if( find( edge[ j ].x ) != find( edge[ j ].y) )
					fa[ fa[ edge[ j ].x ] ] = fa[ edge[ j ].y ] ;
				if( find( s ) == find( t ) )
				{
					if( edge[ j ].v / 1.0 / edge[ i ].v < ans ) 
					{
						Max = edge[ j ].v ;
						Min = edge[ i ].v ;
						ans = edge[ j ].v / 1.0 / edge[ i ].v ;
					}
					break ;
				}
			}
		}
		if( ans == INF )
			cout << "IMPOSSIBLE" << endl ;
		else
		{
			int temp = gcd( Max , Min ) ;
			Max /= temp ;
			Min /= temp ;
			cout << Max ;
			if( Min != 1 )
				cout << "/" <<  Min ;
			cout << endl ;
		}
	}
return 0;
}


你可能感兴趣的:(舒适的路线)