hdu2807
来源:互联网 发布:淘宝内裤真人秀 编辑:程序博客网 时间:2024/06/06 05:11
The Shortest Path
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1525 Accepted Submission(s): 475
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 21 12 21 11 12 24 411 33 21 12 21 11 12 24 311 30 0
Sample Output
1Sorry
Source
HDU 2009-4 Programming Contest
Recommend
lcy
只会暴力~~~~1500ms水过~
#include <iostream>#include <cstdio>#include <cstring>#define inf 99999999using namespace std;int n,m;int e[100][100][100];int map[100][100];int temp[100][100];void floyd(){ int j,i,k; for(k=1;k<=n;k++) for(i=1;i<=n;i++) for(j=1;j<=n;j++) { if(map[i][j]>map[i][k]+map[k][j]) map[i][j]=map[i][k]+map[k][j]; }}void getmap(){ int i,j,k; memset(e,0,sizeof(e)); for(i=1;i<=n;i++) for(j=1;j<=m;j++) for(k=1;k<=m;k++) scanf("%d",&e[i][j][k]); for(i=0;i<=n;i++) for(j=0;j<=n;j++) { if(i==j) map[i][j]=0; else map[i][j]=inf; } //printf("123\n"); for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { if(i==j) continue; memset(temp,0,sizeof(temp)); int l1,l2,l3; for(l1=1;l1<=m;l1++) for(l2=1;l2<=m;l2++) { temp[l1][l2]=0; for(l3=1;l3<=m;l3++) temp[l1][l2]+=e[i][l1][l3]*e[j][l3][l2]; } for(l1=1;l1<=n;l1++) { if(l1==i||l1==j) continue; int flag=1; for(l2=1;l2<=m;l2++) { for(l3=1;l3<=m;l3++) { if(e[l1][l2][l3]!=temp[l2][l3]) { flag=0; break; } } if(flag==0) break; } if(flag) map[i][l1]=1; } } } //printf("345\n"); floyd();}int main(){ while(scanf("%d%d",&n,&m)) { if(n==0&&m==0) break; getmap(); int k; scanf("%d",&k); int i; for(i=0;i<k;i++) { int a,b; scanf("%d%d",&a,&b); if(map[a][b]>=inf) printf("Sorry\n"); else printf("%d\n",map[a][b]); } } return 0;}
- hdu2807
- hdu2807
- HDU2807
- hdu2807---The Shortest Path
- HDU2807:The Shortest Path(Floyd)
- hdu2807 矩阵乘法建图加 佛洛依德
- hdu2807之矩阵乘法+最短路
- hdu2807(floyd +快速矩阵计算)
- HDU2807:The Shortest Path(矩阵乘法+快速矩阵比较+Floyd)
- Java体系结构介绍
- System.InvalidOperationException: 未在本地计算机上注册“Microsoft.Jet.OLEDB.
- Android平台下SeeJoPlayer视频播放器
- Android View.post(Runnable )
- spring tx:advice 和 aop:config 配置事务
- hdu2807
- linux shell 不同进制数据转换(二进制,八进制,十六进制,base64)
- Codeforces Round #178 (Div. 2)——A,B
- 如何检查 MDAC 版本
- Instrumentation 两种方法 premain Agent
- 《程序员的第一年》----------多页面要登录时,用继承基类方法
- PHP Simple HTML DOM解析器使用入门
- sgu 365
- linux下分析系统cpu使用率