Cow Toll Paths(floyd变形)
鏈接:https://ac.nowcoder.com/acm/contest/1077/K
來源:??途W(wǎng)
時(shí)間限制:C/C++ 1秒,其他語言2秒
空間限制:C/C++ 32768K,其他語言65536K
64bit IO Format: %lld
題目描述
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 Aj and Bj (1 <= Aj <= N; 1 <= Bj <= N). FJ has assigned a toll Lj (1 <= Lj <= 100,000) to the path connecting pastures Aj and Bj.
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 Ci (1 <= Ci <= 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 si and ti (1 <= si <= N; 1 <= ti <= N; si != ti) 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.
輸入描述:
- Line 1: Three space separated integers: N, M, and K
- Lines 2…N+1: Line i+1 contains a single integer: Ci
- Lines N+2…N+M+1: Line j+N+1 contains three space separated integers: Aj, Bj, and Lj
- Lines N+M+2…N+M+K+1: Line i+N+M+1 specifies query i using two space-separated integers: si and ti
輸出描述: - Lines 1…K: Line i contains a single integer which is the lowest cost of any route from si to ti
示例1
輸入
復(fù)制
輸出
復(fù)制
/*
考察對floyd的理解。
本題要求 加上 所選路徑中 點(diǎn)權(quán)最大值的 最短路徑。
本題難點(diǎn)就在于更新 所選路徑中 點(diǎn)權(quán) 最大值。
而floyd中 中轉(zhuǎn)點(diǎn)的順序 是可以隨意的,
抓住這點(diǎn)按點(diǎn)權(quán)從小到大排序,
然后 重新 給點(diǎn)編號。
如果此時(shí)中轉(zhuǎn)點(diǎn)可以松弛本條路徑,
那么此時(shí)本條路徑點(diǎn)券最大值肯定是當(dāng)前中轉(zhuǎn)點(diǎn)的點(diǎn)權(quán)。
*/
總結(jié)
以上是生活随笔為你收集整理的Cow Toll Paths(floyd变形)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Music Notes(前缀和+二分)
- 下一篇: 过河卒(简单dp)