题目

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. 一篇文章快速入门React框架

    视频教程 本文章在B站配有视频教程 课程目标 了解最常用的React概念和相关术语,例如JSX,组件,属性(Props),状态(state). 构建一个非常简单的React应用程序,以阐述上述概念. ...

  2. keras搭建神经网络快速入门笔记

    之前学习了tensorflow2.0的小伙伴可能会遇到一些问题,就是在读论文中的代码和一些实战项目往往使用keras+tensorflow1.0搭建, 所以本次和大家一起分享keras如何搭建神经网络 ...

  3. MySql多表查询优化

    一.多表查询连接的选择 相信内连接,左连接什么的大家都比较熟悉了,当然还有左外连接什么的,基本用不上,我就补贴出来了,这个图只是让大家熟悉一下各种连接查询.然后要告诉大家的是,需要根据查询的信息,想好 ...

  4. zabbix 邮箱告警

    脚本内容 #!/bin/env python #coding:utf- import smtplib from email.mime.text import MIMEText from sys imp ...

  5. Android开源框架选择

    Android开源项目推荐之「网络请求哪家强」 Android开源项目推荐之「图片加载到底哪家强」 Android网络框架的封装 Android Volley+OkHttp3+Gson(Jackson ...

  6. 构造函数继承与class继承

    构造函数继承 1.子类通过apply方法或者call方法把this指向父类 js代码 function Parent(name, age) { this.name = name this.age = ...

  7. Jackson乱码问题

    在配置文件中加入下面的内容 <!-- Json乱码问题配置--> <mvc:annotation-driven> <mvc:message-converters regi ...

  8. mtail 调试

    mtail 调式 mtail 不会采集当前accesslog 内容以前的内容,只有当你启动mtail后,去访问你的监控tomcat,有新的access 日志刷入localhost_access_log ...

  9. Keiichi Tsuchiya the Drift King (c++三角函数公式)【几何+三角函数公式】

    Keiichi Tsuchiya the Drift King 感谢:  https://blog.csdn.net/xiao_you_you/article/details/89357815 题目链 ...

  10. Excel经典教程之一

    照片名称:未命名 照片名称:自动筛选 照片名称:在Excel中字符替换 照片名称:在Excel中直接编辑“宏” 照片名称:在Excel中为导入外部数据 照片名称:在Excel中行列快速转换 照片名称: ...