本文主要是介绍【HDU】1595 find the longest of the shortest 枚举+最短路,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
传送门:【HDU】1595 find the longest of the shortest
题目分析:首先求出一条最短路,记录下最短路上用到的边,枚举删除每一条边,求一次最短路,求完后恢复删除的边。重复这一过程直到枚举完所有的边为止。所有删除边后求得的最短路里最长的那条就是答案。
代码如下:
#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 = 2000005 ;
const int MAXE = 2000005 ;
const int INF = 0x3f3f3f3f ;struct Edge {int v , c , n ;bool flag ;Edge () {}Edge ( int v , int c , bool flag , int n ) : v ( v ) , c ( c ) , flag ( flag ) , n ( n ) {}
} ;struct Heap {int v ;int 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 x ) {int o = x , p = o , l = o << 1 , r = o << 1 | 1 ;while ( o > 1 && heap[o] < heap[o >> 1] ) {swap ( heap[o] , heap[o >> 1] ) ;o >>= 1 ;}o = x ;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 ) ;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 used[MAXN] ;int d[MAXN] ;int f[MAXN] ;bool vis[MAXN] ;void init () {cur = 0 ;CLR ( H , -1 ) ;}void addedge ( int u , int v , int c = 0 , bool flag = 0 ) {E[cur] = Edge ( v , c , flag , H[u] ) ;H[u] = cur ++ ;}void dijkstra ( int s , int t , bool first ) {CLR ( d , INF ) ;CLR ( vis , 0 ) ;q.clear () ;d[s] = 0 ;f[s] = -1 ;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 ;if ( !E[i].flag && d[v] > d[u] + E[i].c ) {if ( first ) {f[v] = u ;used[v] = i ;}d[v] = d[u] + E[i].c ;q.push ( d[v] , v ) ;}}}}
} dij ;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 ;dij.init () ;while ( m -- ) {scanf ( u ) , scanf ( v ) , scanf ( c ) ;dij.addedge ( u , v , c ) ;dij.addedge ( v , u , c ) ;}dij.dijkstra ( n , 1 , 1 ) ;int ans = dij.d[1] ;for ( int v = 1 ; ~v ; v = dij.f[v] ) {dij.E[dij.used[v]].flag = 1 ;dij.dijkstra ( n , 1 , 0 ) ;dij.E[dij.used[v]].flag = 0 ;ans = max ( ans , dij.d[1] ) ;}printf ( "%d\n" , ans ) ;
}int main () {while ( ~scanf ( "%d%d" , &n , &m ) ) solve () ;return 0 ;
}
这篇关于【HDU】1595 find the longest of the shortest 枚举+最短路的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!