题目

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. 一文了解Docker容器技术的操作

    一文了解Docker容器技术的操作 前言一.Docker是什么二.Docker的安装及测试Docker的安装Docker的Hello world测试三.Docker的常见操作镜像的基本操作容器的基本操 ...

  2. tensorflow2.0学习笔记第二章第二节

    2.2复杂度和学习率 指数衰减学习率可以先用较大的学习率,快速得到较优解,然后逐步减少学习率,使得模型在训练后期稳定指数衰减学习率 = 初始学习率 * 学习率衰减率^(当前轮数/多少轮衰减一次) 空间 ...

  3. 温故知新-java多线程&深入理解线程池

    文章目录 摘要 java中的线程 java中的线程池 线程池技术 线程池的实现原理 简述 ThreadPoolExecutor是如何运行的? 线程池运行的状态和线程数量 任务执行机制 队列缓存 Wor ...

  4. iOS -App主流框架UINavigationController && UITabBarController的简单使用

     一个iOS app几乎没有由一个控制器组成,除非这个app非常简单.       当app中有多个控制器的时候,就需要对这些控制器进行管理,用1个控制器去管理其他多个控制器:       如图所示: ...

  5. arduino 的analogRead() 和analogWrite()

    模拟输入analogRead()函数的返回值范围是0 到1023; 而模拟输出analogWrite()函数的输出值范围是0 到255; 所以: val = analogRead(potpin); / ...

  6. Prometheus监控Docker Swarm集群(一)

    Prometheus监控Docker Swarm集群(一) cAdvisor简介 为了解决容器的监控问题,Google开发了一款容器监控工具cAdvisor(Container Advisor),它为 ...

  7. abp-CMS模块-广告

    无论是开发app还是网站,可能都需要一个广告功能,比如我们常见的在首页有个轮播广告,里面会轮播显示多个图片.还有比如一个新闻门户网站 很常见的 banner横幅广告,还有js特效广告等.本篇说说在ab ...

  8. Dorado开发——树形下拉框

    最近在学习Dorado开发的过程中,遇到了一个问题,Dorado的树形下拉框选择:Dorado默认情况下父节点和子节点都是可选的,而我要实现的是父节点不可选. 解决办法:在下拉框中,判断父子节点,点击 ...

  9. 厉害了!除了find命令,还有这么多文件查找命令,高手必备!

    大家好,我是良许. 在系统里查找文件,是所有工程师都必备的技能(不管你用的是 Windows .Linux.还是 MacOS 系统).对于 Linux 操作系统,单单一个 find 命令就可以完成非常 ...

  10. 【JMeter_03】JMeter GUI操作界面介绍

    JMeter主界面主要分为 标题栏.菜单栏.工具栏.测试计划树形目录.内容展示区 标题栏:主要展示JMeter的程序版本.当前脚本的名称.脚本的储存路径 菜单栏:程序基本上所有功能的所属分类目录,基本 ...