题目描述

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),表示起点和终点的草地。

输入输出格式

输入格式:

  • 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:

5 7 2
2
5
3
3
4
1 2 3
1 3 2
2 5 3
5 3 1
5 4 1
2 4 3
3 4 4
1 4
2 3
输出样例#1:

8
9
 #include<bits/stdc++.h>
using namespace std;
const int MAXN=;
const int maxn=0x7ffffff;
int read()
{
int ret=,ok=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')ok=-;
ch=getchar();
}
for(;ch>=''&&ch<='';ch=getchar())
ret=ret*+ch-'';
return ret*ok;
}
int a[MAXN][MAXN];
int spend[MAXN][MAXN];
int b[MAXN];
int n,m,q;
inline void floyd()
{
for(int k=;k<=n;k++)
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
{
int p=a[i][k]+a[k][j]+max(spend[i][k],spend[k][j]);
if((p<a[i][j]+spend[i][j])&&a[i][k]<maxn&&a[k][j]<maxn)
{
a[i][j]=a[i][k]+a[k][j];
spend[i][j]=max(spend[i][k],spend[k][j]);
}
}
}
int main()
{
n=read();
m=read();
q=read();
for(int i=;i<=n;i++)
b[i]=read();
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
{
if(i==j)
a[i][j]=;
else
a[i][j]=maxn;
}
for(int i=;i<=m;i++)
{
int a1,b1,c;
a1=read(),b1=read(),c=read();
if(a[a1][b1]>c)
{
a[a1][b1]=c;
a[b1][a1]=c;
spend[a1][b1]=max(b[a1],b[b1]);
spend[b1][a1]=max(b[a1],b[b1]);
}
}
floyd();
floyd();
floyd();
for(int i=;i<=q;i++)
{
int x;int y;
x=read(),y=read();
cout<<a[x][y]+spend[x][y]<<endl;
}
return ;
}

我们可以分别记录下从点x到点y 的最短路径的长度和在最短路上的花费

然后就是套Floyd的模板。

但是注意,因为你的长度和花费是分开记录的

所以一遍Floyd跑出来的不一定是最小值

我们可以多跑几遍试试

Luogu P2966 [USACO09DEC]牛收费路径Cow Toll Paths的更多相关文章

  1. P2966 [USACO09DEC]牛收费路径Cow Toll Paths

    P2966 [USACO09DEC]牛收费路径Cow Toll Paths 题目描述 Like everyone else, FJ is always thinking up ways to incr ...

  2. 洛谷 P2966 [USACO09DEC]牛收费路径Cow Toll Paths

    题目描述 Like everyone else, FJ is always thinking up ways to increase his revenue. To this end, he has ...

  3. [Luogu P2966][BZOJ 1774][USACO09DEC]牛收费路径Cow Toll Paths

    原题全英文的,粘贴个翻译题面,经过一定的修改. 跟所有人一样,农夫约翰以宁教我负天下牛,休叫天下牛负我的伟大精神,日日夜夜苦思生财之道.为了发财,他设置了一系列的规章制度,使得任何一只奶牛在农场中的道 ...

  4. [USACO09DEC]牛收费路径Cow Toll Paths(floyd、加路径上最大点权值的最短路径)

    https://www.luogu.org/problem/P2966 题目描述 Like everyone else, FJ is always thinking up ways to increa ...

  5. [USACO09DEC]牛收费路径Cow Toll Paths

    跟所有人一样,农夫约翰以着宁教我负天下牛,休叫天下牛负我的伟大精神,日日夜夜苦思生 财之道.为了发财,他设置了一系列的规章制度,使得任何一只奶牛在农场中的道路行走,都 要向农夫约翰上交过路费. 农场中 ...

  6. 洛谷 2966 2966 [USACO09DEC]牛收费路径Cow Toll Paths

    [题意概述] 给出一个图,点有正点权,边有正边权,通过两点的代价为两点间的最短路加上路径通过的点的点权最大值. 有M个询问,每次询问通过两点的代价. [题解] 先把点按照点权从小到大排序,然后按照这个 ...

  7. 【[USACO09DEC]牛收费路径Cow Toll Paths】

    很妙的一道题,我之前一直是用一个非常暴力的做法 就是枚举点权跑堆优化dijkstra 但是询问次数太多了 于是一直只有50分 今天终于抄做了这道题,不贴代码了,只说一下对这道题的理解 首先点权和边权不 ...

  8. P2966 [USACO09DEC]Cow Toll Paths G

    题意描述 Cow Toll Paths G 这道题翻译的是真的不错,特别是第一句话 给定一张有 \(n\) 个点 \(m\) 条边的无向图,每条边有边权,每个点有点权. 两点之间的路径长度为所有边权 ...

  9. Luogu P3033 [USACO11NOV]牛的障碍Cow Steeplechase(二分图匹配)

    P3033 [USACO11NOV]牛的障碍Cow Steeplechase 题意 题目描述 --+------- -----+----- ---+--- | | | | --+-----+--+- ...

随机推荐

  1. [leetcode-543-Diameter of Binary Tree]

    Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a b ...

  2. 3.VBScript基础

    1.VBS只有一种数据类型 ->Variant类似于泛类型,其中具体类型会在调用的时候具体化 2.声明变量可以用Dim语句,Public语句,Private语句 声明多个变量用逗号分隔 也可以隐 ...

  3. Easyui设置动态表格,动态导出数据实例,附Demo

    最近开发的过程中碰到一个客户提出的需求,一个指定的页面导出需要提供一个弹出页面选择列表页面需要显示的列,页面确认之后需要修改列表页面显示的表格,导出的数据也需要同步变化. 总结一下可以称为一个列表数据 ...

  4. 第三章(jQuery中的DOM操作)

    3.1 DOM 操作分类 ①DOM Core 包括(getElementById() , getElementsByTagName() , getAttribute() , setAttribute( ...

  5. iOS- 解决iOS10 App启动时放大铺满App Icon的问题

    0.前言 iOS10 App启动时放大铺满App图标 iPad Application shows app icon as launch screen in iOS 10 如图,点击APP后APP图标 ...

  6. Java IO学习笔记(五)对象流

    1.Object流:直接将Object写入或读出. 2.序列化:将Object直接转化成字节流写到硬盘或网络上. 3.如果想把一个对象转化成字节流,该对象的实体类必须得实现Serializable接口 ...

  7. mac版破解office

    下载地址:http://ereach-public.oss-cn-shanghai.aliyuncs.com/office%202016%20for%20mac.dmg 解压密码:www.ifunma ...

  8. Maven部署(linux)

    1.下载 进入http://maven.apache.org/download.cgi下载.或者使用wget命令. mkdir /opt/maven cd /opt/maven wget http:/ ...

  9. .NET C#到Java没那么难,DB篇

    前言 .NET C#到Java没那么难,都是面向对象的语言,而且语法还是相似的,先对比一下开发环境,再到Servlet,再到MVC,都是一样一样的,只是JAVA的配制项比较多而已,只要配好一个,后面都 ...

  10. Dapper.Rainbow 简单使用

    一.  Dapper 简介        一个效率比较高的微型ORM.   二 . Dapper.Rainbow        Dapper的扩展,在这个扩展里面实现了 Dynamic 的 插入和更新 ...