「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;
}

「POJ3613」Cow Relays的更多相关文章

  1. poj3613:Cow Relays(倍增优化+矩阵乘法floyd+快速幂)

    Cow Relays Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7825   Accepted: 3068 Descri ...

  2. LG5196 「USACO2019JAN」Cow Poetry 背包+乘法原理

    \(\mathrm{Cow Poetry}\) 问题描述 LG5196 题解 因为每句诗的长度一定是\(k\),所以自然而然想到背包. 设\(opt[i][j]\)代表到第\(i\)位时,结尾为\(j ...

  3. 【POJ3613】Cow Relays 离散化+倍增+矩阵乘法

    题目大意:给定一个 N 个顶点,M 条边的无向图,求从起点到终点恰好经过 K 个点的最短路. 题解:设 \(d[1][i][j]\) 表示恰好经过一条边 i,j 两点的最短路,那么有 \(d[r+m] ...

  4. POJ3613 Cow Relays [矩阵乘法 floyd类似]

    Cow Relays Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7335   Accepted: 2878 Descri ...

  5. poj3613 Cow Relays【好题】【最短路】【快速幂】

    Cow Relays Time Limit: 1000MS   Memory Limit: 65536K Total Submissions:9207   Accepted: 3604 Descrip ...

  6. 「译」JUnit 5 系列:条件测试

    原文地址:http://blog.codefx.org/libraries/junit-5-conditions/ 原文日期:08, May, 2016 译文首发:Linesh 的博客:「译」JUni ...

  7. 「译」JUnit 5 系列:扩展模型(Extension Model)

    原文地址:http://blog.codefx.org/design/architecture/junit-5-extension-model/ 原文日期:11, Apr, 2016 译文首发:Lin ...

  8. JavaScript OOP 之「创建对象」

    工厂模式 工厂模式是软件工程领域一种广为人知的设计模式,这种模式抽象了创建具体对象的过程.工厂模式虽然解决了创建多个相似对象的问题,但却没有解决对象识别的问题. function createPers ...

  9. 「C++」理解智能指针

    维基百科上面对于「智能指针」是这样描述的: 智能指针(英语:Smart pointer)是一种抽象的数据类型.在程序设计中,它通常是经由类型模板(class template)来实做,借由模板(tem ...

随机推荐

  1. Ubuntu 16 安装Nginx+Php+Mysql

    嗯哼,结束外派,我胡汉三又回来了,回来第一件事,就是重新装服务器,搭环境,以前用的apache,最近改了nginx,来吧,从头开始 因为以前一直用apache,这次换一个nginx试试. 1.更新系统 ...

  2. 吴裕雄--天生自然神经网络与深度学习实战Python+Keras+TensorFlow:使用TensorFlow和Keras开发高级自然语言处理系统——LSTM网络原理以及使用LSTM实现人机问答系统

    !mkdir '/content/gdrive/My Drive/conversation' ''' 将文本句子分解成单词,并构建词库 ''' path = '/content/gdrive/My D ...

  3. Go之Gin

    文章引用自 Gin框架介绍及使用 Gin是一个用Go语言编写的web框架.它是一个类似于martini但拥有更好性能的API框架, 由于使用了httprouter,速度提高了近40倍. 如果你是性能和 ...

  4. 分布式事务 --- BASE 理论

    部分图片总结出自参考资料 问题 : Base 理论为什么会被提出,动机是什么 Base 和 ACID 的区别与联系 概述 上一篇我们知道CAP 理论,也知道由于现实中网络等原因,分区容错性这一元素大多 ...

  5. Spring boot+Thymeleaf传参跳转

    $.ajax(): $.ajax({ type: "get", url:"/public/inform", async: true, data: detailJ ...

  6. 下载完idea后需要做的设置

    1.设置字体 2.安装插件 3.设置文件头(C:\Users\用户名\.IntelliJIdea2019.2\config\fileTemplates\includes下有个文件叫做File Head ...

  7. elasticsearch数据组织结构

    elasticsearch数据组织结构 1.      mapping 1.1.    简介 mapping:意为映射关系,特别是指组织结构.在此语境中可理解为数据结构,包括表结构,表约束,数据类型等 ...

  8. JavaScript 闭包&基于闭包实现柯里化和bind

    闭包: 1 函数内声明了一个函数,并且将这个函数内部的函数返回到全局 2 将这个返回到全局的函数内中的函数存储到全局变量中 3 内部的函数调用了外部函数中的局部变量 闭包简述: 有权访问另一个函数局部 ...

  9. SQL 函数 排序 等基础操作 DDL DML DQL 用法和<> <=>等

    sql基础汇总 --根据函数别名排序 --排序规则,默认是升序 sleect LENGTH(NAME) nameLength from user ORDER BY nameLength DESC -- ...

  10. 线程池ExecutorService的使用及其正确关闭方法

    创建一个容量为5的线程池 ExecutorService executorService = Executors.newFixedThreadPool(5); 向线程池提交15个任务,其实就是通过线程 ...