题解 P2910 【[USACO08OPEN]寻宝之路Clear And Present Danger】
说起来这还是本蒟蒻学完Floyd之后做的第一道题。
emm...这是一道裸题,题目大致是说有一堆岛,岛之间有海盗,因此每一条边都有一个危险指数(权重),然后给出一段必须经过的路线,求从一号小岛走到N号小岛最小的危险指数是多少。
先介绍一下Floyd算法吧:
Floyd(弗洛伊德)算法是用来求解带权图(无论正负)中的多源最短路问题。算法的原理是动态规划。
用dist(i,j,k)表示从顶点i到顶点j只经过前k个顶点的最短路的长度。那么只有如下两种情况1.i,j之间的最短路不经过k+1,dist(i,j,k+1)<-dist(i,j,k)
2.i,j之间的最短路经过k+1,dist(i,j,k+1)<-dist(i,k+1,k)+dist(k+1,j,k)。
所以dist(i,j,k+1)<-min{dist(i,j,k),dist(i,k+1,k)+dist(k+1,j,k)}。
_在算法实现的时候可以省略掉k那一维,只需要用一个二维数组即可。
——《ACM国际大学生程序设计竞赛 知识与入门》
AC代码见下。
其中dist为dp数组,order用于储存要求必须走的那一段路程。
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std; const int maxn = ; int n, m, ans = ;
int dist[maxn][maxn];
int order[]; int main() {
cin >> n >> m;
for(int i = ; i <= m; i++) cin >> order[i];
for(int i = ; i <= n; i++)
for(int j = ; j <= n; j++) {
cin >> dist[i][j];
} for(int k = ; k <= n; k++)
for(int i = ; i <= n; i++)
for(int j = ; j <= n; j++)
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]); for(int i = ; i <= m; i++) ans += dist[order[i - ]][order[i]];
ans += dist[][order[]];
ans += dist[order[m]][n]; cout << ans;
}
题解 P2910 【[USACO08OPEN]寻宝之路Clear And Present Danger】的更多相关文章
- 洛谷——P2910 [USACO08OPEN]寻宝之路Clear And Present Danger
P2910 [USACO08OPEN]寻宝之路Clear And Present Danger 题目描述 Farmer John is on a boat seeking fabled treasur ...
- (最短路 Floyd) P2910 [USACO08OPEN]寻宝之路Clear And Present Danger 洛谷
题意翻译 题目描述 农夫约翰正驾驶一条小艇在牛勒比海上航行. 海上有N(1≤N≤100)个岛屿,用1到N编号.约翰从1号小岛出发,最后到达N号小岛. 一张藏宝图上说,如果他的路程上经过的小岛依次出现了 ...
- 洛谷 P2910 [USACO08OPEN]寻宝之路Clear And Present Danger
题目描述 Farmer John is on a boat seeking fabled treasure on one of the N (1 <= N <= 100) islands ...
- P2910 [USACO08OPEN]寻宝之路Clear And Present Danger 洛谷
https://www.luogu.org/problem/show?pid=2910 题目描述 Farmer John is on a boat seeking fabled treasure on ...
- P2910 [USACO08OPEN]寻宝之路Clear And Present Danger |Floyd
题目描述 农夫约翰正驾驶一条小艇在牛勒比海上航行. 海上有N(1≤N≤100)个岛屿,用1到N编号.约翰从1号小岛出发,最后到达N号小岛. 一张藏宝图上说,如果他的路程上经过的小岛依次出现了Ai,A2 ...
- Luogu [USACO08OPEN]寻宝之路Clear And Present Danger
题目描述 Farmer John is on a boat seeking fabled treasure on one of the N (1 <= N <= 100) islands ...
- [USACO08OPEN]寻宝之路Clear And Present Danger
OJ题号:洛谷2910 思路:Floyd #include<cstdio> #include<algorithm> using namespace std; int main( ...
- 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 ...
- 1624: [Usaco2008 Open] Clear And Present Danger 寻宝之路
1624: [Usaco2008 Open] Clear And Present Danger 寻宝之路 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 3 ...
随机推荐
- Java绘图技术基础
public class Demo1 extends JFrame{ MyPanel mp=null; public static void main(String[] args){ Demo1 de ...
- JSON.stringify(),JSON.parse(),eval(string)
JSON.stringify()用于从一个对象解析出字符串 : var obj = {"name":"week","age":" ...
- HDU 6051 If the starlight never fade(原根+推式子)
题目大意: 设\(f(i)\)为使\((x+y)^i \equiv x^i (mod\ p)\)成立的(x,y)的对数.其中\(1 \leq x \leq p-1 , 1\leq y\leq m\), ...
- 【jQuery03】简单的选项卡切换
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- wackoPicko 渗透平台的安装
2016-05-17 wackoPicko 的介绍及下载地址 https://github.com/adamdoupe/WackoPicko#from=codefrom.com 首先我们 ...
- 管理windows自启动程序
1. 点击开始,在运行程序框中输入msconfig,然后回车. 在弹出的对话框中,点击”启动“选项卡,在启动项目列表中,把不需要的启动项目前面的对号去掉. 然后切换到”服务“选项卡,这里的服务项目列表 ...
- Failed to initialize component [Connector[HTTP/1.1-8086]]
严重: Failed to initialize end point associated with ProtocolHandler ["http-apr-8086"] java. ...
- TreeMap集合怎样依照Value进行排序
------- android培训.java培训.期待与您交流! ---------- 我们知道,TreeMap集合是依照Key进行排序的,怎样依照Value进行排序呢?如今有一个TreeMap集合 ...
- UVA 11426 GCD - Extreme (II) (数论|欧拉函数)
题意:求sum(gcd(i,j),1<=i<j<=n). 思路:首先能够看出能够递推求出ans[n],由于ans[n-1]+f(n),当中f(n)表示小于n的数与n的gcd之和 问题 ...
- 建筑建模学习笔记2——3DMax房屋框架建模
以下这幅图是用3DMax做出的大体的框架 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I ...