The Shortest Path
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3319 Accepted Submission(s): 1098
Problem Description
There are N cities in the country. Each city is represent by a matrix size of M*M. If city A, B and C satisfy that A*B = C, we say that there is a road from A to C with distance 1 (but that does not means there is a road from C to A).
Now the king of the country wants to ask me some problems, in the format:
Is there is a road from city X to Y?
I have to answer the questions quickly, can you help me?
Now the king of the country wants to ask me some problems, in the format:
Is there is a road from city X to Y?
I have to answer the questions quickly, can you help me?
Input
Each test case contains a single integer N, M, indicating the number of cities in the country and the size of each city. The next following N blocks each block stands for a matrix size of M*M. Then a integer K means the number of questions the king will ask, the following K lines each contains two integers X, Y(1-based).The input is terminated by a set starting with N = M = 0. All integers are in the range [0, 80].
Output
For each test case, you should output one line for each question the king asked, if there is a road from city X to Y? Output the shortest distance from X to Y. If not, output "Sorry".
Sample Input
3 2 1 1 2 2 1 1 1 1 2 2 4 4 1 1 3 3 2 1 1 2 2 1 1 1 1 2 2 4 3 1 1 3 0 0
Sample Output
1 Sorry
Source
思路:给出n个点,每一个点用m*m的矩阵来描述,判断a和c之间有边,那么a*b = c,并且a和c之间的距离为1,(注意a*b也可能等于a和b,要避免这两种情况),需要先创建图,然后用弗洛伊德算法,就可以。
代码:
#include<stdio.h>
#include<string.h>
#define INF 0x3f3f3f3f
int a[85][85][85];
int distance[85][85];
int t, n, m;
void floyd() {
int i, j, k;
for(k = 1; k <= n; k++)
for(i = 1; i <= n; i++)
for(j = 1; j <= n; j++) {
if(distance[i][j] > distance[i][k] + distance[k][j])
distance[i][j] = distance[i][k] + distance[k][j];
}
}
void creatDistance() {
int i, j, k1, k2, k3;
int flag;
int temp[85][85];
for(i = 1; i <= n; i++)
for(j = 1; j <= n; j++) { //计算第i个与第j个的乘积,i*j
if(i == j)
continue;
for(k1 = 1; k1 <= m; k1++) //第i个的行
for(k2 = 1; k2 <= m; k2++) { //第j个的列
temp[k1][k2] = 0; //保留结果
for(k3 = 1; k3 <= m; k3++) //第i个的列和第j个的行
temp[k1][k2] += a[i][k1][k3] * a[j][k3][k2];
}
for(k1 = 1; k1 <= n; k1++) { //判断矩阵temp和哪一个相等
if(i == k1 || j == k1)
continue;
flag = 1;
for(k2 = 1; k2 <= m; k2++) {
for(k3 = 1; k3 <= m; k3++) {
if(temp[k2][k3] != a[k1][k2][k3]) {
flag = 0;
break;
}
}
if(!flag)
break;
}
if(flag) { //发现有相等的,第i个和第k1个之间有边
distance[i][k1] = 1;
}
}
}
}
int main() {
int i, j, k, u, v;
while(scanf("%d%d", &n, &m) != EOF) {
if(n == 0 && m == 0)
return 0;
for(i = 1; i <= n; i++)
for(j = 1; j <= n; j++) {
if(i == j)
distance[i][j] = 0;
else
distance[i][j] = INF;
}
for(i = 1; i <= n; i++)
for(j = 1; j <= m; j++)
for(k = 1; k <= m; k++) {
scanf("%d", &a[i][j][k]);
}
creatDistance();
floyd();
scanf("%d", &t);
for(k = 1; k <= t; k++) {
scanf("%d%d", &u, &v);
if(distance[u][v] != INF)
printf("%d\n", distance[u][v]);
else
printf("Sorry\n");
}
}
}