题目

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 ≤ I1_i ≤ 1,000; 1 ≤ I2_i ≤ 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.

给出一张无向连通图,求S到E经过k条边的最短路。

输入格式

  • Line \(1\): Four space-separated integers: N, T, S, and E

  • 一行四个正整数 \(N,T,S,E\) ,意义如题面所示。

  • Lines \(2..T+1\): Line \(i+1\) describes trail i with three space-separated integers: \(length_i\) , \(I1_i\) , and \(I2_i\)

  • 接下来 \(T\) 行每行三个正整数 \(w,u,v\),分别表示路径的长度,起点和终点。

输出格式

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

  • 一行一个整数表示图中从 \(S\) 到 \(E\) 经过 $N 条边的最短路长度。

输入样例

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

输出样例

10

题解

矩阵我不熟,看了大佬的一个式子:

把经过\(x\)个点的最短路的邻接矩阵\(X\)和经过\(y\)个点的最短路的邻接矩阵\(Y\)合并的式子为:

\(A_{i,j}=min(A_{i,j},X_{i,k}+Y_{k,j})\)

把输入转成邻接矩阵后,这个邻接矩阵可以看作恰好经过一个点的最短路,然后转移\(n-1\)次就可以了

矩阵相乘时,需要使用快速幂优化

代码


#include <cstdio>
#include <cstring>
#define min(a, b) (a < b ? a : b)
int num[1000005], n, s, t, e, tol, x, y, z;
struct map {
int data[500][500];
map operator*(const map &other) const {
map c;
for (int k = 1; k <= tol; k++)
for (int i = 1; i <= tol; i++)
for (int j = 1; j <= tol; j++)
c.data[i][j] =
min(c.data[i][j], data[i][k] + other.data[k][j]);
return c;
}
map() { memset(data, 0x3f3f3f3f, sizeof(data)); }
} dis, ans;
inline int input() { int t; scanf("%d", &t); return t; }
int main() {
n = input() - 1, t = input(), s = input(), e = input();
for (int i = 1; i <= t; i++) {
x=input();
if(!num[y=input()])num[y] = ++tol;
if(!num[z=input()])num[z] = ++tol;
dis.data[num[y]][num[z]] = dis.data[num[z]][num[y]] = x;
}
ans = dis;
while (n) (n & 1) && (ans = ans * dis, 0), dis = dis * dis, n >>= 1;
printf("%d",ans.data[num[s]][num[e]]);
}

USACO07NOV Cow Relays G 题解的更多相关文章

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

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

  2. 【图论】USACO07NOV Cow Relays G

    题目大意 洛谷链接 给定一张\(T\)条边的无向连通图,求从\(S\)到\(E\)经过\(N\)条边的最短路长度. 输入格式 第一行四个正整数\(N,T,S,E\),意义如题面所示. 接下来\(T\) ...

  3. [USACO07NOV]Cow Relays G

    题目大意 给出一张无向连通图(点数小于1000),求S到E经过k条边的最短路. 算法 这是之前国庆模拟赛的题 因为懒 所以就只挑一些题写博客 在考场上写了个dp 然后水到了50分 出考场和神仙们一问才 ...

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

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

  5. [USACO07NOV]Cow Relays

    map+floyed+矩阵乘法(倍增floyed) # include <stdio.h> # include <stdlib.h> # include <iostrea ...

  6. P2340 [USACO03FALL]Cow Exhibition G题解

    新的奇巧淫技 原题传送门 众所周知,模拟退火是一种很强大的算法,DP很强,但我模拟退火也不虚,很多题你如果不会的话基本可以拿来水很多分.比如这道题,我用模拟退火可以轻松水过(虽然我是足足交了两页才过) ...

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

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

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

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

  9. poj 3613 Cow Relays

    Cow Relays Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5411   Accepted: 2153 Descri ...

随机推荐

  1. Linux用户管理命令useradd、passwd、who详解

    创建用户命令useradd 命令useradd,所在路径为: 可以看到命令useradd的路径为:/usr/sbin/useradd,因此它的执行权限是root 命令的功能是创建一个新用户,例如:us ...

  2. Python 图像处理 OpenCV (7):图像平滑(滤波)处理

    前文传送门: 「Python 图像处理 OpenCV (1):入门」 「Python 图像处理 OpenCV (2):像素处理与 Numpy 操作以及 Matplotlib 显示图像」 「Python ...

  3. 【1】svn 指令总结

    [1]svn log 1.svn log 2. [2]svn di [3]

  4. kworkerds挖矿木马

    昨天一朋友的公司服务器中了挖矿病毒,一起帮忙查看并做下记录.   病毒信息 名称:kworkerds 目录:/tmp/ 关键点:文件 -i 属性   i :这个i可就很厉害了.它可以让一个文件“不能被 ...

  5. SuperSlide之属性targetCell介绍

    禁用页面菜单js代码: <script type="text/javascript"> document.oncontextmenu=function(){ retur ...

  6. Second Large Rectangle【单调栈】

    Second Large Rectangle 题目链接(点击) 题目描述 Given a N×MN \times MN×M binary matrix. Please output the size ...

  7. Java_图片转字符

    把高达头像转换成字符[-V-] 调节双循环里y与x的增量改变字符输出的细节.高和长 public class ImgToStr { public static void main(String arg ...

  8. python反向遍历一个可迭代对象

    我们通常情况下都是正向遍历一个列表,下面是一种简单的反向遍历一个列表的方式. ## 正向遍历 >>>A = [9, 8, 7] >>>for index, a in ...

  9. Android学习笔记样式资源文件

    样式资源和主题资源都是写在styles.xml文件里面的 <style name="title"> <item name="android:textSi ...

  10. laravel查询常用的方式含义.

    find($id) 传值并返回一个模型.如果不存在匹配的模型,则返回null.findOrFail($id) 传值并返回一个模型.如果不存在匹配的模型, 它会抛出异常.first() 返回在数据库中找 ...