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. ASP.NET页面事件顺序

    当一个页面请求发送到WEB服务器时,不论该事件是由页面提交还是由页面重定向而激发的,页面在其被创建到释放的过程中都会运行一系列的事件.一个ASP.NET页面从悲怆见到释放的过程包含10个事件. (1) ...

  2. VS2015 C#6.0 中的那些新特性(转载)

    自动属性初始化 (Initializers for auto-properties) 以前我们是这么写的 为一个默认值加一个后台字段是不是很不爽,现在我们可以这样写 只读属性的初始化(Getter-o ...

  3. Struts2标签库之iterator

    传说中的第一种方式,这种在Struts2.1权威指南的例子中也木有说明白: <%@ page language="java" contentType="text/h ...

  4. Memcached和Redis异同

    在考虑对应用程序的性能表现进行提升时,缓存机制往往是解决问题的重要起点,而Memcached与Redis则经常被作为初步方案来加以比较.这两套声名显赫的缓存引擎拥有着诸多相似之处,但它们同样也具备大量 ...

  5. topas top vmstat

    Linux监控脚本 http://www.jb51.net/article/58799.htm  http://blog.csdn.net/icescream6/article/details/456 ...

  6. python wmi模块学习

    # -*- coding: cp936 -*- import wmi c = wmi.WMI () for sys in c.Win32_OperatingSystem(): print " ...

  7. Android开发常用工具汇总

    Android开发常用工具汇总,本文章不断更新完善 一.数据库小工具Sqlite Developer  SQLite,是一款轻型的数据库,是遵守ACID的关系型数据库管理系统,它的设计目标是嵌入式的, ...

  8. [置顶] Android安全机制分析

    Android系统是基于Linux内核开发的,因此,Android系统不仅保留和继承了Linux操作系统的安全机制,而且其系统架构的各个层次都有独特的安全特性[2] . 1. Linux内核层安全机制 ...

  9. [Redux] Extracting Presentational Components -- TodoApp

    Finally, I just noticed that the to-do app component doesn't actually have to be a class. I can turn ...

  10. Internetmap.apk实现原理分析

    1.本地实现调用 程序根据data文件目录下的asinfo.json文件(包含自治域网络名和对应的坐标值),调用so文件绘制asn结点图(ASN,AutoSystemNode,自治域结点) 2.路由查 ...