The enemy has invaded your spaceship, and only superior tactics will allow you to defend it! To travel around your spaceship, your soldiers will use two devices: teleporters andturbolifts.
Teleporters allow your soldiers to move instantly between rooms. Every room contains a teleporter, and rooms are color-coded: if a soldier is in a room with some color, she can use the teleporter in that room to immediately move to any other room with the same color.
Turbolifts allow your soldiers to move between rooms more slowly. A turbolift is like an elevator that moves in many directions. Each turbolift moves from one room to one other room, and it takes a certain amount of time to travel. Notes about turbolifts:
a
to room b
, the same turbolift cannot move soldiers from room b
to room a
, although there might be another turbolift that does that.You will be given the locations and destinations of several soldiers. For each soldier, output the minimum amount of time it could take that soldier to travel from his location to his destination.
The first line of the input gives the number of test cases, T. T test cases follow.
For every test case:
The first line of every test case contains an integer N, which is the number of rooms in your spaceship. The rooms are numbered from 1 to N. The following N lines each contain a string telling the color of the rooms, from room 1 to room N. The strings only contain characters a-z
(the lower-case English letters) and 0-9
(the number 0 to 9), and the length of each string will be less than or equal to 2.
The next line in the test case is an integer M, which indicates the number of turbolifts in your spaceship. The following M lines each contain 3 space-separated integers ai, bi, ti, telling us that there is a turbolift that can transport soldiers from room ai to room bi in tiseconds.
The next line in the test case contains an integer S, which is the number of soldiers at your command. The following S lines each contain two integers: the location and destination of one soldier, pj and qj.
For each test case, output one line containing only the string "Case #x:", where x is the number of the test case (starting from 1). On the next S lines, output a single integer: on line j, the smallest number of seconds it could take for a soldier to travel from pj to qj. If there is no path from pj to qj, the integer you output should be -1.
1 ≤ S ≤ 100.
1 ≤ ai, bi ≤ N.
0 ≤ ti ≤ 1000.
1 ≤ pj, qj ≤ N.
1 ≤ T ≤ 10.
1 ≤ N ≤ 1000.
0 ≤ M ≤ 3000.
T = 1.
1 ≤ N ≤ 80000.
0 ≤ M ≤ 3000.
Input |
Output |
3 3 gl t3 t3 3 1 2 217 3 2 567 1 1 21 2 2 1 2 3 4 ca bl bl 8z 0 3 1 2 2 3 1 1 8 re b7 ye gr 0l 0l ye b7 7 4 1 19 2 4 21 2 5 317 4 5 34 4 7 3 4 8 265 8 6 71 3 4 3 2 6 1 4 |
Case #1: -1 0 Case #2: -1 0 0 Case #3: 3 55 -1 |
类型:图论 难度:2
题意:有编号1-n的n个点,给出每个点的颜色,同颜色的点可以不花时间立即到达,给出每条边的起点终点和路径长度,求给出的起点和终点之间的最短距离,若到达不了输出-1。
分析:求单源最短距离的变型,将每个颜色看成一个点,将给出的边化成每种颜色之间的边,再将所求的起点和终点转化为颜色用dij求最短距离即可。注意dij细节即可。
代码:
#include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<cstring> #include<string> #include<map> #include<set> #include<iostream> #define MIN(x,y) (x)<(y)?(x):(y) using namespace std; const int N=80100; const int M=1500; map<string,int> color; int colnum[N]; int edge[M][M][2]; int ednum[M][M]; int cal(int st,int en) { if(st == en) return 0; if(edge[st][en][1] == 1) return edge[st][en][0]; map<int,int> minedge; edge[st][st][1] = 1; for(int i=1; i<=ednum[st][0]; i++) { int pvt = ednum[st][i]; minedge[pvt] = edge[st][pvt][0]; } while(!minedge.empty()) { map<int,int>::iterator it; int minnum = -1; for(it=minedge.begin(); it!=minedge.end(); it++) { if(minnum<0 || it->second<minedge[minnum]) { minnum = it->first; } } edge[st][minnum][1] = 1; for(int i=1; i<=ednum[minnum][0]; i++) { int pvt = ednum[minnum][i]; if(edge[st][pvt][0] == -1 || edge[st][pvt][0] > edge[st][minnum][0]+edge[minnum][pvt][0]) { edge[st][pvt][0] = edge[st][minnum][0]+edge[minnum][pvt][0]; minedge[pvt] = edge[st][pvt][0]; } } minedge.erase(minnum); } for(int i=0; i<M; i++) { edge[st][i][1] = 1; } return edge[st][en][0]; } int main() { freopen("E-large-practice.in","r",stdin); freopen("E-large-practice.out","w",stdout); int t; scanf("%d",&t); for(int cnt=1;cnt<=t;cnt++) { color.clear(); memset(colnum,0,sizeof(colnum)); memset(edge,-1,sizeof(edge)); memset(ednum,0,sizeof(ednum)); int n; scanf("%d",&n); int coln = 0; for(int i=1; i<=n; i++) { char tmp[5]; scanf("%s",tmp); string tmp_s = tmp; if(color[tmp_s] == 0) color[tmp_s] = ++coln; colnum[i] = color[tmp_s]; //printf("dddd %d %d\n",i,colnum[i]); } int m; scanf("%d",&m); for(int i=0;i<m;i++) { int st,en,ti; scanf("%d%d%d",&st,&en,&ti); int stc = colnum[st]; int enc = colnum[en]; if(stc == enc) continue; if(edge[stc][enc][0] == -1) { edge[stc][enc][0] = ti; ednum[stc][++ednum[stc][0]] = enc; } else { if(edge[stc][enc][0] > ti) edge[stc][enc][0] = ti; } } /* printf("\nedge:\n"); for(int i=1; i<=coln; i++) { for(int j=1; j<=coln;j++) printf("%d ",edge[i][j][0]); printf("\n"); } printf("\nedgenum:\n"); for(int i=1; i<=coln; i++) { printf("i:",i); for(int j=1; j<=ednum[i][0];j++) printf("%d ",ednum[i][j]); printf("\n"); } */ printf("Case #%d:\n",cnt); int s; scanf("%d",&s); for(int i=0; i<s; i++) { int st,en; scanf("%d%d",&st,&en); int stc = colnum[st]; int enc = colnum[en]; //printf("xxx %d %d\n",stc,enc); printf("%d\n",cal(stc,enc)); } } }