当前位置: 首页 > news >正文

牛客 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.

思路:要求按照路线顺序的最小路径长度,我们可以利用弗洛伊德算法预处理出所有点之间的最短路,然后按照路线顺序将所有最短路相加即是答案;

AC代码:

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;

const int N=110,M=10010;

int d[N][N];
int n,m;
int seq[M];

void floyd()
{
	for(int k=1;k<=n;k++)
		for(int i=1;i<=n;i++)
			for(int j=1;j<=n;j++)
				d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
}

int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++)
		scanf("%d",&seq[i]);
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			scanf("%d",&d[i][j]);
	floyd();
	
	int res=0;
	for(int i=2;i<=m;i++)
		res+=d[seq[i-1]][seq[i]];
	cout<<res<<endl;
}

相关文章:

  • 洛谷 P2349:金字塔 ← 链式前向星 dfs
  • Flink—窗口、时间和水印
  • Cadence OrCAD Capture 查找功能详细介绍
  • 物联网病毒Mirai可靠性分析
  • c语言实现数据结构中的单向链表
  • (没学懂,待填坑)【动态规划】数位动态规划
  • 小功能⭐️Unity判断是否单击到了UI
  • 常见的传输介质及其特性
  • 660——第一章
  • vue中计算属性computed的特性和应用
  • UAC实现原理
  • 【通信】Matlab实现多同步压缩变换
  • Element常用api webview
  • C语言结构体小栗子
  • 空间数据结构管理---RTree (下篇,代码实例)
  • 《Javascript高级程序设计 (第三版)》第五章 引用类型
  • 【399天】跃迁之路——程序员高效学习方法论探索系列(实验阶段156-2018.03.11)...
  • js
  • js中的正则表达式入门
  • vue--为什么data属性必须是一个函数
  • 大主子表关联的性能优化方法
  • 开源SQL-on-Hadoop系统一览
  • 聊聊sentinel的DegradeSlot
  • 如何在 Tornado 中实现 Middleware
  • 什么软件可以提取视频中的音频制作成手机铃声
  • 使用 @font-face
  • 使用 Node.js 的 nodemailer 模块发送邮件(支持 QQ、163 等、支持附件)
  • 试着探索高并发下的系统架构面貌
  • “十年磨一剑”--有赞的HBase平台实践和应用之路 ...
  • Java总结 - String - 这篇请使劲喷我
  • Semaphore
  • #HarmonyOS:Web组件的使用
  • #pragma预处理命令
  • #Ubuntu(修改root信息)
  • (2/2) 为了理解 UWP 的启动流程,我从零开始创建了一个 UWP 程序
  • (8)Linux使用C语言读取proc/stat等cpu使用数据
  • (Pytorch框架)神经网络输出维度调试,做出我们自己的网络来!!(详细教程~)
  • (zt)基于Facebook和Flash平台的应用架构解析
  • (阿里巴巴 dubbo,有数据库,可执行 )dubbo zookeeper spring demo
  • (过滤器)Filter和(监听器)listener
  • (十一)c52学习之旅-动态数码管
  • (一) storm的集群安装与配置
  • (原創) 人會胖會瘦,都是自我要求的結果 (日記)
  • .htaccess配置重写url引擎
  • .NET CORE使用Redis分布式锁续命(续期)问题
  • .net framework profiles /.net framework 配置
  • .NET 事件模型教程(二)
  • [1] 平面(Plane)图形的生成算法
  • [20170705]diff比较执行结果的内容.txt
  • [20170713] 无法访问SQL Server
  • [acwing周赛复盘] 第 94 场周赛20230311
  • [BUG] Authentication Error
  • [C#]使用PaddleInference图片旋转四种角度检测
  • [CareerCup] 17.8 Contiguous Sequence with Largest Sum 连续子序列之和最大
  • [CTSC2014]企鹅QQ