首页 > 其他分享 >NC25005 [USACO 2008 Ope S]Clear And Present Danger

NC25005 [USACO 2008 Ope S]Clear And Present Danger

时间:2023-01-03 21:57:59浏览次数:62  
标签:danger NC25005 island Clear treasure int Farmer islands 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;
}

标签:danger,NC25005,island,Clear,treasure,int,Farmer,islands,Danger
From: https://www.cnblogs.com/BlankYang/p/17023469.html

相关文章

  • Clearview Mac 电子书阅读器
    介绍Clearview是一款易于使用的标签式电子书阅读器,配备了图书馆书架,支持流行的电子书格式,如PDF、EPUB(DRM免费)、CHM和MOBI。您可以制作注释,插入书签,并自由搜索。注意:不支持......
  • element Cascader级联选择框clearCheckedNodes失效的解决办法
    问题:当我只选择了父级选项,并没有选择二级选项,然后关闭弹框后再次打开弹框,此时仍然保留了上次选中的父级选项(重新打开图二需回到图一) 图一图二  解决:找了一圈......
  • RecyclearView的基本使用
    <androidx.recyclerview.widget.RecyclerViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:id=......
  • php中的clearstatcache
    clearstatcache()函数的作用是:清除文件状态缓存。PHP的缓存数据对更快更好的运行函数是非常有利的。如果一个文件在脚本中测试了多次,你也许会禁......
  • Discourse 的 An error occurred: Title seems unclear, did you mean to enter it in
    Discourse在发布主题的时候,在默认情况下会遇到:Anerroroccurred:Titleseemsunclear,didyoumeantoenteritinALLCAPS?的提示。 ​​​​ 问题和解决出现这个......
  • Discourse 的 An error occurred: Title seems unclear, did you mean to enter it in
    Discourse在发布主题的时候,在默认情况下会遇到:Anerroroccurred:Titleseemsunclear,didyoumeantoenteritinALLCAPS?的提示。  问题和解决出现这个......
  • ClearCase是全球领先的软件配置管理工具
    ClearCase是全球领先的软件配置管理工具,而VisualStudio2005是微软推出的强大的.Net开发工具。微软的VSIP计划为VisualStudio提供了界面的可扩展性,通过VSIP,ClearCase将自......
  • 2022-11-18 clearInterval(定时器)无法完全关闭定时器
    问题描述:vue+uniapp之小程序定时器业务原代码:clearInterval(this.timer)this.timer为定时器的容器,多此点击开启定时器然后就会发现你想使用clearInterval(this.timer)来......
  • CF1740G Dangerous Laser Power
    题面传送门不是很理解为啥场上只有很少的人过掉这道题。首先看看这种题目不让你输出最大值而且SPJ还死难写的就猜它全部可以取到并且最优解唯一。然后你翻了翻CF提交记......
  • DIV流式布局(float和clear)
    语法: float:none|left|right 参数: none:对象不浮动left:对象浮在左边right:对象浮在右边 说明: 该属性的值指出了对象是否及如何浮动。请......