「POJ3613」Cow Relays
「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的更多相关文章
- poj3613:Cow Relays(倍增优化+矩阵乘法floyd+快速幂)
Cow Relays Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7825 Accepted: 3068 Descri ...
- LG5196 「USACO2019JAN」Cow Poetry 背包+乘法原理
\(\mathrm{Cow Poetry}\) 问题描述 LG5196 题解 因为每句诗的长度一定是\(k\),所以自然而然想到背包. 设\(opt[i][j]\)代表到第\(i\)位时,结尾为\(j ...
- 【POJ3613】Cow Relays 离散化+倍增+矩阵乘法
题目大意:给定一个 N 个顶点,M 条边的无向图,求从起点到终点恰好经过 K 个点的最短路. 题解:设 \(d[1][i][j]\) 表示恰好经过一条边 i,j 两点的最短路,那么有 \(d[r+m] ...
- POJ3613 Cow Relays [矩阵乘法 floyd类似]
Cow Relays Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7335 Accepted: 2878 Descri ...
- poj3613 Cow Relays【好题】【最短路】【快速幂】
Cow Relays Time Limit: 1000MS Memory Limit: 65536K Total Submissions:9207 Accepted: 3604 Descrip ...
- 「译」JUnit 5 系列:条件测试
原文地址:http://blog.codefx.org/libraries/junit-5-conditions/ 原文日期:08, May, 2016 译文首发:Linesh 的博客:「译」JUni ...
- 「译」JUnit 5 系列:扩展模型(Extension Model)
原文地址:http://blog.codefx.org/design/architecture/junit-5-extension-model/ 原文日期:11, Apr, 2016 译文首发:Lin ...
- JavaScript OOP 之「创建对象」
工厂模式 工厂模式是软件工程领域一种广为人知的设计模式,这种模式抽象了创建具体对象的过程.工厂模式虽然解决了创建多个相似对象的问题,但却没有解决对象识别的问题. function createPers ...
- 「C++」理解智能指针
维基百科上面对于「智能指针」是这样描述的: 智能指针(英语:Smart pointer)是一种抽象的数据类型.在程序设计中,它通常是经由类型模板(class template)来实做,借由模板(tem ...
随机推荐
- iframe内外的操作
因为iframe涉及到跨域问题,有时候有的比较多,这不今天遇到了一个问题,处在iframe里头的js要操作iframe元素,查找百度,是可以实现的: 用jQuery在IFRAME里取得父窗口的某个元素 ...
- JAVA基础学习(2)之判断
2判断 2.1比较 2.1.1比较 System.out.println(amount>=10);输出的值为true或false 2.1.2关系运算 优先级 <算术运算 >赋值运算 ...
- Echarts字体和线条颜色设置操作笔记
在网上偶然看到的一篇文章 感觉不错 下面是原著地址 原著地址:https://blog.csdn.net/eastmount/article/details/52823548
- 201771010135 杨蓉庆《面对对象程序设计(java)》第十六周学习总结
1.实验目的与要求 (1) 掌握线程概念: (2) 掌握线程创建的两种技术: (3) 理解和掌握线程的优先级属性及调度方法: (4) 掌握线程同步的概念及实现技术: 一.理论知识 ⚫ 线程的概念 (1 ...
- vs2019 opencv4的相关配置
opencv4.11存在改动,导致许多demo没有办法正常运行,但是配置方法却是相同的. 主要是连接器输入,头文件包含路径,库路.如果想要调试,还需要设置调试文件符号表. [未完待续]
- java编译器不匹配问题(java compiler level does not match the version of the installed java project facet)
问题:项目经常copy过来,经常会报说Java编译器不匹配 解决方法:找到项目所在的目录,在.settings子目录里面,用文本编辑器打开org.eclipse.wst.common.project. ...
- php 用mysqli的json_encode()转换出来 全是对象的问题
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/9/6 0006 * Time: 15:33 */ //查 ...
- http的长连接和短连接(史上最通俗!)
1.以前的误解 很久之前就听说过长连接的说法,而且还知道HTTP1.0协议不支持长连接,从HTTP1.1协议以后,连接默认都是长连接.但终究觉得对于长连接一直懵懵懂懂的,有种抓不到关键点的感觉. 今天 ...
- LeetCode练题——53. Maximum Subarray
1.题目 53. Maximum Subarray——Easy Given an integer array nums, find the contiguous subarray (containin ...
- 使用 swiper 制作 导航,点击项居中
目的:导航有多个,被点击的项居中显示. html: <div class="swiper-container"> <div class="swiper- ...