hdu 2807 The Shortest Path(矩阵相乘+floyd)
来源:互联网 发布:unity3d特效包下载 编辑:程序博客网 时间:2024/06/05 13:28
http://acm.hdu.edu.cn/showproblem.php?pid=2807
大致题意:给出n个m*m的矩阵,若存在三个互异的矩阵满足a*b = c,那么a,c之间存在权值为1的单向边。有询问u,v,输出uv之间的最短距离。
#include <stdio.h>#include <iostream>#include <algorithm>#include <set>#include <map>#include <vector>#include <math.h>#include <string.h>#include <queue>#include <string>#define LL long long#define _LL __int64using namespace std;const int INF = 0x3f3f3f3f;const int maxn = 85;int n,m;int a[maxn][maxn][maxn];int Map[maxn][maxn];void init(){for(int i = 1; i <= n; i++){for(int j = 1; j <= n; j++){if(i == j) Map[i][j] = 0;else Map[i][j] = INF;}}for(int i = 1; i <= n; i++){for(int j = 1; j <= m; j++){for(int k = 1; k <= m; k++)scanf("%d",&a[i][j][k]);}}for(int i = 1; i <= n; i++){for(int j = 1; j <= n; j++){if(i == j) continue;//任意两个矩阵相乘int tmp[maxn][maxn];memset(tmp,0,sizeof(tmp));for(int x = 1; x <= m; x++){for(int y = 1; y <= m; y++){for(int z = 1; z <= m; z++)tmp[x][y] += a[i][x][z] * a[j][z][y];}}//枚举与tmp相等的矩阵for(int k = 1; k <= n; k++){if(k == i || k == j) continue;int flag = 1;for(int x = 1; x <= m; x++){for(int y = 1; y <= m; y++){if(a[k][x][y] != tmp[x][y]){flag = 0;break;}}if(!flag) break;}if(flag)Map[i][k] = 1;}}}}void floyd(){for(int k = 1; k <= n; k++){for(int i = 1; i <= n; i++){for(int j = 1; j <= n; j++){if(Map[i][j] > Map[i][k] + Map[k][j])Map[i][j] = Map[i][k] + Map[k][j];}}}}int main(){while(~scanf("%d %d",&n,&m)){if(n == 0 && m == 0) break;init();floyd();int k;scanf("%d",&k);while(k--){int u,v;scanf("%d %d",&u,&v);if(Map[u][v] == INF)printf("Sorry\n");else printf("%d\n",Map[u][v]);}}return 0;}
0 0
- hdu 2807 The Shortest Path(矩阵相乘+floyd)
- HDU 2807 The Shortest Path(矩阵相乘+Floyd)
- HDU - 2807The Shortest Path(矩阵相乘+Floyd)
- Hdu 2807 The Shortest Path 矩阵相乘
- hdu 2807 The Shortest Path(矩阵计算+Floyd)
- hdu 2807 The Shortest Path 优化矩阵运算+floyd
- HDOJ 2807 - The Shortest Path 读题细节+矩阵相乘+Floyd
- hdu 2807 The Shortest Path(矩阵)
- HDU 2807 The Shortest Path 把矩阵抽象成点+Floyd
- hdu 2807 The Shortest Path【暴力/矩阵判等优化+Floyd】
- HDU2807:The Shortest Path(矩阵乘法+快速矩阵比较+Floyd)
- HDU 2807 The Shortest Path 矩阵运算+最短路
- hdu 3631 Shortest Path(floyd插点法)
- hdu 3631 Shortest Path(Floyd)
- hdu 3631 Shortest Path(Floyd)
- hdu 3631 Shortest Path (floyd)
- HDU 5636 Shortest Path(Floyd)
- hdu 2807 The Shortest Path
- 关于c语言内存地址对齐的一点思考
- C++虚析构函数
- PHP命名空间(Namespace)的使用详解
- 有关图像分割算法的几个网站
- NOJ [1358] An Easy Problem
- hdu 2807 The Shortest Path(矩阵相乘+floyd)
- 关于c语言等于运算符的一点思考
- vs2012编译xapian问题2 ------U1077,返回代码0x2
- 完全背包 nyoj 311
- 自动创建设备文件
- QQ中抖动窗口的vc++实现方法
- 关于malloc使用经历
- Algorithm学习笔记 --- Just a Hook
- 第7周作业