USACO07NOV Cow Relays G 题解
题目
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 题解的更多相关文章
- 2021.11.03 P2886 [USACO07NOV]Cow Relays G(矩阵+floyed)
2021.11.03 P2886 [USACO07NOV]Cow Relays G(矩阵+floyed) [P2886 USACO07NOV]Cow Relays G - 洛谷 | 计算机科学教育新生 ...
- 【图论】USACO07NOV Cow Relays G
题目大意 洛谷链接 给定一张\(T\)条边的无向连通图,求从\(S\)到\(E\)经过\(N\)条边的最短路长度. 输入格式 第一行四个正整数\(N,T,S,E\),意义如题面所示. 接下来\(T\) ...
- [USACO07NOV]Cow Relays G
题目大意 给出一张无向连通图(点数小于1000),求S到E经过k条边的最短路. 算法 这是之前国庆模拟赛的题 因为懒 所以就只挑一些题写博客 在考场上写了个dp 然后水到了50分 出考场和神仙们一问才 ...
- 洛谷P2886 [USACO07NOV]Cow Relays G (矩阵乘法与路径问题)
本题就是求两点间只经过n条边的最短路径,定义广义的矩阵乘法,就是把普通的矩阵乘法从求和改成了取最小值,把内部相乘改成了相加. 代码包含三个内容:广义矩阵乘法,矩阵快速幂,离散化: 1 #include ...
- [USACO07NOV]Cow Relays
map+floyed+矩阵乘法(倍增floyed) # include <stdio.h> # include <stdlib.h> # include <iostrea ...
- P2340 [USACO03FALL]Cow Exhibition G题解
新的奇巧淫技 原题传送门 众所周知,模拟退火是一种很强大的算法,DP很强,但我模拟退火也不虚,很多题你如果不会的话基本可以拿来水很多分.比如这道题,我用模拟退火可以轻松水过(虽然我是足足交了两页才过) ...
- 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 ...
- poj 3613 Cow Relays
Cow Relays Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5411 Accepted: 2153 Descri ...
随机推荐
- Linux 源码包服务的管理
源码包安装服务的启动 使用绝对路径,调用启动脚本来启动.不同源码包的启动脚本不同,可以查看源码包的安装说明,查看启动脚本的方法 /usr/local/apache2/bin/apachectl sta ...
- 【python】【开源】使用Tkinter和matplotlib实时显示图像,打造属于自己的性能测试小工具
在腾讯的perfdog工具还未公开时,当时需要查看内存使用情况等信息,就用python写了个小工具 为了提升开发效率,就直接借用了雷子开源的性能测试工具的布局,自己美化了一下,然后加入了实时显示数据的 ...
- webpack从什么都不懂到入门
前言 这篇文章是自己在整理webpack相关的东西时候突发奇想,想总结自己所学知识,也希望能够帮助想学习webpack的同学们,都是入门级别的,大佬请出门右转. 本文的webpack基于webpack ...
- 【Spring注解驱动开发】组件注册-@ComponentScan-自动扫描组件&指定扫描规则
写在前面 在实际项目中,我们更多的是使用Spring的包扫描功能对项目中的包进行扫描,凡是在指定的包或子包中的类上标注了@Repository.@Service.@Controller.@Compon ...
- 调优 | Apache Hudi应用调优指南
通过Spark作业将数据写入Hudi时,Spark应用的调优技巧也适用于此.如果要提高性能或可靠性,请牢记以下几点. 输入并行性:Hudi对输入进行分区默认并发度为1500,以确保每个Spark分区都 ...
- conda 切换为国内源
添加清华源 conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/ conda con ...
- mybatis实现多表一对一,一对多,多对多关联查询
原文:https://blog.csdn.net/m0_37787069/article/details/79247321 1.一对一关键字:association作用:针对pojo对象属性的映射 ...
- 深度解密 Go 语言之 sync.map
工作中,经常会碰到并发读写 map 而造成 panic 的情况,为什么在并发读写的时候,会 panic 呢?因为在并发读写的情况下,map 里的数据会被写乱,之后就是 Garbage in, garb ...
- 如何在VMware虚拟机中安装CentOS6.7系统(下篇)
上一篇文章讲到了CentOS6.7的安装教程,安装步骤到时区选择这块了,这篇文章接上篇文章,继续讲述CentOS6.7的安装教程,直至安装完成. 17.设置root的登录密码,日后登录虚拟机,用户名就 ...
- 图像处理中的valid卷积与same卷积
valid卷积 在full卷积的卷积过程中,会遇到\(K_{flip}\)靠近I的边界(K矩阵与I矩阵),就会有部分延申到I之外,这时候忽略边界,只考虑I完全覆盖\(K_{flip}\)内的值情况,这 ...