传送门:【POJ】2472 106 miles to Chicago
题目分析:直接将边的权值c变成log(c/100.0),这样将乘积最大转化为相加最大了,这样求的就是最长路了,注意log里面一定要除以100,否则会产生负权环。最后输出exp(ans)*100即可。
代码如下:
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;
#define REP( i , a , b ) for ( int i = ( a ) ; i < ( b ) ; ++ i )
#define FOR( i , a , b ) for ( int i = ( a ) ; i <= ( b ) ; ++ i )
#define REV( i , a , b ) for ( int i = ( a ) ; i >= ( b ) ; -- i )
#define travel( e , H , u ) for ( Edge* e = H[u] ; e ; e = e -> next )
#define CLR( a , x ) memset ( a , x , sizeof a )
typedef double type_c ;
const int MAXN = 105 ;
const int MAXH = 20005 ;
const int MAXE = 20005 ;
const double INF = 1e9 ;
struct Edge {
int v ;
type_c c ;
Edge* next ;
} ;
struct Shortest_Path_Algorithm {
Edge E[MAXE] , *H[MAXN] , *cur ;
type_c d[MAXN] ;
bool vis[MAXN] ;
int Q[MAXN] , head , tail ;
void init () {
cur = E ;
CLR ( H , 0 ) ;
}
void addedge ( int u , int v , type_c c ) {
cur -> v = v ;
cur -> c = c ;
cur -> next = H[u] ;
H[u] = cur ++ ;
}
void spfa ( int s ) {
head = tail = 0 ;
REP ( i , 0 , MAXN ) d[i] = INF ;
CLR ( vis , 0 ) ;
d[s] = 0 ;
Q[tail ++] = s ;
while ( head != tail ) {
int u = Q[head ++] ;
if ( head == MAXN ) head = 0 ;
vis[u] = 0 ;
travel ( e , H , u ) {
int v = e -> v ;
if ( d[v] > d[u] + e -> c ) {
d[v] = d[u] + e -> c ;
if ( !vis[v] ) {
vis[v] = 1 ;
if ( d[v] < d[Q[head]] ) {
if ( head == 0 ) head = MAXN ;
Q[-- head] = v ;
} else {
Q[tail ++] = v ;
if ( tail == MAXN ) tail = 0 ;
}
}
}
}
}
}
} G ;
int n , m ;
void scanf ( int& x , char c = 0 ) {
while ( ( c = getchar () ) < '0' || c > '9' ) ;
x = c - '0' ;
while ( ( c = getchar () ) >= '0' && c <= '9' ) x = x * 10 + c - '0' ;
}
void solve () {
int u , v , c ;
G.init () ;
while ( m -- ) {
scanf ( "%d%d%d" , &u , &v , &c ) ;
G.addedge ( u , v , -log ( c / 100.0 ) ) ;
G.addedge ( v , u , -log ( c / 100.0 ) ) ;
}
G.spfa ( 1 ) ;
printf ( "%.6f percent\n" , exp ( -G.d[n] ) * 100 ) ;
}
int main () {
while ( ~scanf ( "%d%d" , &n , &m ) && n ) solve () ;
return 0 ;
}