Skip to main content
  1. Posts/

BZOJ 1624: [Usaco2008 Open] Clear And Present Danger 寻宝之路 (Floyd)

·2 mins
Table of Contents
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

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

Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 507  Solved: 345 [Submit][Status][Discuss]

Description
#

    农夫约翰正驾驶一条小艇在牛勒比海上航行.

    海上有N(1≤N≤100)个岛屿,用1到N编号.约翰从1号小岛出发,最后到达N号小岛.一

张藏宝图上说,如果他的路程上经过的小岛依次出现了Ai,A2,…,AM(2≤M≤10000)这样的序列(不一定相邻),那他最终就能找到古老的宝藏.  但是,由于牛勒比海有海盗出没.约翰知道任意两个岛屿之间的航线上海盗出没的概率,他用一个危险指数Dij(0≤Dij≤100000)来描述.他希望他的寻宝活动经过的航线危险指数之和最小.那么,在找到宝藏的前提下,这个最小的危险指数是多少呢?

Input
#

    第1行输入N和M,之后M行一行一个整数表示A序列,之后输入一个NxN的方阵,表示两两岛屿之间航线的危险指数.数据保证Dij=Dji,Dii=0.

Output
#

    最小的危险指数和.

Sample Input
#

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

INPUT DETAILS:

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.

Sample Output
#

7

OUTPUT DETAILS:

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.

HINT
#

Source
#

Silver

思路:可以把危险指数看成距离,于是floyd一下就好咯

 1/* ***********************************************
 2Author :111qqz
 3Created Time :2016年04月04日 星期一 02时54分18秒
 4File Name :code/bzoj/1624.cpp
 5************************************************ */
 6
 7#include <cstdio>
 8#include <cstring>
 9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < int ,int >
26#define MP make_pair
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33const int N=105;
34int d[N][N];
35int n,m;
36int a[10005];
37int main()
38{
39	#ifndef  ONLINE_JUDGE
40	freopen("code/in.txt","r",stdin);
41  #endif
42
43	ios::sync_with_stdio(false);
44	cin>>n>>m;
45	a[0]=1;
46	a[m+1]=n;
47	for ( int i = 1 ; i <= m ; i++) cin>>a[i];
48
49	for ( int i = 1 ; i <= n ; i++)
50	    for ( int j = 1 ; j <= n ; j++) cin>>d[i][j];
51
52	for ( int k = 1 ; k <= n ; k++)
53	    for ( int i = 1 ; i <= n ; i++)
54		for ( int j = 1 ; j <= n ; j++)
55		    d[i][j] = min(d[i][j],d[i][k]+d[k][j]);
56
57	int ans = 0 ;
58	for ( int i = 1 ; i <= m+1 ; i++)
59	    ans +=d[a[i-1]][a[i]];
60
61	cout<<ans<<endl;
62
63  #ifndef ONLINE_JUDGE
64  fclose(stdin);
65  #endif
66    return 0;
67}

Related

bc #74 div1 1001 || hdu 5636 Shortest Path (floyd?)

·2 mins
题目链接 题意:有一条n个节点的链,节点i和节点j的距离为abs(i-j) 现在新增加三条边,距离也都为1,然后给出m个询问,每组询问给出两个点s,t,问s,t之间的最短距离。 思路:比赛的时候没搞出来。 观察特点,对于大多数点来说,都是没有直接的改变,只是增加了三条边。总的思路是:之前s到t的距离为abs(s-t),通过枚举中间经过的特殊点,观察是否能使得距离减小。

poj 3660 Cow Contest (floyd,传递闭包)

http://poj.org/problem?id=3660 题意:给定n个奶牛,m个奶牛的关系,a,b表示a比b强…问能确定多少个奶牛的排名。 思路:最重要的一点是。。能确定奶牛i的排名的条件是。。知道奶牛i和其他n-1个奶牛的关系。。不管是能打败奶牛i也好。。会被奶牛i打败也好。。只要不是不确定就行。。所以我们跑一遍floyd做传递闭包。得到任何两个点之间的联系。然后对于每一个点。看其他n-1个点是否和他有关系。

codeforces 500 B. New Year Permutation

·1 min
http://codeforces.com/contest/500/problem/B 题意:给定一个1至n的数的一种排列。给定一个n*n的矩阵,a[i][j]==0代表pi,pj不可以交换,a[i][j]为1代表p[i],p[j]可以交换。 问字典序最小的排列。。