bzoj1726 第二短路
一道严格次短路题,WA了一下午回家才发现bellman_ford中的vis [ o ] = false 写成了 vis [ S ] = false.被自己脑残了一脸。。。。
1 #include<climits> 2 #include<cstdio> 3 #include<algorithm> 4 #include<queue> 5 using namespace std ; 6 7 void min_equal ( int & a , const int b ) { 8 if ( a > b ) a = b ; 9 } 10 11 const int MAXN = 10000 + 20 ; 12 const int MAXR = 1000000 + 20 ; 13 14 struct edge { 15 int dis ; 16 int p ; 17 edge * next ; 18 } ; 19 20 edge * V [ MAXN ] ; 21 edge E [ MAXR * 2 ] ; 22 23 void add_edge ( const int a , const int b , const int dis ) { 24 static edge * t = E ; 25 edge * x = t ++ , * y = t ++ ; 26 x -> dis = y -> dis = dis ; 27 x -> p = b ; y -> p = a ; 28 x -> next = V [ a ] ; y -> next = V [ b ] ; 29 V [ a ] = x ; V [ b ] = y ; 30 } 31 32 int N , R ; 33 int dis1 [ MAXN ] ; 34 int dis2 [ MAXN ] ; 35 36 void bellman_ford ( const int S , const int T , int dis [] ) { 37 static int vis [ MAXN ] ; 38 static const int INF = INT_MAX ; 39 static queue < int > q ; 40 fill ( dis , dis + N + 1 , INF ) ; 41 fill ( vis , vis + N + 1 , 0 ) ; 42 dis [ S ] = 0 ; 43 vis [ S ] = true ; 44 q . push ( S ) ; 45 while ( ! q . empty () ) { 46 const int o = q . front () ; 47 q . pop () ; 48 vis [ o ] = false ; 49 for ( edge * v = V [ o ] ; v != 0 ; v = v -> next ) 50 if ( dis [ o ] + v -> dis < dis [ v -> p ] ) { 51 dis [ v -> p ] = dis [ o ] + v -> dis ; 52 if ( ! vis [ v -> p ] ) { 53 vis [ v -> p ] = true ; 54 q . push ( v -> p ) ; 55 } 56 } 57 } 58 } 59 60 int main () { 61 scanf ( "%d%d" , & N , & R ) ; 62 while ( R -- ) { 63 int a , b , c ; 64 scanf ( "%d%d%d" , & a , & b , & c ) ; 65 add_edge ( a , b , c ) ; 66 } 67 bellman_ford ( 1 , N , dis1 ) ; 68 bellman_ford ( N , 1 , dis2 ) ; 69 int ans = INT_MAX ; 70 for ( int i = 1 ; i <= N ; ++ i ) 71 for ( edge * v = V [ i ] ; v != 0 ; v = v -> next ) { 72 const int a = dis1 [ i ] + v -> dis + dis2 [ v -> p ] ; 73 if ( a != dis1 [ N ] ) min_equal ( ans , a ) ; 74 } 75 printf ( "%d\n" , ans ) ; 76 return 0 ; 77 }