原题: FZU 2173 http://acm.fzu.edu.cn/problem.php?pid=2173

一开始看到这个题毫无头绪,根本没想到是矩阵快速幂,其实看见k那么大,就应该想到用快速幂什么的,况且n<=50,可以用矩阵来表示图。

1.为什么能用矩阵快速幂呢?

原理:

原始矩阵m[][]中,m[u][v]代表u到v的花费,求矩阵的k次幂后,此时m[u][v]代表,从u走向b经过v步的最少花费
注意此时矩阵的相乘应该写成:
m[a][b]=min(m[a][1]+m[1][b],...m[a][n]+m[n][b])  ,即取最小值而非相加。

2.为什么呢?

m的1次方,无疑是正确的。

m的2次方
此时(m[a][b])^2=min(m[a][1]+m[1][b],..m[a][n]+m[n][b]),就是枚举a经过1到n点再到b的最少花费,就是a经过两步到达b的最少花费

归纳法:

如果(m[a][b])^i代表了a走i步到达b的最少花费,则m^(i+1)=min((m[a][1])^i+m[1][b],...(m[a][n])^i+m[n][b])

所以可以这样做。

(借鉴nothing的博客)

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#define Mod 1000000007
#define lll __int64
using namespace std;
#define N 6007 struct Matrix
{
lll m[][];
};
int n,h,k; Matrix Mul(Matrix a,Matrix b)
{
Matrix c;
memset(c.m,-,sizeof(c.m));
for(int i=;i<n;i++)
for(int j=;j<n;j++)
{
for(int k=;k<n;k++)
{
if(a.m[i][k]!=-&&b.m[k][j]!=-)
{
if(c.m[i][j] == -)
c.m[i][j] = a.m[i][k]+b.m[k][j];
else
c.m[i][j] = min(c.m[i][j],a.m[i][k]+b.m[k][j]);
}
}
}
return c;
} Matrix fastm(Matrix a,int n)
{
if(n == )
return a;
Matrix res = fastm(a,n/);
res = Mul(res,res);
if(n&)
res = Mul(res,a);
return res;
} Matrix MPow(Matrix a,int n) //第二种写法
{
Matrix res = a;
n--;
while(n)
{
if(n&)
res = Mul(res,a);
n>>=;
a = Mul(a,a);
}
return res;
} int main()
{
int t,i,j,k;
int u,v;
lll w;
Matrix A,ans;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&h,&k);
memset(A.m,-,sizeof(A.m));
for(i=;i<h;i++)
{
scanf("%d%d%I64d",&u,&v,&w);
u--,v--;
if(A.m[u][v] == -)
A.m[u][v] = w;
else
A.m[u][v] = min(A.m[u][v],w);
}
ans = MPow(A,k);
printf("%I64d\n",ans.m[][n-]);
}
return ;
}

2014 Super Training #10 G Nostop --矩阵快速幂的更多相关文章

  1. poj 3735 Training little cats(矩阵快速幂,模版更权威,这题数据很坑)

    题目 矩阵快速幂,这里的模版就是计算A^n的,A为矩阵. 之前的矩阵快速幂貌似还是个更通用一些. 下面的题目解释来自 我只想做一个努力的人 @@@请注意 ,单位矩阵最初构造 行和列都要是(猫咪数+1) ...

  2. 2014 Super Training #10 C Shadow --SPFA/随便搞/DFS

    原题: FZU 2169 http://acm.fzu.edu.cn/problem.php?pid=2169 这题貌似有两种解法,DFS和SPFA,但是DFS怎么都RE,SPFA也要用邻接表表示边, ...

  3. 2014 Super Training #8 G Grouping --Tarjan求强连通分量

    原题:ZOJ 3795 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3795 题目大意:给定一个有向图,要求把点分为k个集 ...

  4. 2014 Super Training #4 G What day is that day? --两种方法

    原题: ZOJ 3785 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3785 题意:当天是星期六,问经过1^1+2^2+ ...

  5. 2014 Super Training #10 D 花生的序列 --DP

    原题: FZU 2170 http://acm.fzu.edu.cn/problem.php?pid=2170 这题确实是当时没读懂题目,连样例都没想通,所以没做了,所以还是感觉这样散漫的做不好,有些 ...

  6. 2014 Super Training #6 G Trim the Nails --状态压缩+BFS

    原题: ZOJ 3675 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3675 由m<=20可知,可用一个二进制数表 ...

  7. POJ 3735 Training little cats(矩阵快速幂)

    Training little cats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11787 Accepted: 2892 ...

  8. HDU 5863 cjj's string game ( 16年多校10 G 题、矩阵快速幂优化线性递推DP )

    题目链接 题意 : 有种不同的字符,每种字符有无限个,要求用这k种字符构造两个长度为n的字符串a和b,使得a串和b串的最长公共部分长度恰为m,问方案数 分析 : 直觉是DP 不过当时看到 n 很大.但 ...

  9. Training little cats(poj3735,矩阵快速幂)

    Training little cats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10737   Accepted:  ...

随机推荐

  1. Servlet3.0 Test

    1. Servlet3.0 Test and Annotation used 你可以从tomcat7中lib文件夹中找到servlet-api.jar package com.goodfan.serv ...

  2. Represent code in math equations

    Introduce The article shows a way to use math equations to represent code's logical. Key ideas logic ...

  3. handler机制的原理(转)

      Handler概述   andriod提供了Handler 和 Looper 来满足线程间的通信.Handler先进先出原则.Looper类用来管理特定线程内对象之间的消息交换(MessageEx ...

  4. Hadoop技术内幕(YARN)第4章问题部分答案

    问题1:改写DistributedShell程序,使得每个container运行在不同节点上(目前是随机的,可能运行在任意节点上). 问题2:改写DistributedShell程序,使得某个用户指定 ...

  5. 转:HTTP 1.1与HTTP 1.0的比较

    原文地址:http://blog.csdn.net/elifefly/article/details/3964766 HTTP 1.1与HTTP 1.0的比较 一个WEB站点每天可能要接收到上百万的用 ...

  6. Android实战--短信发送器

    首先设计界面 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:t ...

  7. 【转】c++中引用的全方位解读

    对于习惯使用C进行开发的朋友们,在看到c++中出现的&符号,可能会犯迷糊,因为在C语言中这个符号表示了取地址符,但是在C++中它却有着不同的用途,掌握C++的&符号,是提高代码执行效率 ...

  8. 在阿里云主机的Debian操作系统上安装Docker

    因为需要新搭建饭团网站,所以需要在阿里云的主机上跑数据库,java环境. 考虑到可扩展性和模块化,所以准备最近流行的docker技术.Docker -- 从入门到实践 阿里云主机1核1G,资源不多,所 ...

  9. Effective Java 34 Emulate extensible enums with interfaces

    Advantage Disadvantage Enum types Clarity Safety Ease of maintenance. None extensibility Typesafe en ...

  10. JavaScript Patterns 4.3 Returning Functions

    Use closure to store some private data, which is accessible by the returned function but not to the ...