「POJ3613」Cow Relays

传送门

就一个思想:\(N\) 遍 \(\text{Floyd}\) 求出经过 \(N\) 个点的最短路

看一眼数据范围,想到离散化+矩阵快速幂

代码:

#include <cstring>
#include <cstdio>
#define rg register
#define file(x) freopen(x".in", "r", stdin), freopen(x".out", "w", stdout)
template < class T > inline void chkmin(T& a, const T& b) { if (a > b) a = b; }
template < class T > inline void read(T& s) {
s = 0; int f = 0; char c = getchar();
while ('0' > c || c > '9') f |= c == '-', c = getchar();
while ('0' <= c && c <= '9') s = s * 10 + c - 48, c = getchar();
s = f ? -s : s;
} const int _ = 502, __ = 1e6 + 5; int n, m, s, t, tot, id[__]; struct Matrix {
int a[_][_];
inline void init() { memset(a, 0x3f, sizeof a); }
inline int* operator [] (const int& id) { return a[id]; }
inline Matrix operator * (const Matrix& b) const {
Matrix ans; ans.init();
for (rg int k = 1; k <= tot; ++k)
for (rg int i = 1; i <= tot; ++i)
for (rg int j = 1; j <= tot; ++j)
chkmin(ans.a[i][j], a[i][k] + b.a[k][j]);
return ans;
}
} f; inline Matrix power(Matrix x, int k) {
Matrix res = x; --k;
for (; k; k >>= 1, x = x * x)
if (k & 1) res = res * x;
return res;
} int main() {
#ifndef ONLINE_JUDGE
file("cpp");
#endif
read(n), read(m), read(s), read(t), f.init();
for (rg int u, v, w; m--; ) {
read(w), read(u), read(v);
if (!id[u]) id[u] = ++tot;
if (!id[v]) id[v] = ++tot;
f[id[u]][id[v]] = f[id[v]][id[u]] = w;
}
Matrix res = power(f, n);
printf("%d\n", res[id[s]][id[t]]);
return 0;
}

最新文章

  1. [Intel Edison开发板] 04、Edison开发基于nodejs和redis的服务器搭建
  2. 推荐一个iOS关于颜色的库-Wonderful
  3. 十二 个经典 Linux 进程管理命令介绍
  4. 【BZOJ】【1177】【APIO2009】Oil
  5. 【学习笔记】【C语言】算术运算
  6. [Andrew]Ext.net前台弹框
  7. I/O端口与I/O内存
  8. 如何用.NET创建Windows服务
  9. ASP.NET MVC Controller向View传值的几种方式
  10. C#图像处理——ImageProcessor
  11. Php如何返回json数据,前后端分离的基本解决方案
  12. DAS、SAN和NAS三种存储方式
  13. VUE实现登录然后跳转到原来的页面
  14. POJ 2689 - Prime Distance - [埃筛]
  15. iOS 新浪微博-5.1 首页微博列表_时间/配图
  16. 【CSS】定义元素的位置
  17. English trip -- VC(情景课)10 C I like to watch TV. 我爱看电视
  18. laravel实现定时器功能
  19. js字符串解析成数字
  20. vue3.0环境最新安装步骤

热门文章

  1. 基于G6画个xmind出来
  2. 「JSOI2013」旅行时的困惑
  3. cmd创建用户开启3389命令
  4. 使用$.ajax时的注意事项
  5. python opencv:摄像头捕获图像
  6. springboot2.x整合redis
  7. 寒假pta二
  8. 显ipQQ
  9. 安装pytorch
  10. 【Java excel】导出excel文件