POJ 3613 Cow Relays floyd+矩阵
来源:互联网 发布:java构造方法重载要素 编辑:程序博客网 时间:2024/06/03 16:29
题目:
http://poj.org/problem?id=3613
题意:
一个无向图上有n个点,每条边都有权值,问从点s到点e的恰好经过t条边的最短路径是多少
思路:
利用矩阵快速幂的思想,再套用floyd算法,实际上就是做了t次floyd,利用快速幂加速。其中只有ans结构体对角线置0而其他结构对角线不置0的原因是考虑s和e相同时的情况。
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int N = 1000 + 10, INF = 0x3f3f3f3f;int num, has[N];struct matrix{ int row, col; int mat[110][110]; matrix(): row(100), col(100) {} void init() { memset(mat, 0x3f, sizeof mat); }};matrix ans, mp, ta, tb;void floyd(matrix &a, matrix &b, matrix &c, int f){ for(int k = 1; k <= num; k++) for(int i = 1; i <= num; i++) for(int j = 1; j <= num; j++) a.mat[i][j] = min(a.mat[i][j], b.mat[i][k] + c.mat[k][j]);}void work(int n){ ans.init(), ta.init(), tb.init(); for(int i = 1; i <= num; i++) ans.mat[i][i] = 0; while(n) { if(n & 1) { floyd(tb, ans, mp, 1); ans = tb; tb.init(); } floyd(ta, mp, mp, 0); mp = ta; ta.init(); n >>= 1; }}int main(){ int n, t, s, e; scanf("%d%d%d%d", &n, &t, &s, &e); num = 0; memset(has, 0, sizeof has); mp.init(); int a, b, c; for(int i = 1; i <= t; i++) { scanf("%d%d%d", &c, &a, &b); if(! has[a]) has[a] = ++num; if(! has[b]) has[b] = ++num; mp.mat[has[a]][has[b]] = mp.mat[has[b]][has[a]] = min(mp.mat[has[a]][has[b]], c); } work(n); printf("%d\n", ans.mat[has[s]][has[e]]); return 0;}
阅读全文
0 0
- POJ 3613 Cow Relays floyd+矩阵
- POJ 3613 Cow Relays (floyd + 矩阵快速幂)
- POJ 3613 Cow Relays(矩阵模板+快速幂+floyd)
- POJ-3613-Cow Relays-矩阵
- POJ--3613[Cow Relays] floyd 倍增法
- POJ 3613 Cow Relays floyd + 快速幂
- POJ--3613[Cow Relays] floyd 倍增法
- POJ 3613 Cow Relays Floyd最短路
- POJ 3613 Cow Relays floyd+快速幂
- poj 3613 Cow Relays (floyd快速幂)
- POJ 3613 Cow Relays 倍增Floyd
- Poj 3613 Cow Relays【Floyd+快速幂】
- POJ 3613 Cow Relays [矩阵连乘]
- poj 3613 Cow Relays (K步最短路+Floyd+矩阵快速幂)
- poj 3613 Cow Relays(经过N条边的最短路,floyd + 二分矩阵)
- poj 3613 Cow Relays (floyd+矩阵快速幂+坐标离散化)
- POJ 3613 Cow Relays (Floyd + 矩阵快速幂 + 离散化 神题!)
- POJ 3613 Cow Relays k步最短路 (floyd + 矩阵快速幂)
- vim将多行代码前后移动一个tab的方法。
- 一个基于FPGA的虚拟参观系统
- ROS RViz基本学习笔记(二)
- C++中直接class 类名和#include的区别
- 剑指offer 面试题2 实现单例模式
- POJ 3613 Cow Relays floyd+矩阵
- DFS和BFS
- 宠物商店编程问题
- oracle 创建表空间详细介绍
- fine-tuning的二三事
- GDBT
- 03Collection集合与Map集合的基础知识
- 创建感知机 python
- 欢迎使用CSDN-markdown编辑器