http://acm.hdu.edu.cn/showproblem.php?pid=2807

第一次做矩阵乘法,没有优化超时,看了别人的优化的矩阵乘法,就过了。

 #include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#define maxn 100
using namespace std;
const int inf=<<; int n,m,t1,x,y;
int g[maxn][maxn];
int d[maxn][maxn];
int aa[maxn][maxn][maxn]; void deal(int a,int b)
{
for(int i=; i<=m; i++)
{
d[][i]=;
for(int j=; j<=m; j++)
{
d[][i]+=(aa[a][i][j]*d[b][j]);
}
}
for(int i=; i<=n; i++)
{
if(i==a||i==b) continue;
bool flag=false;
for(int k=; k<=m; k++)
{
if(d[][k]!=d[i][k])
{
flag=true;
break;
}
}
if(!flag) g[a][i]=;
}
} int main()
{
while(cin>>n>>m)
{
if(n==&&m==) break;
for(int i=; i<=n; i++)
{
for(int j=; j<=m; j++)
{
d[i][j]=;
for(int k=; k<=m; k++)
{
scanf("%d",&aa[i][j][k]);
d[i][j]+=(aa[i][j][k]*k);
}
}
}
for(int i=; i<=n; i++)
{
for(int j=i+; j<=n; j++)
{
g[i][j]=g[j][i]=inf;
}
}
for(int i=; i<=n; i++)
{
for(int j=; j<=n; j++)
{
if(i==j) continue;
deal(i,j);
}
}
for(int k=; k<=n; k++)
{
for(int i=; i<=n; i++)
{
for(int j=; j<=n; j++)
{
if(g[i][j]>g[i][k]+g[k][j])
{
g[i][j]=g[i][k]+g[k][j];
}
}
}
}
scanf("%d",&t1);
for(int i=; i<t1; i++)
{
scanf("%d%d",&x,&y);
if(g[x][y]!=inf) printf("%d\n",g[x][y]);
else printf("Sorry\n");
}
}
return ;
}

hdu 2807 The Shortest Path的更多相关文章

  1. hdu 2807 The Shortest Path(矩阵+floyd)

    The Shortest Path Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  2. Hdu 4725 The Shortest Path in Nya Graph (spfa)

    题目链接: Hdu 4725 The Shortest Path in Nya Graph 题目描述: 有n个点,m条边,每经过路i需要wi元.并且每一个点都有自己所在的层.一个点都乡里的层需要花费c ...

  3. HDU 4725 The Shortest Path in Nya Graph [构造 + 最短路]

    HDU - 4725 The Shortest Path in Nya Graph http://acm.hdu.edu.cn/showproblem.php?pid=4725 This is a v ...

  4. HDU 2224 The shortest path

    The shortest path Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  5. HDU 4725 The Shortest Path in Nya Graph

    he Shortest Path in Nya Graph Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged o ...

  6. (中等) HDU 4725 The Shortest Path in Nya Graph,Dijkstra+加点。

    Description This is a very easy problem, your task is just calculate el camino mas corto en un grafi ...

  7. HDU 4725 The Shortest Path in Nya Graph(构图)

    The Shortest Path in Nya Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  8. HDU 4725 The Shortest Path in Nya Graph (最短路)

    The Shortest Path in Nya Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  9. HDU 4725 The Shortest Path in Nya Graph(最短路径)(2013 ACM/ICPC Asia Regional Online ―― Warmup2)

    Description This is a very easy problem, your task is just calculate el camino mas corto en un grafi ...

随机推荐

  1. hdu Big Number

    #include <cstdio> #include <cstring> #include <cmath> using namespace std; int mai ...

  2. 用keil怎么像makefile那样选择哪些文件进行编译?

    因为设备有多种不同的型号的硬件,所以就有不同的驱动,我想在编译的时候,像在linux下的makeile那样,自己写一个编译连接的东西,来控制我哪些文件进行编译链接,不知道在keil下有没有这样的方法. ...

  3. Thunderbird Mail 支持 Microsoft Exchange

    Thunderbird地址:https://www.mozilla.org/en-US/thunderbird/ 插件下载地址:https://addons.mozilla.org/zh-CN/thu ...

  4. ubuntu记录

    1. gleboneblack OMAPES=4.x ANDROID_ROOT_DIR=$HOME/aosp W=1 install /bin/sh: 5: ./install.sh: Permiss ...

  5. FreeBSd ports 安装软件

    1.ports的目录在/usr/ports2.POSTS安装软件有时可能这个包已经安装过了,会有提示,无法 继续安装.能够用提示中的参数:#make install clean FORCE_PKG_R ...

  6. (转)iOS7界面设计规范(4) - UI基础 - 布局

    好吧,第四篇的样子,节奏还好:我怎么又是刚吃过小龙虾的状况呢. 今天的话题是布局,篇幅很短:我自己好喜欢这样,在还有些剩余时间和精力的情况下,一个话题一个话题的推进,做做译文,并热切的希望这些内容在自 ...

  7. 深度剖析JDK动态代理机制

    摘要 相比于静态代理,动态代理避免了开发人员编写各个繁锁的静态代理类,只需简单地指定一组接口及目标类对象就能动态的获得代理对象. 代理模式 使用代理模式必须要让代理类和目标类实现相同的接口,客户端通过 ...

  8. The secret of ROWID

    表里每个数据行都有一个行头部,在这里存放了该行数据所包含的列的数量,以及锁定标记等.当某个事务更新某条记录时,会在该数据行的头部记录所用到的ITL槽号以及锁定标记.接下来则是列长度以及列的值.Orac ...

  9. CF Zepto Code Rush 2014 B. Om Nom and Spiders

    Om Nom and Spiders time limit per test 3 seconds memory limit per test 256 megabytes input standard ...

  10. Opencv 简单的图片显示

    #include <opencv\cv.h> #include <opencv\highgui.h> #include <opencv\cxcore.h> int ...