题目链接

题目

题目描述

Farmer John is on a boat seeking fabled treasure on one of the N (1 <= N <= 100) islands conveniently labeled 1..N in the Cowribbean Sea.

The treasure map tells him that he must travel through a certain sequence A1, A2, ..., AM of M (2 <= M <= 10,000) islands, starting on island 1 and ending on island N before the treasure will appear to him. He can visit these and other islands out of order and even more than once, but his trip must include the Ai sequence in the order specified by the map.

FJ wants to avoid pirates and knows the pirate-danger rating (0 <= danger <= 100,000) between each pair of islands. The total danger rating of his mission is the sum of the danger ratings of all the paths he traverses.

Help Farmer John find the least dangerous route to the treasure that satisfies the treasure map's requirement.

输入描述

  • Line 1: Two space-separated integers: N and M
  • Lines 2..M+1: Line i+1 describes the ith island FJ must visit with a single integer: Ai
  • Lines M+2..N+M+1: Line i+M+1 contains N space-separated integers that are the respective danger rating of the path between island i and islands 1, 2, ..., and N, respectively. The ith integer is always zero.

输出描述

  • Line 1: The minimum danger that Farmer John can encounter while obtaining the treasure.

示例1

输入

3 4
1
2
1
3
0 5 1
5 0 2
1 2 0

输出

7

说明

There are 3 islands and the treasure map requires Farmer John to visit a sequence of 4 islands in order: island 1, island 2, island 1 again, and finally island 3. The danger ratings of the paths are given: the paths (1, 2); (2, 3); (3, 1) and the reverse paths have danger ratings of 5, 2, and 1, respectively.

He can get the treasure with a total danger of 7 by traveling in the sequence of islands 1, 3, 2, 3, 1, and 3. The cow map's requirement (1, 2, 1, and 3) is satisfied by this route. We avoid the path between islands 1 and 2 because it has a large danger rating.

题解

知识点:最短路。

因为要经过给定的地点,并且两地点之间应该走最短路,所以考虑floyd把所有地点之间的最短路求出来。

时间复杂度 \(O(n^3+m)\)

空间复杂度 \(O(n^3+m)\)

代码

#include <bits/stdc++.h>
#define ll long long using namespace std; int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n, m;
cin >> n >> m; vector<int> q(m + 1);
for (int i = 1;i <= m;i++) cin >> q[i]; vector<vector<int>> g(n + 1, vector<int>(n + 1));
for (int i = 1;i <= n;i++)
for (int j = 1;j <= n;j++)
cin >> g[i][j]; for (int k = 1;k <= n;k++)
for (int i = 1;i <= n;i++)
for (int j = 1;j <= n;j++)
g[i][j] = min(g[i][j], g[i][k] + g[k][j]); int ans = 0;
for (int i = 2;i <= m;i++) ans += g[q[i - 1]][q[i]];
cout << ans << '\n';
return 0;
}

