洛谷P2886牛继电器
传送门啦
倍增 $ Floyd $
注意结构体里二维数组不能开到 $ 2000 $
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define Re register
using namespace std;
inline int read(){
char ch = getchar();
int f = 1 , x = 0;
while(ch > '9' || ch < '0') {if(ch == '-' ) f = -1 ; ch = getchar();}
while(ch >= '0' && ch <= '9') {x = (x << 1) + (x << 3) + ch - '0' ; ch = getchar();}
return x * f;
}
long long n,tot,m,s,t,x,y,z;
long long vis[20050],cnt;
long long w[2050][2050];
struct node{
long long f[205][205];
node() {
memset(f , 0x3f , sizeof(f)) ;
}
}ans ;
node mul(node a , node b) {
node res ;
for(Re int k = 1 ; k <= tot ; ++k)
for(Re int i = 1 ; i <= tot ; ++i)
for(Re int j = 1 ; j <= tot ; ++j)
if(res.f[i][j] > a.f[i][k] + b.f[k][j])
res.f[i][j] = a.f[i][k] + b.f[k][j];
return res;
}
inline long long quick_power(long long k){
node res ;
for(Re int i = 1 ; i <= tot ; ++i)
res.f[i][i] = 0;
while(k) {
if(k & 1) res = mul(res , ans);
ans = mul(ans , ans ) ;
k >>= 1;
}
return res.f[vis[s]][vis[t]] ;
}
int main(){
n = read(); m = read(); s = read(); t = read();
for(Re int i = 1 ; i <= m ; ++i) {
z = read() ; x = read() ; y = read() ;
if(!vis[x]) vis[x] = ++tot;
if(!vis[y]) vis[y] = ++tot;
ans.f[vis[x]][vis[y]] = ans.f[vis[y]][vis[x]] = min(ans.f[vis[x]][vis[y]] , z) ;
}
printf("%lld\n" , quick_power(n));
return 0;
}
洛谷P2886牛继电器的更多相关文章
- 洛谷 [P2886] 牛继电器Cow Relays
最短路 + 矩阵快速幂 我们可以改进矩阵快速幂,使得它适合本题 用图的邻接矩阵和快速幂实现 注意 dis[i][i] 不能置为 0 #include <iostream> #include ...
- [洛谷P2886] 牛继电器Cow Relays
问题描述 For their physical fitness program, N (2 ≤ N ≤ 1,000,000) cows have decided to run a relay race ...
- 洛谷P2886 [USACO07NOV]牛继电器Cow Relays
题意很简单,给一张图,把基本的求起点到终点最短路改成求经过k条边的最短路. 求最短路常用的算法是dijkstra,SPFA,还有floyd. 考虑floyd的过程: c[i][j]=min(c[i][ ...
- 洛谷 P2886 [USACO07NOV]牛继电器Cow Relays
题面 解题思路 ## floyd+矩阵快速幂,跟GhostCai爷打赌用不用离散化,最后完败..GhostCai真是tql ! 有个巧妙的方法就是将节点重新编号,因为与节点无关. 代码 #includ ...
- 洛谷银牛派对SPFA
题目描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the b ...
- 洛谷P1522 牛的旅行 Cow Tours
---恢复内容开始--- P1522 牛的旅行 Cow Tours189通过502提交题目提供者该用户不存在标签 图论 USACO难度 提高+/省选-提交该题 讨论 题解 记录 最新讨论 输出格式题目 ...
- 【Luogu】P2886牛继电器(矩阵加速floyd)
题目链接 矩阵加速floyd……牛逼牛逼. 注意离散化,注意更新的时候要用旧的权值矩阵更新. #include<cstdio> #include<cstring> #inclu ...
- 洛谷 P1522 牛的旅行 Cow Tours 题解
P1522 牛的旅行 Cow Tours 题目描述 农民 John的农场里有很多牧区.有的路径连接一些特定的牧区.一片所有连通的牧区称为一个牧场.但是就目前而言,你能看到至少有两个牧区通过任何路径都不 ...
- 洛谷P1565 牛宫
题目描述 AP 神牛准备给自己盖一座很华丽的宫殿.于是,他看中了一块N*M 的矩形空地. 空地中每个格子都有自己的海拔高度.AP 想让他的宫殿的平均海拔在海平面之上(假设 海平面的高度是0,平均数都会 ...
随机推荐
- Java之Stream流
Stream流的初步学习 初次学习Stream流的学习笔记,学习之前先了解一下函数式接口 概述 API是一个程序向使用者提供的一些方法,通过这些方法就能实现某些功能.所以对于流API来 说,重点是怎么 ...
- maven使用内嵌tomcat7
在web项目的pom.xml中添加如下: <build> <plugins> <plugin> <groupId>org.apache.tomcat.m ...
- C++析构函数的自动调用(用于父类指针指向子类对象,内存泄漏问题)
class A {public:A() { printf("A \n"); }~A() { printf(" ~A \n"); } // 这里不管写不写virt ...
- web项目中日志管理工具的使用
在web项目中,很多时候会用到日志管理工具,常见的日志管理用具有:JDK logging(配置文件:logging.properties) 和log4j(配置文件:log4j.properties) ...
- Linux可执行文件后缀问题
一般来说,可执行文件没有扩展名. Linux不根据扩展名判断文件类型,而是根据文件的内容来判断.所以扩展名的作用是帮助人来识别文件,对于Linux系统本身来说没有什么用处. .sh结尾表示是shell ...
- 01 C++ 多线程入门实例
1.可复用的完整实例 #include <iostream> #include <thread> #include <mutex> using namespace ...
- html5 +css3 点击后水波纹扩散效果 兼容移动端
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- RAC转换传统的通信
///////////////////各种机制转信号/////////////////////////////// 1.UI事件 [self.logInButton rac_signalForCont ...
- bootstrap-switch
首先需要引入bootstrap的css和js文件,再引入bootstrap-switch.css和bootstrap-switch.js文件 <script type="text/ja ...
- git爬坑不完全指北(一):Permission to xxx.git denied to user的解决方案
由于对之前github账户名不太满意,又不像大神一样需要两个账号绑定.所以今天注册了新账号,想把电脑绑定到新账号上,结果就踩坑了……报错如标题. 在网上按照这个题目可以搜索出一大堆的解决方案,但都是一 ...