【HDU】3631 Shortest Path 【floyd】

来源:互联网 发布:知乎年度吐槽 编辑:程序博客网 时间:2024/04/29 21:13

传送门:【HDU】3631 Shortest Path


题目分析:考察对floyd的理解,如果存在一条路i->k,k->j,那么如果中间的k是未被标记的,那么我们就不用k去更新i->j之间的最短路,那么除非存在另一条路i->l,l->j且l已经被标记或者存在i->j的边,否则i->j是不可达的。每次标记一个点的时候就用这个点去更新所有的最短路即可。不要忘记重边的判断。


代码如下:


#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 long long LL ;const int MAXN = 300 ;const int INF = 0x3f3f3f3f ;bool vis[MAXN] ;int G[MAXN][MAXN] ;int n , m , q ;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 , tmp ;CLR ( G , INF ) ;CLR ( vis , 0 ) ;while ( m -- ) {scanf ( u ) , scanf ( v ) , scanf ( c ) ;if ( G[u][v] > c ) G[u][v] = c ;}while ( q -- ) {scanf ( c ) ;if ( c == 0 ) {scanf ( u ) ;if ( vis[u] ) printf ( "ERROR! At point %d\n" , u ) ;else {vis[u] = 1 ;G[u][u] = 0 ;REP ( i , 0 , n ) REP ( j , 0 , n )if ( G[i][j] > G[i][u] + G[u][j] ) G[i][j] = G[i][u] + G[u][j] ;}} else {scanf ( u ) , scanf ( v ) ;if ( !vis[u] || !vis[v] ) printf ( "ERROR! At path %d to %d\n" , u , v ) ;else if ( G[u][v] == INF ) printf ( "No such path\n" ) ;else printf ( "%d\n" , G[u][v] ) ;}}}int main () {int cas = 0 ;while ( ~scanf ( "%d%d%d" , &n , &m , &q ) && ( n || m || q ) ) {if ( cas ) printf ( "\n" ) ;printf ( "Case %d:\n" , ++ cas ) ;solve () ;}return 0 ;}


0 0
原创粉丝点击