程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 舒適的路線

舒適的路線

編輯:C++入門知識

// 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; 

// 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;
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved