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. 虚拟机最佳实践:单个 VM、临时存储和已上传磁盘

    大家好! 我是 Drew McDaniel,来自 Microsoft Azure虚拟机功能研发团队,我从团队成立之初就已加入. 在本博客文章中,我将分享一些最佳实践指南,帮助您充分利用您的Azure虚 ...

  2. VC++如何在程序中用代码注册和卸载ocx控件(代码)

    方法一:在dos或Windows命令行下运行:regsvr32 ocxname.ocx 注册 示例:regsvr32 netshare.ocx     //注册netshare.ocx控件regsvr ...

  3. 深入理解linux网络技术内幕读书笔记(四)--通知链

    Table of Contents 1 概述 2 定义链 3 链注册 4 链上的通知事件 5 网络子系统的通知链 5.1 包裹函数 5.2 范例 6 测试实例 概述 [注意] 通知链只在内核子系统之间 ...

  4. Spring中给Bean注入集合

    Spring中如果一个Bean里含有集合元素,需要给Bean里的集合元素注入元素时,可以采用如下方法,一个是构造器注入,一个是setter注入 JavaBean源代码: import java.uti ...

  5. phpcms:八、show.html

    标题:{$title}来源:{$copyfrom}评论:<a href="#comment_iframe" id="comment">0</a ...

  6. centerOS安装chkrootkit

    Chkrootkit是一个在本地系统检查rootkit痕迹的工具,它是检查系统二进制文件是否被rootkit病毒修改的一个shell脚本. (1)centerOS安装chkrootkit 安装gcc编 ...

  7. 关于ognl.OgnlException: target is null for setProperty(null的解决方案

    在跑struts2的时候有时候会出现上面的错,特别是新手, 这种情况是在struts2高级的POJO访问时候出现的s 警告: Error setting expression 'user.passwo ...

  8. js练习【DOM操作】

    完成效果: 演示地址:http://codepen.io/anon/pen/jPbYWq HTML: <!DOCTYPE html> <html lang="en" ...

  9. Android AdapterView View的复用机制 分析

    对于ListView.GridView相信大家都不陌生,重写个BaseView,实现对于的几个方法,然后就完毕了我们的界面展示.而且在大部分情况下,我们载入特别多的Item也不会发生OOM,大家也都明 ...

  10. windows下绑定线程(进程)到指定的CPU核心

    一个程序指定到单独一个CPU上运行会比不指定CPU运行时快.这中间主要有两个原因:1)CPU切换时损耗的性能.2)Intel的自动降频技术和windows的机制冲突:windows有一个功能是平衡负载 ...