蓝桥杯试题——灾后重建
#include <iostream>
#include <cstring>
int N, M, Q;
int x;
int C;
int visited[ 5002 ] ;
int p[ 5002 ] ;
using namespace std;
struct Map {
int arc[ 5002 ] [ 5002 ] ;
int vexnum;
int arcnum;
} ;
Map map;
void Init ( )
{
for ( int i = 0 ; i < 5002 ; i++ ) {
for ( int j = 0 ; j < 5002 ; j++ ) {
map. arc[ i] [ j] = 0 ;
}
}
return ;
}
void set ( int l, int r, int k, int c)
{
int i;
C = 0 ;
for ( int i = l; i <= r; i++ ) {
if ( i % k == c) {
p[ C] = i;
C++ ;
}
}
return ;
}
void dfs ( int temp, int s, int e)
{
if ( s == e) {
if ( temp < x) x = temp;
return ;
}
if ( visited[ s] == 1 ) return ;
visited[ s] = 1 ;
for ( int i = 1 ; i < N+ 1 ; i++ ) {
if ( map. arc[ s] [ i] != 0 ) {
if ( temp < map. arc[ s] [ i] )
dfs ( map. arc[ s] [ i] , i, e) ;
else
dfs ( temp, i, e) ;
}
}
visited[ s] = 0 ;
return ;
}
void fun ( )
{
int min = 0 ;
for ( int i = 1 ; i < C; i++ ) {
for ( int j = i+ 1 ; j< C ; j++ ) {
x = 1000000 ;
dfs ( 0 , p[ i] , p[ j] ) ;
if ( min < x) min = x;
}
}
cout << min << endl;
return ;
}
int main ( ) {
int x, y, p, c1, c2;
cin >> N >> M >> Q;
map. arcnum = M;
map. vexnum = N;
Init ( ) ;
for ( int i = 0 ; i < M; i++ ) {
cin >> c1 >> c2;
if ( c1 == c2)
cin >> c1;
else
{
cin >> map. arc[ c1] [ c2] ;
map. arc[ c2] [ c1] = map. arc[ c1] [ c2] ;
}
}
int L, R, K, C;
for ( int i = 0 ; i < Q; i++ )
{
memset ( visited, 0 , sizeof ( int ) * 5002 ) ;
cin >> L >> R >> K >> C;
set ( L, R, K, C) ;
fun ( ) ;
}
return 0 ;
}