NC25005 [USACO 2008 Ope S]Clear And Present Danger
题目
题目描述
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的更多相关文章
- 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 ...
- BZOJ 1624: [Usaco2008 Open] Clear And Present Danger 寻宝之路( 最短路 )
直接floyd.. ----------------------------------------------------------------------- #include<cstdio ...
- 1624: [Usaco2008 Open] Clear And Present Danger 寻宝之路
1624: [Usaco2008 Open] Clear And Present Danger 寻宝之路 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 3 ...
- 洛谷——P2910 [USACO08OPEN]寻宝之路Clear And Present Danger
P2910 [USACO08OPEN]寻宝之路Clear And Present Danger 题目描述 Farmer John is on a boat seeking fabled treasur ...
- BZOJ 1624: [Usaco2008 Open] Clear And Present Danger 寻宝之路
Description 农夫约翰正驾驶一条小艇在牛勒比海上航行. 海上有N(1≤N≤100)个岛屿,用1到N编号.约翰从1号小岛出发,最后到达N号小岛.一 张藏宝图上说,如果他的路程上经过的小岛依次出 ...
- bzoj1624 [Usaco2008 Open] Clear And Present Danger 寻宝之路
Description 农夫约翰正驾驶一条小艇在牛勒比海上航行. 海上有N(1≤N≤100)个岛屿,用1到N编号.约翰从1号小岛出发,最后到达N号小岛.一 张藏宝图上说,如果他的路程上 ...
- [Usaco2008 Open] Clear And Present Danger 寻宝之路[最短路][水]
Description 农夫约翰正驾驶一条小艇在牛勒比海上航行. 海上有N(1≤N≤100)个岛屿,用1到N编号.约翰从1号小岛出发,最后到达N号小岛.一 张藏宝图上说,如果他的路程上 ...
- Luogu [USACO08OPEN]寻宝之路Clear And Present Danger
题目描述 Farmer John is on a boat seeking fabled treasure on one of the N (1 <= N <= 100) islands ...
- (最短路 Floyd) P2910 [USACO08OPEN]寻宝之路Clear And Present Danger 洛谷
题意翻译 题目描述 农夫约翰正驾驶一条小艇在牛勒比海上航行. 海上有N(1≤N≤100)个岛屿,用1到N编号.约翰从1号小岛出发,最后到达N号小岛. 一张藏宝图上说,如果他的路程上经过的小岛依次出现了 ...
- [USACO08OPEN]寻宝之路Clear And Present Danger
OJ题号:洛谷2910 思路:Floyd #include<cstdio> #include<algorithm> using namespace std; int main( ...
随机推荐
- zookeeper 集群环境搭建及集群选举及数据同步机制
本文为博主原创,未经允许不得转载: 目录: 1. 分别创建3个data目录用于存储各节点数据 2. 编写myid文件 3. 编写配置文件 4.分别启动 5.分别查看状态 6. 检查集群复制情况 ...
- 根据返回的多层Json来进行创建文件,达到根据阶层创建,然后压缩成压缩包进行下载
临时接到一个需求说让根据按照下面的这个图片的结构来打包下载指定位置下的文件到指定位置! 实现思路: 1.把已经实现的树形结构的代码进行调用,拿到他的数据进行创建对应的文件夹 2.因为结构下方的文件没有 ...
- JS - 递归实现无限分类
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 理解Asp.net MVC模式
MVC是模型(Model).视图(View).控制器(Controller)的缩写,它是Web应用程序中一种常用的架构模式.这种模式将应用程序大体上分为3层,即Model层.View层和Control ...
- [转帖]xtrabackup2.4备份恢复脚本
https://developer.aliyun.com/article/534230#:~:text=xtrabackup2.4%E5%A4%87%E4%BB%BD%E6%81%A2%E5%A4%8 ...
- [转帖]华为OpenEuler欧拉系统添加epel源方法
https://blog.whsir.com/post-7002.html 由于国产华为OpenEuler欧拉系统的版本命名是22.03.22.03这种,并且在查看版本的路径中是/etc/open ...
- [转帖]Nginx(3):上手Nginx,从配置文件开始
https://cloud.tencent.com/developer/article/1886147?areaSource=&traceId= 其实吧,我配置 tcp 负载均衡的时候也就 ...
- elementUI(datepicker)限制日日期的选择
指定起始日期,后选的将会受到先选的限制 参考地址 https://www.jianshu.com/p/c59c8ef6c500 实现方法不难,利用了 change 事件,动态改变 picker-opt ...
- 去除 i 标签的倾斜样式;如何引入本地的阿里字体图标
去除 i 标签的倾斜样式 i{ font-style:normal; } 如何引入本地的阿里字体图标 将代码下载下来 当然你将下载下载来的资源有用的放在静态资源中 然后在 main.js 引入: ma ...
- Fabric网络升级(一)
原文来自这里. 本章节主要介绍如何从之前的版本或其他长期支持版本升级至最新版. 从2.1升级到2.2 Fabric v2.1和v2.2都是稳定版,以bug修复和其它形式的代码加固位置.因此,升级不需要 ...