本文主要是介绍【HDU】3986 Harry Potter and the Final Battle 最短路,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
传送门:【HDU】3986 Harry Potter and the Final Battle
题目分析:先求一次最短路,同时记录在最短路上的顶点以及以该顶点为弧尾的最短路上的边。然后枚举删除每一条边,分别求一次最短路,其中最大的即答案。当然不可达输出-1。
测试发现堆优化的dij不如slf优化的spfa。。可能图太稀疏了吧。。。反正我觉得我写的挺搓的了。。。
代码如下:
#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 CLR( a , x ) memset ( a , x , sizeof a )const int MAXN = 1005 ;
const int MAXH = 100005 ;
const int MAXE = 100005 ;
const int INF = 0x3f3f3f3f ;struct Edge {int v , c , n ;Edge () {}Edge ( int v , int c , int n ) : v ( v ) , c ( c ) , n ( n ) {}
} ;struct Heap {int v , idx ;Heap () {}Heap ( int v , int idx ) : v ( v ) , idx ( idx ) {}bool operator < ( const Heap& a ) const {return v < a.v ;}
} ;struct priority_queue {Heap heap[MAXH] ;int point ;priority_queue () : point ( 1 ) {}void clear () {point = 1 ;}bool empty () {return point == 1 ;}void maintain ( int o ) {int x = o ;while ( o > 1 && heap[o] < heap[o >> 1] ) {swap ( heap[o] , heap[o >> 1] ) ;o >>= 1 ;}o = x ;int p = o , l = o << 1 , r = o << 1 | 1 ;while ( o < point ) {if ( l < point && heap[l] < heap[p] ) p = l ;if ( r < point && heap[r] < heap[p] ) p = r ;if ( p == o ) break ;swap ( heap[o] , heap[p] ) ;o = p , l = o << 1 , r = o << 1 | 1 ;}}void push ( int v , int idx ) {heap[point] = Heap ( v , idx ) ;maintain ( point ++ ) ;}void pop () {heap[1] = heap[-- point] ;maintain ( 1 ) ;}int front () {return heap[1].idx ;}Heap top () {return heap[1] ;}
} ;struct Shortest_Path_Algorithm {priority_queue q ;Edge E[MAXE] ;int H[MAXN] , cur ;int d[MAXN] ;bool vis[MAXN] ;int used[MAXN] ;int f[MAXN] ;int Q[MAXN] , head , tail ;void init () {cur = 0 ;CLR ( H , -1 ) ;}void addedge ( int u , int v , int c = 0 ) {E[cur] = Edge ( v , c , H[u] ) ;H[u] = cur ++ ;}void dijkstra ( int s , int t , int closed ) {q.clear () ;CLR ( d , INF ) ;CLR ( vis , 0 ) ;d[s] = 0 ;q.push ( d[s] , s ) ;while ( !q.empty () ) {int u = q.front () ;q.pop () ;if ( vis[u] ) continue ;vis[u] = 1 ;for ( int i = H[u] ; ~i ; i = E[i].n ) {int v = E[i].v , c = E[i].c ;if ( i != closed && d[v] > d[u] + c ) {d[v] = d[u] + c ;q.push ( d[v] , v ) ;if ( closed == -1 ) {f[v] = u ;used[v] = i ;}}}}}void spfa ( int s , int t , int closed ) {head = tail = 0 ;CLR ( d , 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 ;for ( int i = H[u] ; ~i ; i = E[i].n ) {int v = E[i].v , c = E[i].c ;if ( i != closed && d[v] > d[u] + c ) {d[v] = d[u] + 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 ;}}if ( closed == -1 ) {f[v] = u ;used[v] = i ;}}}}}
} 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 () ;scanf ( n ) ;scanf ( m ) ;while ( m -- ) {scanf ( u ) , scanf ( v ) , scanf ( c ) ;G.addedge ( u , v , c ) ;G.addedge ( v , u , c ) ;}G.spfa ( 1 , n , -1 ) ;if ( G.d[n] == INF ) {printf ( "-1\n" ) ;return ;}int ans = G.d[n] ;for ( int i = n ; i != 1 ; i = G.f[i] ) {G.spfa ( 1 , n , G.used[i] ) ;if ( G.d[n] > ans ) ans = G.d[n] ;if ( ans == INF ) break ;}printf ( "%d\n" , ans == INF ? -1 : ans ) ;
}int main () {int T ;scanf ( "%d" , &T ) ;while ( T -- ) solve () ;return 0 ;
}
这篇关于【HDU】3986 Harry Potter and the Final Battle 最短路的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!