hdu 2807 The Shortest Path(矩阵)
来源:互联网 发布:怎么筛选excel数据 编辑:程序博客网 时间:2024/06/05 06:03
The Shortest Path
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3006 Accepted Submission(s): 987
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 | We have carefully selected several similar problems for you: 2224 1217 2803 2802 2544
题解:根据题意乱搞即可。。。
#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<cmath>#define N 83#define inf 1000000000using namespace std;int n,m,dis[N][N];struct data{int a[N][N];}point[N];data get(data a,data b){data c;for (int i=1;i<=m;i++) for (int j=1;j<=m;j++) { c.a[i][j]=0; for (int k=1;k<=m;k++) c.a[i][j]=c.a[i][j]+a.a[i][k]*b.a[k][j]; }return c;}bool pd(data a,data b){for (int i=1;i<=m;i++) for (int j=1;j<=m;j++) if (a.a[i][j]!=b.a[i][j]) return 0;return 1;}void calc(){for (int a=1;a<=n;a++) for (int b=1;b<=n;b++) if (a!=b) { data t=get(point[a],point[b]); for (int c=1;c<=n;c++) { if (c==a||c==b) continue; if (pd(point[c],t)) dis[a][c]=1; } }}void floyed(){for (int k=1;k<=n;k++) for (int i=1;i<=n;i++) if (dis[i][k]!=inf) for (int j=1;j<=n;j++) if (dis[k][j]!=inf) if (k!=i&&i!=j&&j!=k) dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);}int main(){while (scanf("%d%d",&n,&m)!=EOF){if (!n&&!m) break;for (int i=1;i<=n;i++) { for (int j=1;j<=m;j++) for (int k=1;k<=m;k++) scanf("%d",&point[i].a[j][k]); }for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) dis[i][j]=inf;calc();floyed();int t; scanf("%d",&t);for (int i=1;i<=t;i++){int x,y; scanf("%d%d",&x,&y);if (dis[x][y]!=inf) printf("%d\n",dis[x][y]);else printf("Sorry\n");}}}
0 0
- hdu 2807 The Shortest Path(矩阵)
- Hdu 2807 The Shortest Path 矩阵相乘
- hdu 2807 The Shortest Path(矩阵相乘+floyd)
- hdu 2807 The Shortest Path(矩阵计算+Floyd)
- HDU 2807 The Shortest Path(矩阵相乘+Floyd)
- HDU - 2807The Shortest Path(矩阵相乘+Floyd)
- hdu 2807 The Shortest Path 优化矩阵运算+floyd
- HDU 2807 The Shortest Path 矩阵运算+最短路
- hdu 2807 The Shortest Path
- hdu 2807 The Shortest Path
- hdu 2807 The Shortest Path
- HDU 2807 The Shortest Path
- HDU 2807 The Shortest Path
- HDU 2807 The Shortest Path
- HDU-2807-The Shortest Path
- HDU 2807 The Shortest Path(最短路+矩阵快速比较)
- HDU The Shortest Path 矩阵的最短路
- hdu 2807 The Shortest Path(最短路)
- Google推荐的图片加载库Glide与Picasso介
- 设计模式学习-单例模式
- React Native项目修改包名(Android版)
- 从客户端 中检测到有潜在危险的Request.Form值
- C语言中的static 详细分析
- hdu 2807 The Shortest Path(矩阵)
- leetcode.30. Substring with Concatenation of All Words
- Android RecyclerView实现瀑布流
- 在路由器上面按IP限速
- cocos2d-x项目实战
- mybatis学习笔记(零)mybatis学习之HelloWorld
- 英文不好,学Unity3D,一件小事搞疯我--(二)
- OpenGLES---初始化OpenGL-ES环境
- hibernate(3)