传送门啦

倍增 $ Floyd $

注意结构体里二维数组不能开到 $ 2000 $

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. #define Re register
  6. using namespace std;
  7. inline int read(){
  8. char ch = getchar();
  9. int f = 1 , x = 0;
  10. while(ch > '9' || ch < '0') {if(ch == '-' ) f = -1 ; ch = getchar();}
  11. while(ch >= '0' && ch <= '9') {x = (x << 1) + (x << 3) + ch - '0' ; ch = getchar();}
  12. return x * f;
  13. }
  14. long long n,tot,m,s,t,x,y,z;
  15. long long vis[20050],cnt;
  16. long long w[2050][2050];
  17. struct node{
  18. long long f[205][205];
  19. node() {
  20. memset(f , 0x3f , sizeof(f)) ;
  21. }
  22. }ans ;
  23. node mul(node a , node b) {
  24. node res ;
  25. for(Re int k = 1 ; k <= tot ; ++k)
  26. for(Re int i = 1 ; i <= tot ; ++i)
  27. for(Re int j = 1 ; j <= tot ; ++j)
  28. if(res.f[i][j] > a.f[i][k] + b.f[k][j])
  29. res.f[i][j] = a.f[i][k] + b.f[k][j];
  30. return res;
  31. }
  32. inline long long quick_power(long long k){
  33. node res ;
  34. for(Re int i = 1 ; i <= tot ; ++i)
  35. res.f[i][i] = 0;
  36. while(k) {
  37. if(k & 1) res = mul(res , ans);
  38. ans = mul(ans , ans ) ;
  39. k >>= 1;
  40. }
  41. return res.f[vis[s]][vis[t]] ;
  42. }
  43. int main(){
  44. n = read(); m = read(); s = read(); t = read();
  45. for(Re int i = 1 ; i <= m ; ++i) {
  46. z = read() ; x = read() ; y = read() ;
  47. if(!vis[x]) vis[x] = ++tot;
  48. if(!vis[y]) vis[y] = ++tot;
  49. ans.f[vis[x]][vis[y]] = ans.f[vis[y]][vis[x]] = min(ans.f[vis[x]][vis[y]] , z) ;
  50. }
  51. printf("%lld\n" , quick_power(n));
  52. return 0;
  53. }

洛谷P2886牛继电器的更多相关文章

  1. 洛谷 [P2886] 牛继电器Cow Relays

    最短路 + 矩阵快速幂 我们可以改进矩阵快速幂,使得它适合本题 用图的邻接矩阵和快速幂实现 注意 dis[i][i] 不能置为 0 #include <iostream> #include ...

  2. [洛谷P2886] 牛继电器Cow Relays

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

  3. 洛谷P2886 [USACO07NOV]牛继电器Cow Relays

    题意很简单,给一张图,把基本的求起点到终点最短路改成求经过k条边的最短路. 求最短路常用的算法是dijkstra,SPFA,还有floyd. 考虑floyd的过程: c[i][j]=min(c[i][ ...

  4. 洛谷 P2886 [USACO07NOV]牛继电器Cow Relays

    题面 解题思路 ## floyd+矩阵快速幂,跟GhostCai爷打赌用不用离散化,最后完败..GhostCai真是tql ! 有个巧妙的方法就是将节点重新编号,因为与节点无关. 代码 #includ ...

  5. 洛谷银牛派对SPFA

    题目描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the b ...

  6. 洛谷P1522 牛的旅行 Cow Tours

    ---恢复内容开始--- P1522 牛的旅行 Cow Tours189通过502提交题目提供者该用户不存在标签 图论 USACO难度 提高+/省选-提交该题 讨论 题解 记录 最新讨论 输出格式题目 ...

  7. 【Luogu】P2886牛继电器(矩阵加速floyd)

    题目链接 矩阵加速floyd……牛逼牛逼. 注意离散化,注意更新的时候要用旧的权值矩阵更新. #include<cstdio> #include<cstring> #inclu ...

  8. 洛谷 P1522 牛的旅行 Cow Tours 题解

    P1522 牛的旅行 Cow Tours 题目描述 农民 John的农场里有很多牧区.有的路径连接一些特定的牧区.一片所有连通的牧区称为一个牧场.但是就目前而言,你能看到至少有两个牧区通过任何路径都不 ...

  9. 洛谷P1565 牛宫

    题目描述 AP 神牛准备给自己盖一座很华丽的宫殿.于是,他看中了一块N*M 的矩形空地. 空地中每个格子都有自己的海拔高度.AP 想让他的宫殿的平均海拔在海平面之上(假设 海平面的高度是0,平均数都会 ...

随机推荐

  1. Java之Stream流

    Stream流的初步学习 初次学习Stream流的学习笔记,学习之前先了解一下函数式接口 概述 API是一个程序向使用者提供的一些方法,通过这些方法就能实现某些功能.所以对于流API来 说,重点是怎么 ...

  2. maven使用内嵌tomcat7

    在web项目的pom.xml中添加如下: <build> <plugins> <plugin> <groupId>org.apache.tomcat.m ...

  3. C++析构函数的自动调用(用于父类指针指向子类对象,内存泄漏问题)

    class A {public:A() { printf("A \n"); }~A() { printf(" ~A \n"); } // 这里不管写不写virt ...

  4. web项目中日志管理工具的使用

    在web项目中,很多时候会用到日志管理工具,常见的日志管理用具有:JDK logging(配置文件:logging.properties) 和log4j(配置文件:log4j.properties) ...

  5. Linux可执行文件后缀问题

    一般来说,可执行文件没有扩展名. Linux不根据扩展名判断文件类型,而是根据文件的内容来判断.所以扩展名的作用是帮助人来识别文件,对于Linux系统本身来说没有什么用处. .sh结尾表示是shell ...

  6. 01 C++ 多线程入门实例

    1.可复用的完整实例 #include <iostream> #include <thread> #include <mutex> using namespace ...

  7. html5 +css3 点击后水波纹扩散效果 兼容移动端

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  8. RAC转换传统的通信

    ///////////////////各种机制转信号/////////////////////////////// 1.UI事件 [self.logInButton rac_signalForCont ...

  9. bootstrap-switch

    首先需要引入bootstrap的css和js文件,再引入bootstrap-switch.css和bootstrap-switch.js文件 <script type="text/ja ...

  10. git爬坑不完全指北(一):Permission to xxx.git denied to user的解决方案

    由于对之前github账户名不太满意,又不像大神一样需要两个账号绑定.所以今天注册了新账号,想把电脑绑定到新账号上,结果就踩坑了……报错如标题. 在网上按照这个题目可以搜索出一大堆的解决方案,但都是一 ...