Navigation Nightmare
Time Limit: 2000MS | Memory Limit: 30000K | |
Total Submissions: 4054 | Accepted: 1606 | |
Case Time Limit: 1000MS |
Description
F1 --- (13) ---- F6 --- (9) ----- F3
| |
(3) |
| (7)
F4 --- (20) -------- F2 |
| |
(2) F5
|
F7
Input
* Line 1: Two space-separated integers: N and M
* Lines 2..M+1: Each line contains four space-separated entities, F1,
F2, L, and D that describe a road. F1 and F2 are numbers of
two farms connected by a road, L is its length, and D is a
character that is either 'N', 'E', 'S', or 'W' giving the
direction of the road from F1 to F2.
* Line M+2: A single integer, K (1 <= K <= 10,000), the number of FB's
queries
* Lines M+3..M+K+2: Each line corresponds to a query from Farmer Bob
and contains three space-separated integers: F1, F2, and I. F1
and F2 are numbers of the two farms in the query and I is the
index (1 <= I <= M) in the data after which Bob asks the
query. Data index 1 is on line 2 of the input data, and so on.
Output
* Lines 1..K: One integer per line, the response to each of Bob's
queries. Each line should contain either a distance
measurement or -1, if it is impossible to determine the
appropriate distance.
Sample Input
7 6
1 6 13 E
6 3 9 E
3 5 7 S
4 1 3 N
2 4 20 W
4 7 2 S
3
1 6 1
1 4 3
2 6 6
Sample Output
13
-1
10
Hint
Source
F1 --- (13) ---- F6 --- (9) ----- F3
| |
(3) |
| (7)
F4 --- (20) -------- F2 |
| |
(2) F5
|
F7
每个农场最多能在东西南北四个方向连接 4 个不同的农场。此外,农场只处在道路的两端。道路不会交叉且每对农场间有且仅有一条路径。邻居鲍勃要约翰来导航,但约翰丢了农场的地图,他只得从电脑的备份中修复了。每一条道路的信息如下:#include <cstdio> #include <cstring> #include <algorithm> using namespace std ; #define REP( i , n ) for ( int i = 0 ; i < n ; ++ i ) #define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i ) #define REPV( i , a , b ) for ( int i = a ; i >= b ; -- i ) #define clear( a , x ) memset ( a , x , sizeof a ) const int MAXN = 50000 ; const int MAXE = 50000 ; struct Edge { int x , y , n ; int idx ; void input ( int __idx ) { scanf ( "%d%d" , &x , &y ) ; idx = __idx ; } } ; struct Line { int x , y , d ; char ch[5] ; void input () { scanf ( "%d%d%d%s" , &x , &y , &d , ch ) ; } } ; Edge edge[MAXE] ; Line line[MAXN] ; int adj[MAXN] , cntE ; int p[MAXN] ; int X[MAXN] , Y[MAXN] ; int ask[MAXN] ; int n , m , t ; int find ( int x ) { int tmp ; if ( p[x] == x ) return x ; tmp = find ( p[x] ) ; X[x] += X[p[x]] ; Y[x] += Y[p[x]] ; return p[x] = tmp ; } void work () { int x , y , day , d ; while ( ~scanf ( "%d%d" , &n , &m ) ) { clear ( adj , -1 ) ; cntE = 0 ; REPF ( i , 1 , n ) p[i] = i , X[i] = Y[i] = 0 ; REPF ( i , 1 , m ) line[i].input () ; scanf ( "%d" , &t ) ; REP ( i , t ) { edge[cntE].input ( i ) ; scanf ( "%d" , &day ) ; edge[cntE].n = adj[day] ; adj[day] = cntE ++ ; } REPF ( i , 1 , m ) { x = find ( line[i].x ) ; y = find ( line[i].y ) ; d = line[i].d ; if ( x != y ) { p[x] = y ; if ( line[i].ch[0] == 'E' ) X[x] = -X[line[i].x] + X[line[i].y] - d , Y[x] = -Y[line[i].x] + Y[line[i].y] ; else if ( line[i].ch[0] == 'W' ) X[x] = -X[line[i].x] + X[line[i].y] + d , Y[x] = -Y[line[i].x] + Y[line[i].y] ; else if ( line[i].ch[0] == 'N' ) Y[x] = -Y[line[i].x] + Y[line[i].y] - d , X[x] = -X[line[i].x] + X[line[i].y] ; else Y[x] = -Y[line[i].x] + Y[line[i].y] + d , X[x] = -X[line[i].x] + X[line[i].y] ; } for ( int j = adj[i] ; ~j ; j = edge[j].n ) { x = edge[j].x ; y = edge[j].y ; int flag = ( find ( x ) == find ( y ) ) ; ask[edge[j].idx] = ( flag ? abs ( X[x] - X[y] ) + abs ( Y[x] - Y[y] ) : -1 ) ; } } REP ( i , t ) printf ( "%d\n" , ask[i] ) ; } } int main () { work () ; return 0 ; }