NC25005 [USACO 2008 Ope S]Clear And Present Danger的更多相关文章

  1. Bzoj 1624: [Usaco2008 Open] Clear And Present Danger 寻宝之路 最短路,floyd

    1624: [Usaco2008 Open] Clear And Present Danger 寻宝之路 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 5 ...

  2. BZOJ 1624: [Usaco2008 Open] Clear And Present Danger 寻宝之路( 最短路 )

    直接floyd.. ----------------------------------------------------------------------- #include<cstdio ...

  3. 1624: [Usaco2008 Open] Clear And Present Danger 寻宝之路

    1624: [Usaco2008 Open] Clear And Present Danger 寻宝之路 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 3 ...

  4. 洛谷——P2910 [USACO08OPEN]寻宝之路Clear And Present Danger

    P2910 [USACO08OPEN]寻宝之路Clear And Present Danger 题目描述 Farmer John is on a boat seeking fabled treasur ...

  5. BZOJ 1624: [Usaco2008 Open] Clear And Present Danger 寻宝之路

    Description 农夫约翰正驾驶一条小艇在牛勒比海上航行. 海上有N(1≤N≤100)个岛屿,用1到N编号.约翰从1号小岛出发,最后到达N号小岛.一 张藏宝图上说,如果他的路程上经过的小岛依次出 ...

  6. bzoj1624 [Usaco2008 Open] Clear And Present Danger 寻宝之路

    Description     农夫约翰正驾驶一条小艇在牛勒比海上航行.     海上有N(1≤N≤100)个岛屿,用1到N编号.约翰从1号小岛出发,最后到达N号小岛.一 张藏宝图上说,如果他的路程上 ...

  7. [Usaco2008 Open] Clear And Present Danger 寻宝之路[最短路][水]

    Description     农夫约翰正驾驶一条小艇在牛勒比海上航行.     海上有N(1≤N≤100)个岛屿,用1到N编号.约翰从1号小岛出发,最后到达N号小岛.一 张藏宝图上说,如果他的路程上 ...

  8. Luogu [USACO08OPEN]寻宝之路Clear And Present Danger

    题目描述 Farmer John is on a boat seeking fabled treasure on one of the N (1 <= N <= 100) islands ...

  9. (最短路 Floyd) P2910 [USACO08OPEN]寻宝之路Clear And Present Danger 洛谷

    题意翻译 题目描述 农夫约翰正驾驶一条小艇在牛勒比海上航行. 海上有N(1≤N≤100)个岛屿,用1到N编号.约翰从1号小岛出发,最后到达N号小岛. 一张藏宝图上说,如果他的路程上经过的小岛依次出现了 ...

  10. [USACO08OPEN]寻宝之路Clear And Present Danger

    OJ题号:洛谷2910 思路:Floyd #include<cstdio> #include<algorithm> using namespace std; int main( ...

随机推荐

  1. WPF Prism框架Region失效了?

    站长最近转载了痕迹的Prism 8系列博文,不知道你对该框架是否有一定了解了?不了解,可以看看本号转载的系列: WPF Prism框架合集(2.创建Prism应用程序) WPF Prism框架合集(3 ...

  2. PHPCMS V9安装出现DNS解析失败的解决方法-不支持采集和保存远程图片

    目前因为phpcms官网停止解析后,很多人安装phpcms v9出现如下错误:     这是因为检测dns解析的域名是phpcms官网的域名,官网域名停止解析后肯定检测失败.解决方法如下:   打开/ ...

  3. Spring——AOP练习

    模仿前面的例子,完成模拟JDBC操作 1.UserDAO接口,具有insert(String name)方法,UserDAOImpl实现它 2. 用前置增强,在插入之前,完成数据库连接.事务创建工作 ...

  4. [转帖]一文看懂Linux内核页缓存(Page Cache)

    https://kernel.0voice.com/forum.php?mod=viewthread&tid=629   玩转Linux内核 发布于 2022-8-9 22:19:08 阅读  ...

  5. CPU算力提升与实际性能提升的关系

    关于SPEC2006CPU和RedisBenchmark的理解 最近研究过硬件CPU的性能和Redis这样单线程重IO服务 突然想对比一下CPU算力提升占Redis性能提升的比率情况 性能很大程度由C ...

  6. [转帖]1.IPtable基础命令总结

    https://www.cnblogs.com/kcxg/p/10350870.html 规则查询 #查看对应表中的所有规则 iptables -t 表名 -L #查看表的指定链中的规则 iptabl ...

  7. [转帖]Linux系统awk命令详解

    AWK 是一种处理文本文件的语言,是一个强大的文本分析工具. 之所以叫 AWK 是因为其取了三位创始人 Alfred Aho,Peter Weinberger, 和 Brian Kernighan 的 ...

  8. [转帖]解读内核 sysctl 配置中 panic、oops 相关项目

    写在前面 本篇文章的内容主要来自内核源码树 Documentation/admin-guide/sysctl/kernel.rst文件. softlockup vs hardlockup softlo ...

  9. [转帖]JVM 参数

    https://www.cnblogs.com/xiaojiesir/p/15636100.html 我们可以在启动 Java 命令时指定不同的 JVM 参数,让 JVM 调整自己的运行状态和行为,内 ...

  10. Docker 运行 MongoDB的简单办法

    Docker 运行 MongoDB的简单办法 第一步拉取镜像 docker pull mongo 第二步创建自己的目录 地址 10.24.22.240 创建目录 mkdir /mongodb 第三步 ...