[USACO09DEC]牛收费路径Cow Toll Paths(floyd、加路径上最大点权值的最短路径)
https://www.luogu.org/problem/P2966
题目描述
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.
跟所有人一样,农夫约翰以着宁教我负天下牛,休叫天下牛负我的伟大精神,日日夜夜苦思生财之道。为了发财,他设置了一系列的规章制度,使得任何一只奶牛在农场中的道路行走,都要向农夫约翰上交过路费。 农场中由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),表示起点和终点的草地。
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: C_i
* Lines N+2..N+M+1: Line j+N+1 contains three space separated
integers: A_j, B_j, and L_j
* Lines N+M+2..N+M+K+1: Line i+N+M+1 specifies query i using two space-separated integers: s_i and t_i
输出格式
* Lines 1..K: Line i contains a single integer which is the lowest cost of any route from s_i to t_i
输入输出样例
输入 #1
输出 #1
题目大意:
给定 N 个节点,M 条边的无向图,边有边权,点有点权,现给出 Q 个询问,每个询问查询两个节点之间的最短路径,
这里最短路径的定义是两个节点之间的最短路径与这条路径中经过的节点点权的最大值之和。
题解:
多源最短路问题,数据范围这么小,很显然就应该用 floyd 算法来处理,
由于最短路径涉及到路径中最大的点权,因此如何在决策阶段快速进行状态转移,得出路径中最大的点权是这道题考虑的核心。若每次进行枚举点权,复杂度显然爆炸。
我们重新回想一遍floyd算法的原理:
i到j有两种可能:直接到和借助中间接口k,所以取一个min就行了
我们再往下细细的想一下,k代表的是中间的接口,而且k的枚举顺序是任意的?
显然是任意的!突破口就在这:我们可以肆意的修改k的枚举顺序!,所以,这道题就解决了。
我们对点按权值从小到大重新编号,这是的中转点k代表的就是重新编号的点,k越大,点权越大
我们只需要从小到大枚举中转点k,由于k是从小到大枚举的
我们在找i,j之间的最短路的时候,我们的中转点如果只是枚举到了k,这说明了我们当前的i,j之间的最短路中并没有点权超过k点点权大的点,最后的k即作为i->j的路径上除i,j外点权的最大值,
然后就可以去跑flyod,该路径上点权的最大值即为 i,j,k 三个点中点权的最大值!
因此在开始时对点权进行排序,这样对于带点权的最短路径,时间复杂度为 O(n3)。
另:单纯计算多源最短路时,各个节点之间的顺序对答案无影响。(显然如此,故可以对点重新编号)
代码如下:
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <math.h>
const int INF=0x3f3f3f3f;
typedef long long LL;
const int mod=1e9+;
const double PI=acos(-);
const int maxn=;
using namespace std;
//ios::sync_with_stdio(false);
// cin.tie(NULL); int n,m,K;
struct node
{
int id;//最初编号
int cost;//点权
}point[];//存点的信息
int dis[][];//新编号的最短路径
int ans[][];//带点权的最短路径(新编号),即答案
int rank[];//存放新编号,由原始编号查找新编号 bool cmp(node a,node b)
{
return a.cost<b.cost;
} int main()
{
//freopen("sample.txt","r",stdin);
scanf("%d %d %d",&n,&m,&K);
for(int i=;i<=n;i++)
{
scanf("%d",&point[i].cost);
point[i].id=i;
}
sort(point+,point++n,cmp);//按点权从小到大排序
for(int i=;i<=n;i++)
{
rank[point[i].id]=i;//因为询问是用原始编号,这里存一下新编号
}
memset(dis,INF,sizeof(dis));
for(int i=;i<=n;i++)
{
dis[i][i]=;
}
for(int i=;i<m;i++)
{
int u,v,w;
scanf("%d %d %d",&u,&v,&w);
dis[rank[u]][rank[v]]=dis[rank[v]][rank[u]]=min(dis[rank[u]][rank[v]],w);
}
memset(ans,INF,sizeof(ans));
for(int k=;k<=n;k++)
{
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
if(i==j)
continue;
dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
if(dis[i][j]==dis[i][k]+dis[k][j])//可去掉,但去掉运行时间会增加很多
ans[i][j]=min(ans[i][j],dis[i][j]+max(point[i].cost,max(point[j].cost,point[k].cost)));
}
}
}
for(int i=;i<K;i++)
{
int u,v;
scanf("%d %d",&u,&v);
printf("%d\n",ans[rank[u]][rank[v]]);
}
return ;
}
[USACO09DEC]牛收费路径Cow Toll Paths(floyd、加路径上最大点权值的最短路径)的更多相关文章
- P2966 [USACO09DEC]牛收费路径Cow Toll Paths
P2966 [USACO09DEC]牛收费路径Cow Toll Paths 题目描述 Like everyone else, FJ is always thinking up ways to incr ...
- BZOJ1774[USACO 2009 Dec Gold 2.Cow Toll Paths]——floyd
题目描述 跟所有人一样,农夫约翰以着宁教我负天下牛,休叫天下牛负我的伟大精神,日日夜夜苦思生 财之道.为了发财,他设置了一系列的规章制度,使得任何一只奶牛在农场中的道路行走,都 要向农夫约翰上交过路费 ...
- Luogu P2966 [USACO09DEC]牛收费路径Cow Toll Paths
题目描述 Like everyone else, FJ is always thinking up ways to increase his revenue. To this end, he has ...
- 洛谷 P2966 [USACO09DEC]牛收费路径Cow Toll Paths
题目描述 Like everyone else, FJ is always thinking up ways to increase his revenue. To this end, he has ...
- [USACO09DEC]牛收费路径Cow Toll Paths
跟所有人一样,农夫约翰以着宁教我负天下牛,休叫天下牛负我的伟大精神,日日夜夜苦思生 财之道.为了发财,他设置了一系列的规章制度,使得任何一只奶牛在农场中的道路行走,都 要向农夫约翰上交过路费. 农场中 ...
- [Luogu P2966][BZOJ 1774][USACO09DEC]牛收费路径Cow Toll Paths
原题全英文的,粘贴个翻译题面,经过一定的修改. 跟所有人一样,农夫约翰以宁教我负天下牛,休叫天下牛负我的伟大精神,日日夜夜苦思生财之道.为了发财,他设置了一系列的规章制度,使得任何一只奶牛在农场中的道 ...
- 【[USACO09DEC]牛收费路径Cow Toll Paths】
很妙的一道题,我之前一直是用一个非常暴力的做法 就是枚举点权跑堆优化dijkstra 但是询问次数太多了 于是一直只有50分 今天终于抄做了这道题,不贴代码了,只说一下对这道题的理解 首先点权和边权不 ...
- 洛谷 2966 2966 [USACO09DEC]牛收费路径Cow Toll Paths
[题意概述] 给出一个图,点有正点权,边有正边权,通过两点的代价为两点间的最短路加上路径通过的点的点权最大值. 有M个询问,每次询问通过两点的代价. [题解] 先把点按照点权从小到大排序,然后按照这个 ...
- P2966 [USACO09DEC]Cow Toll Paths G
题意描述 Cow Toll Paths G 这道题翻译的是真的不错,特别是第一句话 给定一张有 \(n\) 个点 \(m\) 条边的无向图,每条边有边权,每个点有点权. 两点之间的路径长度为所有边权 ...
随机推荐
- 新发布的廉价版iPhoneXR值得购买吗?
每年苹果九月份的发布会,都是一场声势浩大的全民狂欢"大趴体".其中最兴奋的,自然就是各路段子手--就指望着苹果发布会来提供一年的笑料呢!而今年苹果发布会召开之后,网上最流行的就是下 ...
- Keras + Flask 提供接口服务的坑~~~
最近在搞Keras,训练完的模型要提供个预测服务出来.就想了个办法,通过Flask提供一个http服务,后来发现也能正常跑,但是每次预测都需要加载模型,效率非常低. 然后就把模型加载到全局,每次要用的 ...
- bootstrap快速上手
bootstarp快速上手 首先英文不是非常好无法阅读英文文档的同学的可以翻阅其他团队翻译的官方:http://code.z01.com/ 项目依赖 ,css文件在所有样式之前,js依赖,首先jq,再 ...
- selenium登陆qq邮箱页面
from selenium import webdriver driver = webdriver.Chrome() driver.get('https://mail.qq.com/cgi-bin/l ...
- ZJNU 2351 - 快乐
由题意得,如果有个人从前往后能找到第一个不低于自己等级的任务,就会接取其后所有任务 那么就可以让输入数据处理成递增数列 例如1 3 5 4 6 2 7 7 3 可以处理成1 3 5 5 6 6 7 7 ...
- Dynamics CRM - 为 Form 或者字段设置 Error Notification
在 Dynamics CRM 开发中,我们一般要利用 JS 来做一些数据验证的功能,我们也需要将验证结果显示出来,比起直接 alert 出信息来提示用户的方式,CRM 提供了更加美观和人性化的方式来通 ...
- 美团:WSDM Cup 2019自然语言推理任务获奖解题思路
WSDM(Web Search and Data Mining,读音为Wisdom)是业界公认的高质量学术会议,注重前沿技术在工业界的落地应用,与SIGIR一起被称为信息检索领域的Top2. 刚刚在墨 ...
- Linux 配置单机yum源--ISO镜像做源
前提:防火墙关闭.SElinus关闭 1.上传ISO镜像(建议传到home目录下) [root@localhost home]# ls iso/ CentOS-.iso 2.挂载目录 [root@lo ...
- windows 安装Bitcoin Core使用
1.官网下载https://bitcoin.org/en/download 选择Windows 其他系统就选择对应的就好 2.双击安装完过后,进入bin目录,打开bitcoin-qt.exe运行,提 ...
- 转发和重定向简介及与之相关的(URL)参数(parameter)、属性(attribute)问题探讨
1.引子 转发和重定向是我们在做web项目中常用到的两个术语,有必要理清两者的区别和与之相关的参数.属性获取问题. 2.转发和重定向 1).转发 转发是服务器行为,将当前请求(Request)和响应( ...