【HDU】2807 The Shortest Path 最短路

传送门:【HDU】2807 The Shortest Path


题目分析:题目很简单,矩阵计算出两个城市的连通性,建边,然后每次询问求最短路回答(或者floyd预处理)。

当然暴力的代价是惨痛的,用堆优化+dij+输入优化最多800ms。

然后很好奇前面的是怎么跑的这么快的,看了别人写的题解才发现,原来他们是用了hash的方法将二维化为一维了,虽然可能会错误,但在出题人不是故意去卡的情况下算是基本可以高效的过这道题了。不过我宁可速度慢一点,求稳。

这里我两份代码均用了dij,因为我感觉不是稠密图。


自己一开始的方法:堆优化+dij+输入优化 G++796ms


代码如下:


#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 = 100 ;
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 ) {
		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 ( d[v] > d[u] + c ) {
					d[v] = d[u] + c ;
					q.push ( d[v] , v ) ;
				}
			}
		}
	}
	
	void spfa ( int s , int t ) {
		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 ( 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 ;
						}
					}
				}
			}
		}
	}
} G ;


struct Matrix {
	int mat[MAXN][MAXN] , n ;
	
	Matrix () {}
	
	Matrix ( int n ) : n ( n ) {
		CLR ( mat , 0 ) ;
	}
	
	Matrix operator * ( const Matrix& a ) const {
		Matrix res ( n ) ;
		FOR ( i , 0 , n ) FOR ( j , 0 , n ) FOR ( k , 0 , n ) res.mat[i][j] += mat[i][k] * a.mat[k][j] ;
		return res ;
	}
	
	bool operator == ( const Matrix& a ) const {
		FOR ( i , 0 , n ) FOR ( j , 0 , n ) if ( mat[i][j] != a.mat[i][j] ) return 0 ;
		return 1 ;
	}
} a[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 ;
	G.init () ;
	FOR ( i , 1 , n ) a[i].n = m ;
	FOR ( i , 1 , n ) REP ( x , 0 , m ) REP ( y , 0 , m ) scanf ( a[i].mat[x][y] ) ;
	FOR ( i , 1 , n ) FOR ( j , 1 , n ) {
		if ( i == j ) continue ;
		Matrix tmp = a[i] * a[j] ;
		FOR ( k , 1 , n ) if ( i != k && j != k ) if ( tmp == a[k] ) G.addedge ( i , k , 1 ) ;
	}
	scanf ( q ) ;
	while ( q -- ) {
		scanf ( u ) , scanf ( v ) ;
		G.dijkstra ( u , v ) ;
		if ( G.d[v] == INF ) printf ( "Sorry\n" ) ;
		else printf ( "%d\n" , G.d[v] ) ;
	}
}

int main () {
	while ( ~scanf ( "%d%d" , &n , &m ) && ( n || m ) ) solve () ;
	return 0 ;
}



后来用了hash优化的代码:G++31ms


代码如下:


#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 = 100 ;
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 ) {
		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 ( d[v] > d[u] + c ) {
					d[v] = d[u] + c ;
					q.push ( d[v] , v ) ;
				}
			}
		}
	}
	
	void spfa ( int s , int t ) {
		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 ( 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 ;
						}
					}
				}
			}
		}
	}
} G ;

int n , m , q ;

struct Matrix2D {
	int mat[MAXN][MAXN] ;
} a[MAXN] ;

struct Matrix1D {
	int mat[MAXN] ;
	bool operator == ( const Matrix1D& a ) const {
		REP ( i , 0 , m ) if ( mat[i] != a.mat[i] ) return 0 ;
		return 1 ;
	}
} b[MAXN] ;

Matrix1D mul ( Matrix2D a , Matrix1D b ) {
	Matrix1D res ;
	REP ( i , 0 , m ) {
		res.mat[i] = 0 ;
		REP ( j , 0 , m ) res.mat[i] += a.mat[i][j] * b.mat[j] ;
	}
	return res ;
}

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 ;
	G.init () ;
	FOR ( i , 1 , n ) REP ( j , 0 , m ) {
		b[i].mat[j] = 0 ;
		REP ( k , 0 , m ) {
			scanf ( a[i].mat[j][k] ) ;
			b[i].mat[j] += a[i].mat[j][k] * k ;
		}
	}
	FOR ( i , 1 , n ) FOR ( j , 1 , n ) {
		if ( i == j ) continue ;
		Matrix1D tmp = mul ( a[i] , b[j] ) ;
		FOR ( k , 1 , n ) if ( i != k && j != k ) if ( tmp == b[k] ) G.addedge ( i , k , 1 ) ;
	}
	scanf ( q ) ;
	while ( q -- ) {
		scanf ( u ) , scanf ( v ) ;
		G.spfa ( u , v ) ;
		if ( G.d[v] == INF ) printf ( "Sorry\n" ) ;
		else printf ( "%d\n" , G.d[v] ) ;
	}
}

int main () {
	while ( ~scanf ( "%d%d" , &n , &m ) && ( n || m ) ) solve () ;
	return 0 ;
}
相关文章
相关标签/搜索