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

题目描述

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

floyd中要先枚举中间点k,我们可以按照点权从小到大排序,在计算最大点权的时候只要考虑i,j,k三者中点权的最大值即可。

通过排序使得最大值变成当前点的点权。

 #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN = ;
struct node{
int v,id;
bool operator < (const node &a) const
{
return v < a.v;
}
}c[MAXN];
int w[MAXN][MAXN];
int d[MAXN][MAXN];
int t[MAXN];
int n,m,q; void floyd()
{
for (int a=; a<=n; ++a)
{
int k = c[a].id;
for (int i=; i<=n; ++i)
for (int j=; j<=n; ++j)
{
d[i][j] = d[j][i] = min(d[i][k]+d[k][j],d[i][j]);
w[i][j] = w[j][i] = min(w[i][j],d[i][j]+max(c[a].v,max(t[i],t[j])));
}
}
}
int main()
{
memset(w,0x3f,sizeof(w));
scanf("%d%d%d",&n,&m,&q);
for (int i=; i<=n; ++i)
{
scanf("%d",&c[i].v);
c[i].id = i;
}
for (int i=; i<=n; i++)
for (int j=; j<=n; j++)
if (i!=j) d[i][j] = d[j][i] = w[i][j] = w[j][i] = 1e9;
else w[i][j] = w[j][i] = c[i].v;
sort(c+,c+n+);
for (int i=; i<=n; i++)
t[c[i].id] = c[i].v;
for (int x,y,z,i=; i<=m; ++i)
{
scanf("%d%d%d",&x,&y,&z);
if (z<d[x][y])
d[x][y]=d[y][x]=z;
}
floyd();
while (q--)
{
int x,y;
scanf("%d%d",&x,&y);
printf("%d\n",w[x][y]);
}
return ;
}

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

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

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

  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. [USACO09DEC] Cow Toll Paths

    https://www.luogu.org/problem/show?pid=2966 题目描述 Like everyone else, FJ is always thinking up ways t ...

随机推荐

  1. c++利用互斥锁实现读写锁

    很简单就是在读的时候把写的锁锁住就好了 class readwrite_lock { public: readwrite_lock() : read_cnt(0) { } void readLock( ...

  2. HashMap对HashCode碰撞的处理

    先说Java之外的,什么是拉链法?怎么解决冲突的: 拉链法解决冲突的做法是:将所有关键字为同义词的结点链接在同一个单链表中. 若选定的散列表长度为m,则可将散列表定义为一个由m个头指针组成的指针数组t ...

  3. 逻辑回归(分类问题)(Logistic Regression、罗杰斯特回归)

    逻辑回归:问题只有两项,即{0, 1}.一般而言,回归问题是连续模型,不用在分类问题上,且噪声较大,但如果非要引入,那么采用逻辑回归模型. 对于一般训练集: 参数系统为: 逻辑回归模型为:      ...

  4. (转)理解YOLOv2训练过程中输出参数含义

    最近有人问起在YOLOv2训练过程中输出在终端的不同的参数分别代表什么含义,如何去理解这些参数?本篇文章中我将尝试着去回答这个有趣的问题. 刚好现在我正在训练一个YOLOv2模型,拿这个真实的例子来讨 ...

  5. jsp的4个作用域区别( pageScope、requestScope、sessionScope、applicationScope)

    简单描述 page里的变量没法从index.jsp传递到test.jsp.只要页面跳转了,它们就不见了. request里的变量可以跨越forward前后的两页.但是只要刷新页面,它们就重新计算了.  ...

  6. 【题解】洛谷P1072 Hankson的趣味题 (gcd和lcm的应用)

    洛谷P1072:https://www.luogu.org/problemnew/show/P1072 思路 gcd(x,a0)=a1 lcm(x,b0)=b1→b0*x=b1*gcd(x,b0) ( ...

  7. CSS3 小会

    -webkit-perspective:800px;   其子元素获得3D元素支持,这里是设置子元素距离试图的位置:若设为0 则不支持子元素获得3d效果,不支持透视. 常与transform:rota ...

  8. 封装全局icon组件 svg (仿造element-ui源码)

    一.引入  svg-sprite-loader 插件 npm install svg-sprite-loader --save-dev vue-cli项目默认情况下会使用 url-loader 对sv ...

  9. 复制D:\\day05目录下的所有文件到D:\\copy,并将.txt文件改为.java文件。

    **解题思路: 1.首先定义一个静态的refile方法,参数传入两个文件路径 2.要复制目录下的所有文件,首先查询File类的方法,可以使用listFiles方法得到目录下的文件 3.想到这问题基本就 ...

  10. 轻量ORM-SqlRepoEx (二)初始化SqlRepoEx

    一.SqlRepoEx引用 暂时没放至nuget上,可以直接到https://github.com/AzThinker/SqlRepoEx下载源码,编译引用. (一).静态引用 1.需引用以下dll在 ...