当前位置: 代码迷 >> 综合 >> 【例题题解】[USACO]过路费:理解floyed算法的本质
  详细解决方案

【例题题解】[USACO]过路费:理解floyed算法的本质

热度:59   发布时间:2023-12-17 11:42:37.0

题面

Like everyone else, FJ is always thinking up ways to increase his revenue. To this end, he has set up a series of tolls that the cows will pay when they traverse the cowpaths throughout the farm.

The cows move from any of the N (1 <= N <= 250) pastures conveniently numbered 1…N to any other pasture over a set of M (1 <= M <= 10,000) bidirectional cowpaths that connect pairs of different pastures A_j and B_j (1 <= A_j <= N; 1 <= B_j <= N). FJ has assigned a toll L_j (1 <= L_j <= 100,000) to the path connecting pastures A_j and B_j.

While there may be multiple cowpaths connecting the same pair of pastures, a cowpath will never connect a pasture to itself. Best of all, a cow can always move from any one pasture to any other pasture by following some sequence of cowpaths.

In an act that can only be described as greedy, FJ has also assigned a toll C_i (1 <= C_i <= 100,000) to every pasture. The cost of moving from one pasture to some different pasture is the sum of the tolls for each of the cowpaths that were traversed plus a single additional toll that is the maximum of all the pasture tolls encountered along the way, including the initial and destination pastures.

The patient cows wish to investigate their options. They want you to write a program that accepts K (1 <= K <= 10,000) queries and outputs the minimum cost of trip specified by each query. Query i is a pair of numbers s_i and t_i (1 <= s_i <= N; 1 <= t_i <= N; s_i != t_i) specifying a starting and ending pasture.

Consider this example diagram with five pastures:

The ‘edge toll’ for the path from pasture 1 to pasture 2 is 3. Pasture 2’s ‘node toll’ is 5.

To travel from pasture 1 to pasture 4, traverse pastures 1 to 3 to 5 to 4. This incurs an edge toll of 2+1+1=4 and a node toll of 4 (since pasture 5’s toll is greatest), for a total cost of 4+4=8.

The best way to travel from pasture 2 to pasture 3 is to traverse pastures 2 to 5 to 3. This incurs an edge toll of 3+1=4 and a node toll of 5, for a total cost of 4+5=9.

跟所有人一样,农夫约翰以着宁教我负天下牛,休叫天下牛负我的伟大精神,日日夜夜苦思生 财之道。为了发财,他设置了一系列的规章制度,使得任何一只奶牛在农场中的道路行走,都 要向农夫约翰上交过路费。 农场中由N(1 <= N <= 250)片草地(标号为1到N),并且有M(1 <= M <= 10000)条 双向道路连接草地A_j和B_j(1 <= A_j <= N; 1 <= B_j <= N)。

奶牛们从任意一片草 地出发可以抵达任意一片的草地。FJ已经在连接A_j和B_j的双向道路上设置一个过路费L_j (1 <= L_j <= 100,000)。 可能有多条道路连接相同的两片草地,但是不存在一条道路连接一片草地和这片草地本身。最 值得庆幸的是,奶牛从任意一篇草地出发,经过一系列的路径,总是可以抵达其它的任意一片 草地。 除了贪得无厌,叫兽都不知道该说什么好。

FJ竟然在每片草地上面也设置了一个过路费C_i (1 <= C_i <= 100000)。从一片草地到另外一片草地的费用,是经过的所有道路的过路 费之和,加上经过的所有的草地(包括起点和终点)的过路费的最大值。 任劳任怨的牛们希望去调查一下她们应该选择那一条路径。

她们要你写一个程序,接受K(1 <= K <= 10,000)个问题并且输出每个询问对应的最小花费。第i个问题包含两个数字s_i 和t_i(1 <= s_i <= N; 1 <= t_i <= N; s_i != t_i),表示起点和终点的草地。

Solution

对于这道题,我们可以尝试从floyed算法的本质去理解这道题

对于每一个i,j,若一定经过中间点k,我们就需要找到一个一定经过点k的最短路与这条路径中的最大值。

根据题目,我们一定可以知道要用floyed算法来解决。

先理解三重循环k,i,j的含义,i和j表示一个区间的左右端点,k表示一个区间的中间点。那么就有迭代时的f[i][j]=min(f[i][j],f[i][k]+f[k][j])的更新最短路的方式。若顺序枚举;则i到j的最短路径的中转点一定是1~k的节点,即经过的可能的点的集合为{i,j,1,2,…,k}。当我们确定了k的时候,如果当前进行了转移,我们就找到一个决策:经过k的决策且i到j更短了,此时加上一个最大值就变成了一个合法的答案。

如何做呢??

我们可以将这些点按照点的点权从小到大排序,这样最大值一定是max(ai,aj,ak).

那么代码就很容易得到了:

#include<bits/stdc++.h>
using namespace std;
const int N