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. DOCKER功能练习

    都是书上的示例,慢慢进入..

  2. Qt Lite

    http://blog.qt.io/blog/2016/08/18/introducing-the-qt-lite-project-qt-for-any-platform-any-thing-any- ...

  3. 前端模拟发送数据-Chrome下的REST Client

    1)确定需要POST的数据 2)拼接数据,POST给服务器 3)查看服务器响应及结果

  4. logstash 通过type判断

    [elk@zjtest7-frontend type]$ cat input.conf input { file { type => "type_a" path => ...

  5. jar包版本冲突,并且要保留两个版本都能使用

    问题:在做项目时,遇到jar版本冲突的问题,并且老代码依赖不能用新jar包代替,要保证功能不变须要保证两个jar都能使用 思路:使用runtime 的exec 方式另启线程运行,然后返回结果 解决: ...

  6. <php>统计目录数和文件数

    $dirn = 0; //目录数 $filen = 0; //文件数 //用来统计一个目录下的文件和目录的个数 function getdirnum($file) { global $dirn; gl ...

  7. 盘点20款表现出众的HTML5游戏

    不管是对用户还是开发者来说,HTML5和JavaScript游戏这几年的发展真的是件好事.随着浏览器平台的日趋成熟,并开始整合这类型游戏所 要求的技术,我们每天都能在各大应用商店和社交网站中看到越来越 ...

  8. 基于Cordova5.0开发自己定义插件(android)

    1.开发插件java部分 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvenhmMjE2MjE2/font/5a6L5L2T/fontsize/400/fi ...

  9. 用Spring提高java观察者模式灵活性

    在上篇博客 用java观察者模式解耦经典三层架构 的最后,用了一个Client类把Listener的实现类注冊到了LoginEventSource类中,假设须要加入�新的逻辑,加入�新的listene ...

  10. [MongoDB] Remove, update, create document

    Remove: remove the wand with the name of "Doom Bringer" from our wandscollection. db.wands ...