Cow Relays

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7825   Accepted: 3068

Description

For their physical fitness program, N (2 ≤ N ≤ 1,000,000) cows have decided to run a relay race using the T (2 ≤ T ≤ 100) cow trails throughout the pasture.

Each trail connects two different intersections (1 ≤ I1i ≤ 1,000; 1 ≤ I2i ≤ 1,000), each of which is the termination for at least two trails. The cows know the lengthi of each trail (1 ≤ lengthi  ≤ 1,000), the two intersections the trail connects, and they know that no two intersections are directly connected by two different trails. The trails form a structure known mathematically as a graph.

To run the relay, the N cows position themselves at various intersections (some intersections might have more than one cow). They must position themselves properly so that they can hand off the baton cow-by-cow and end up at the proper finishing place.

Write a program to help position the cows. Find the shortest path that connects the starting intersection (S) and the ending intersection (E) and traverses exactly N cow trails.

Input

* Line 1: Four space-separated integers: NTS, and E
* Lines 2..T+1: Line i+1 describes trail i with three space-separated integers: lengthi , I1i , and I2i

Output

* Line 1: A single integer that is the shortest distance from intersection S to intersection E that traverses exactly N cow trails.

Sample Input

2 6 6 4
11 4 6
4 4 8
8 4 9
6 6 8
2 6 9
3 8 9

Sample Output

10

题意

给出一张图,求k边最短路,即经过k条边的最短路。

分析

思考一下:如果一个矩阵,表示走k条边后,一张图的点与点的最短路径,(a,b)表示从a到b的最短路径,然后我们把它与自己,按照矩阵乘法的格式“相乘”,把其中的乘改为取min,c.a[i][j] = min(c.a[i][j],x.a[i][k]+y.a[k][j]);看不懂先看下面。

这样得到的是走k+k条边的矩阵。有点抽象,下面详细解释下:

c中的一个点(a,b),当我们用x矩阵和y矩阵求它时,我们枚举了x矩阵的a行所有数,与y矩阵的b列所有数,并且他们的坐标只能是相对应的,比如x矩阵的(a,2)这个点,相应的y矩阵点就是(2,b),那么放到图上去理解,即从a点经过2点到b点的距离,类似的点不只有2,把所有点枚举完后,c.a[a][b]就是从a到b的最短距离。(意会一下)

这样下来,会得到走k+k条边的最短路径,对于其他的矩阵这样操作,得到的是他们两个,经过的边数相加的结果。(一个经过a条边后的矩阵 与 一个经过b条边后的矩阵这样操作后,是经过a+b条边后的矩阵,矩阵中存的是最短路径)。解释一下:向上面的例子一样,(a,2)(2,b),是即从a点经过2点到b点的距离,因为x矩阵和y矩阵都是走k条边后的最短路径,那么x矩阵中的(a,2)是走k步后的最短路径,(2,b)也是,那么他们相加不就是走k+k条边后的最短路径吗?其他的矩阵一样。

然后,就可以套用快速幂的模板了,只不过将以前的乘改成加了,也就是倍增的思想的,比如对于走10条边,它的二进制是1010,那么我们就让在走2(10)边时的矩阵 乘以 8(1000)边的矩阵,得到走10条边的矩阵即开始时由1->2->4->8->16……即倍增中的2次幂。

code

 #include<cstdio>
#include<algorithm>
#include<map>
#include<cstring> using namespace std;
const int MAXN = ; int Hash[];
int n,k,q,z,tn; int read()
{
int x = ,f = ;char ch = getchar();
while (ch<''||ch>'') {if (ch='-') f=-;ch = getchar(); }
while (ch>=''&&ch<=''){x = x*+ch-'';ch = getchar(); }
return x*f;
} struct Matrix{
int a[MAXN][MAXN];
Matrix operator * (const Matrix &x) const
{
Matrix c;
memset(c.a,0x3f,sizeof(c.a));
for (int k=; k<=tn; k++)
for (int i=; i<=tn; ++i)
for (int j=; j<=tn; ++j)
c.a[i][j] = min(c.a[i][j],a[i][k]+x.a[k][j]);
return c;
}
}s,ans;
void ksm()
{
ans = s;
k--;
for (; k; k>>=)
{
if (k&) ans = ans*s;
s = s*s;
}
}
int main()
{
k = read();n = read();q = read();z = read();
memset(s.a,0x3f,sizeof(s.a));
for (int x,y,w,i=; i<=n; ++i)
{
w = read();x = read();y = read();
if (!Hash[x]) Hash[x] = ++tn;
if (!Hash[y]) Hash[y] = ++tn;
s.a[Hash[x]][Hash[y]] = s.a[Hash[y]][Hash[x]] = w;
}
ksm();
printf("%d",ans.a[Hash[q]][Hash[z]]);
return ;
}

