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条边的路径数。

  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cmath>
  4. #include <cstring>
  5. #include <string>
  6. #include <queue>
  7. #include <map>
  8. #include <iostream>
  9. #include <sstream>
  10. #include <algorithm>
  11. using namespace std;
  12. #define RD(x) scanf("%d",&x)
  13. #define RD2(x,y) scanf("%d%d",&x,&y)
  14. #define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
  15. #define clr0(x) memset(x,0,sizeof(x))
  16. #define clr1(x) memset(x,-1,sizeof(x))
  17. #define eps 1e-9
  18. const double pi = acos(-1.0);
  19. typedef long long LL;
  20. const int inf = 0x7fffffff;
  21. const int maxn = 205;
  22. map <int , int> mp;
  23. int k,m,st,en;
  24. int n;//floyd矩阵大小
  25. struct Matrix{
  26. int mat[maxn][maxn];
  27. void init(){
  28. for(int i = 0;i < maxn;++i)
  29. for(int j = 0;j < maxn;++j)
  30. mat[i][j] = inf;
  31. }
  32. };
  33. Matrix ans,tmp,_tmp;
  34. void copy(Matrix &b,Matrix a){
  35. for(int i = 1;i <= n;++i)
  36. for(int j = 1;j <= n;++j)
  37. b.mat[i][j] = a.mat[i][j];
  38. }
  39. void floyd(Matrix &a,Matrix b,Matrix c){
  40. a.init();
  41. for(int k = 1;k <= n;++k)
  42. for(int i = 1;i <= n;++i)
  43. for(int j = 1;j <= n;++j){
  44. if(b.mat[i][k] == inf || c.mat[k][j] == inf)
  45. continue;
  46. if(a.mat[i][j] > b.mat[i][k]+c.mat[k][j])
  47. a.mat[i][j] = b.mat[i][k]+c.mat[k][j];
  48. }
  49. }
  50. int has[1005];
  51. void init()
  52. {
  53. n = 0;
  54. clr0(has);
  55. ans.init(),tmp.init();
  56. for(int i = 0;i < maxn;++i)
  57. ans.mat[i][i] = 0;
  58. int u,v,w;
  59. for(int i = 0;i < m;++i){
  60. RD3(w,u,v);
  61. if(has[u] == 0)
  62. has[u] = ++n;
  63. if(has[v] == 0)
  64. has[v] = ++n;
  65. if(tmp.mat[has[u]][has[v]] > w)
  66. tmp.mat[has[v]][has[u]] = tmp.mat[has[u]][has[v]] = w;
  67. }
  68. }
  69. void work()
  70. {
  71. while(k){
  72. if(k&1){
  73. copy(_tmp,ans);
  74. floyd(ans,_tmp,tmp);
  75. }
  76. copy(_tmp,tmp);
  77. floyd(tmp,_tmp,_tmp);
  78. k>>=1;
  79. }
  80. printf("%d\n",ans.mat[has[st]][has[en]]);
  81. }
  82. int main()
  83. {
  84. while(~RD2(k,m)){
  85. RD2(st,en);
  86. init();
  87. work();
  88. }
  89. return 0;
  90. }

