poj 3613 经过k条边最短路 floyd+矩阵快速幂
http://poj.org/problem?id=3613
s->t上经过k条边的最短路
先把1000范围的点离散化到200中,然后使用最短路可以使用floyd,由于求的是经过k条路的最短路,跑k-1次“floyd”即可(使用矩阵快速幂的思想)。
把给定的图转为邻接矩阵,即A(i,j)=1当且仅当存在一条边i->j。令C=A*A,那么C(i,j)=ΣA(i,k)*A(k,j),实际上就等于从点i到点j恰好经过2条边的路径数(枚举k为中转点)。类似地,C*A的第i行第j列就表示从i到j经过3条边的路径数。
- #include <cstdio>
- #include <cstdlib>
- #include <cmath>
- #include <cstring>
- #include <string>
- #include <queue>
- #include <map>
- #include <iostream>
- #include <sstream>
- #include <algorithm>
- using namespace std;
- #define RD(x) scanf("%d",&x)
- #define RD2(x,y) scanf("%d%d",&x,&y)
- #define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
- #define clr0(x) memset(x,0,sizeof(x))
- #define clr1(x) memset(x,-1,sizeof(x))
- #define eps 1e-9
- const double pi = acos(-1.0);
- typedef long long LL;
- const int inf = 0x7fffffff;
- const int maxn = 205;
- map <int , int> mp;
- int k,m,st,en;
- int n;//floyd矩阵大小
- struct Matrix{
- int mat[maxn][maxn];
- void init(){
- for(int i = 0;i < maxn;++i)
- for(int j = 0;j < maxn;++j)
- mat[i][j] = inf;
- }
- };
- Matrix ans,tmp,_tmp;
- void copy(Matrix &b,Matrix a){
- for(int i = 1;i <= n;++i)
- for(int j = 1;j <= n;++j)
- b.mat[i][j] = a.mat[i][j];
- }
- void floyd(Matrix &a,Matrix b,Matrix c){
- a.init();
- for(int k = 1;k <= n;++k)
- for(int i = 1;i <= n;++i)
- for(int j = 1;j <= n;++j){
- if(b.mat[i][k] == inf || c.mat[k][j] == inf)
- continue;
- if(a.mat[i][j] > b.mat[i][k]+c.mat[k][j])
- a.mat[i][j] = b.mat[i][k]+c.mat[k][j];
- }
- }
- int has[1005];
- void init()
- {
- n = 0;
- clr0(has);
- ans.init(),tmp.init();
- for(int i = 0;i < maxn;++i)
- ans.mat[i][i] = 0;
- int u,v,w;
- for(int i = 0;i < m;++i){
- RD3(w,u,v);
- if(has[u] == 0)
- has[u] = ++n;
- if(has[v] == 0)
- has[v] = ++n;
- if(tmp.mat[has[u]][has[v]] > w)
- tmp.mat[has[v]][has[u]] = tmp.mat[has[u]][has[v]] = w;
- }
- }
- void work()
- {
- while(k){
- if(k&1){
- copy(_tmp,ans);
- floyd(ans,_tmp,tmp);
- }
- copy(_tmp,tmp);
- floyd(tmp,_tmp,_tmp);
- k>>=1;
- }
- printf("%d\n",ans.mat[has[st]][has[en]]);
- }
- int main()
- {
- while(~RD2(k,m)){
- RD2(st,en);
- init();
- work();
- }
- return 0;
- }
poj 3613 经过k条边最短路 floyd+矩阵快速幂的更多相关文章
- POJ 3631 Cow Relays Floyd+矩阵快速幂
题目描述 For their physical fitness program, N (2 ≤ N ≤ 1,000,000) cows have decided to run a relay race ...
- POJ 3613 floyd+矩阵快速幂
题意: 求s到e恰好经过n边的最短路 思路: 这题已经被我放了好长时间了. 原来是不会矩阵乘法,快速幂什么的也一知半解 现在终于稍微明白了点了 其实就是把矩阵乘法稍微改改 改成能够满足结合律的矩阵&q ...
- POJ 3070 Fibonacci【斐波那契数列/矩阵快速幂】
Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17171 Accepted: 11999 Descr ...
- poj3613Cow Relays——k边最短路(矩阵快速幂)
题目:http://poj.org/problem?id=3613 题意就是求从起点到终点的一条恰好经过k条边的最短路: floyd+矩阵快速幂,矩阵中的第i行第j列表示从i到j的最短路,矩阵本身代表 ...
- POJ 3233 Matrix Power Series (矩阵快速幂+二分求解)
题意:求S=(A+A^2+A^3+...+A^k)%m的和 方法一:二分求解S=A+A^2+...+A^k若k为奇数:S=(A+A^2+...+A^(k/2))+A^(k/2)*(A+A^2+...+ ...
- poj 3613 Cow Relays【矩阵快速幂+Floyd】
!:自环也算一条路径 矩阵快速幂,把矩阵乘法的部分替换成Floyd(只用一个点扩张),这样每"乘"一次,就是经过增加一条边的最短路,用矩阵快速幂优化,然后因为边数是100级别的,所 ...
- 【66测试20161115】【树】【DP_LIS】【SPFA】【同余最短路】【递推】【矩阵快速幂】
还有3天,今天考试又崩了.状态还没有调整过来... 第一题:小L的二叉树 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣.所以,小L当时卡在了二叉树. ...
- POJ 3744 【矩阵快速幂优化 概率DP】
搞懂了什么是矩阵快速幂优化.... 这道题的重点不是DP. /* 题意: 小明要走某条路,按照个人兴致,向前走一步的概率是p,向前跳两步的概率是1-p,但是地上有地雷,给了地雷的x坐标,(一维),求小 ...
- 矩阵快速幂 POJ 3070 Fibonacci
题目传送门 /* 矩阵快速幂:求第n项的Fibonacci数,转置矩阵都给出,套个模板就可以了.效率很高啊 */ #include <cstdio> #include <algori ...
随机推荐
- c# webform网站图片另存代码
好辛苦生成了二维码,然后要实现点击“保存图片”,弹出选择路径进行保存的效果. look: string qrcodeurl = ""; string username = &quo ...
- CSS3基础01
一.选择器: 分为关系选择器 ,属性选择器 ,伪类选择器 1.1关系选择器 后代选择器 ul li 选择所有的后代元素 子代选择器 ul > li 选择ul的儿子 紧邻选择器 .b ...
- ArcGIS API for Flex实现GraphicsLayer上画点、线、面。
目的: ArcGIS API for Flex实现GraphicsLayer上画点.线.面. 准备工作: 1.这次地图数据就用Esri提供的http://server.arcgisonline.com ...
- C++ 中int,char,string,CString类型转换
1. c++中string到int的转换 1) 在C标准库里面,使用atoi: #include <cstdlib> #include <string> std::stri ...
- 自定义底部tab
public class MainActivity extends TabActivity implements OnCheckedChangeListener { private RadioGrou ...
- 灭顶之灾之网络电视精灵——S2 2.8
从前,有一个神奇的东西叫做搞搞精灵 关于他,有一段历史. 哎呀!我去!写不下去了. -.-以上玩笑 首先需求分析 TreeView显示两种频道 TypeA和TypeB 所以创建三个类 ChannelB ...
- C#语法灵活运用之排列组合算法
今天群里有朋友求一个排列组合算法,题目是给定长度,输出所有指定字母的组合. 如指定字母a.b.c.d.e.f,长度为2,则结果应为:aa.ab.ac ... ef.ff. 有朋友给出算法,很有特色: ...
- 单片机TM4C123学习(九):PWM
1.头文件与变量定义 #include "tiva_pwm.h" // PWM 2.初始化 // PWM 初始化,频率为1000,占空比为0 M1PWM7_init(, ); // ...
- NVelocity 表格行奇偶样式变换
#foreach($test in $tests) #even <tr class="Test1"> #odd <tr class="Test2&quo ...
- [转载] 散列表(Hash Table) 从理论到实用(下)
转载自: 白话算法(6) 散列表(Hash Table) 从理论到实用(下) [澈丹,我想要个钻戒.][小北,等等吧,等我再修行两年,你把我烧了,舍利子比钻戒值钱.] ——自扯自蛋 无论开发一个程序还 ...