poj3613:Cow Relays(倍增优化+矩阵乘法floyd+快速幂)的更多相关文章

  1. Luogu T7152 细胞(递推,矩阵乘法,快速幂)

    Luogu T7152 细胞(递推,矩阵乘法,快速幂) Description 小 X 在上完生物课后对细胞的分裂产生了浓厚的兴趣.于是他决定做实验并 观察细胞分裂的规律. 他选取了一种特别的细胞,每 ...

  2. Luogu 3390 【模板】矩阵快速幂 (矩阵乘法,快速幂)

    Luogu 3390 [模板]矩阵快速幂 (矩阵乘法,快速幂) Description 给定n*n的矩阵A,求A^k Input 第一行,n,k 第2至n+1行,每行n个数,第i+1行第j个数表示矩阵 ...

  3. POJ 3613 [ Cow Relays ] DP,矩阵乘法

    解题思路 首先考虑最暴力的做法.对于每一步,我们都可以枚举每一条边,然后更新每两点之间经过\(k\)条边的最短路径.但是这样复杂度无法接受,我们考虑优化. 由于点数较少(其实最多只有\(200\)个点 ...

  4. 洛谷P2886 [USACO07NOV]Cow Relays G (矩阵乘法与路径问题)

    本题就是求两点间只经过n条边的最短路径,定义广义的矩阵乘法,就是把普通的矩阵乘法从求和改成了取最小值,把内部相乘改成了相加. 代码包含三个内容:广义矩阵乘法,矩阵快速幂,离散化: 1 #include ...

  5. bzoj 3240: [Noi2013]矩阵游戏 矩阵乘法+十进制快速幂+常数优化

    3240: [Noi2013]矩阵游戏 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 613  Solved: 256[Submit][Status] ...

  6. HDU4965 Fast Matrix Calculation —— 矩阵乘法、快速幂

    题目链接:https://vjudge.net/problem/HDU-4965 Fast Matrix Calculation Time Limit: 2000/1000 MS (Java/Othe ...

  7. bzoj 3240 矩阵乘法+十进制快速幂

    首先,构造出从f[][i]->f[][i+1]的转移矩阵a,和从f[i][m]->f[i+1][1]的转移矩阵b, 那么从f[1][1]转移到f[n][m]就是init*(a^(m-1)* ...

  8. 【HDOJ5950】Recursive sequence(矩阵乘法,快速幂)

    题意:f[1]=a,f[2]=b,f[i]=2f[i-2]+f[i-1]+i^4(i>=3),多组询问求f[n]对2147493647取模 N,a,b < 2^31 思路:重点在于i^4的 ...

  9. 2021.11.03 P2886 [USACO07NOV]Cow Relays G(矩阵+floyed)

    2021.11.03 P2886 [USACO07NOV]Cow Relays G(矩阵+floyed) [P2886 USACO07NOV]Cow Relays G - 洛谷 | 计算机科学教育新生 ...

随机推荐

  1. spring运用的设计模式

    1.代理模式(典型的aop) 2.工厂模式(beanFactory) 3.观察者模式(ApplicationContextEvent && ApplicationContextList ...

  2. CSS单词换行and断词,你真的完全了解吗

    背景 某天老板在群里反馈,英文单词为什么被截断了? 很显然,这是我们前端的锅,自行背锅.这个问题太简单了,css里加两行属性,分分钟搞定. 开心的提交代码,刷新页面.我擦,怎么还是没有断词?不可能啊! ...

  3. Retrofit2与RxJava用法大全

    Retrofit2是square公司出品的一个网络请求库,网上有很多相关的介绍.我很久以前都想去研究了,但一直都有各种事情耽搁,现在就让我们一起去捋一捋,这篇主要讲解Retrofit2与RxJava的 ...

  4. Cocos2d-x v3.1 安装图文教程(二)

       Cocos2d-x v3.1 安装图文教程(二) 如果我们需要在Android平台上运行就必须安装android的SDK,如果我们只想在window上运行就只需要安装Cocos2d-x就行了.当 ...

  5. javascript实现 滚动条滚动 加载内容

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. 300行ABAP代码实现一个最简单的区块链原型

    不知从什么时候起,区块链在网上一下子就火了. 这里Jerry就不班门弄斧了,网上有太多的区块链介绍文章.我的这篇文章没有任何高大上的术语,就是300行ABAP代码,实现一个最简单的区块链原型. 我个人 ...

  7. IOS 强指针(strong)和弱指针(weak)

    // strong 强指针        // weak 弱指针        // ARC, 只要对象没有强指针就会自动释放        // OC中默认都是强指针

  8. 集成Ehcache

    提醒 这一小节的是如何在应用层(service或者module或action类)中使用ehcache   准备工作 下载ehcache 你需要一个js文件   请务必阅读下面代码中的注释!! 分情况选 ...

  9. 2dsphere索引

    概念:球面地理位置索引 创建方式: db.collection.ensureIndex({w:'2dsphere'}) wdspere中,位置的表示方式不再是简单的经度,纬度,数组,而是变成一种复杂的 ...

  10. RxJava2 方法总结

    RxJava2 方法总结 看了许多讲解RxJava的文章,有些文章讲解的内容是基于第一个版本的,有些文章的讲解是通过比较常用的一些API和基础的概念进行讲解的. 但是每次看到RxJava的类中的几十个 ...