poj 3613 经过k条边最短路 floyd+矩阵快速幂的更多相关文章

  1. POJ 3631 Cow Relays Floyd+矩阵快速幂

    题目描述 For their physical fitness program, N (2 ≤ N ≤ 1,000,000) cows have decided to run a relay race ...

  2. POJ 3613 floyd+矩阵快速幂

    题意: 求s到e恰好经过n边的最短路 思路: 这题已经被我放了好长时间了. 原来是不会矩阵乘法,快速幂什么的也一知半解 现在终于稍微明白了点了 其实就是把矩阵乘法稍微改改 改成能够满足结合律的矩阵&q ...

  3. POJ 3070 Fibonacci【斐波那契数列/矩阵快速幂】

    Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17171   Accepted: 11999 Descr ...

  4. poj3613Cow Relays——k边最短路(矩阵快速幂)

    题目:http://poj.org/problem?id=3613 题意就是求从起点到终点的一条恰好经过k条边的最短路: floyd+矩阵快速幂,矩阵中的第i行第j列表示从i到j的最短路,矩阵本身代表 ...

  5. 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+...+ ...

  6. poj 3613 Cow Relays【矩阵快速幂+Floyd】

    !:自环也算一条路径 矩阵快速幂,把矩阵乘法的部分替换成Floyd(只用一个点扩张),这样每"乘"一次,就是经过增加一条边的最短路,用矩阵快速幂优化,然后因为边数是100级别的,所 ...

  7. 【66测试20161115】【树】【DP_LIS】【SPFA】【同余最短路】【递推】【矩阵快速幂】

    还有3天,今天考试又崩了.状态还没有调整过来... 第一题:小L的二叉树 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣.所以,小L当时卡在了二叉树. ...

  8. POJ 3744 【矩阵快速幂优化 概率DP】

    搞懂了什么是矩阵快速幂优化.... 这道题的重点不是DP. /* 题意: 小明要走某条路,按照个人兴致,向前走一步的概率是p,向前跳两步的概率是1-p,但是地上有地雷,给了地雷的x坐标,(一维),求小 ...

  9. 矩阵快速幂 POJ 3070 Fibonacci

    题目传送门 /* 矩阵快速幂:求第n项的Fibonacci数,转置矩阵都给出,套个模板就可以了.效率很高啊 */ #include <cstdio> #include <algori ...

随机推荐

  1. c# webform网站图片另存代码

    好辛苦生成了二维码,然后要实现点击“保存图片”,弹出选择路径进行保存的效果. look: string qrcodeurl = ""; string username = &quo ...

  2. CSS3基础01

    一.选择器: 分为关系选择器 ,属性选择器 ,伪类选择器 1.1关系选择器  后代选择器   ul li  选择所有的后代元素 子代选择器   ul > li 选择ul的儿子 紧邻选择器  .b ...

  3. ArcGIS API for Flex实现GraphicsLayer上画点、线、面。

    目的: ArcGIS API for Flex实现GraphicsLayer上画点.线.面. 准备工作: 1.这次地图数据就用Esri提供的http://server.arcgisonline.com ...

  4. C++ 中int,char,string,CString类型转换

      1. c++中string到int的转换 1) 在C标准库里面,使用atoi: #include <cstdlib> #include <string> std::stri ...

  5. 自定义底部tab

    public class MainActivity extends TabActivity implements OnCheckedChangeListener { private RadioGrou ...

  6. 灭顶之灾之网络电视精灵——S2 2.8

    从前,有一个神奇的东西叫做搞搞精灵 关于他,有一段历史. 哎呀!我去!写不下去了. -.-以上玩笑 首先需求分析 TreeView显示两种频道 TypeA和TypeB 所以创建三个类 ChannelB ...

  7. C#语法灵活运用之排列组合算法

    今天群里有朋友求一个排列组合算法,题目是给定长度,输出所有指定字母的组合. 如指定字母a.b.c.d.e.f,长度为2,则结果应为:aa.ab.ac ... ef.ff. 有朋友给出算法,很有特色: ...

  8. 单片机TM4C123学习(九):PWM

    1.头文件与变量定义 #include "tiva_pwm.h" // PWM 2.初始化 // PWM 初始化,频率为1000,占空比为0 M1PWM7_init(, ); // ...

  9. NVelocity 表格行奇偶样式变换

    #foreach($test in $tests) #even <tr class="Test1"> #odd <tr class="Test2&quo ...

  10. [转载] 散列表(Hash Table) 从理论到实用(下)

    转载自: 白话算法(6) 散列表(Hash Table) 从理论到实用(下) [澈丹,我想要个钻戒.][小北,等等吧,等我再修行两年,你把我烧了,舍利子比钻戒值钱.] ——自扯自蛋 无论开发一个程序还 ...