USACO Chocolate Giving
洛谷 P2984 [USACO10FEB]给巧克力Chocolate Giving
JDOJ 2680: USACO 2010 Feb Silver 2.Chocolate Giving
Description
Farmer John is distributing chocolates at the barn for Valentine's
day, and B (1 <= B <= 25,000) of his bulls have a special cow in
mind to receive a chocolate gift.
Each of the bulls and cows is grazing alone in one of the farm's N
(2*B <= N <= 50,000) pastures conveniently numbered 1..N and connected
by M (N-1 <= M <= 100,000) bidirectional cowpaths of various lengths.
Some pastures might be directly connected by more than one cowpath.
Cowpath i connects pastures R_i and S_i (1 <= R_i <= N; 1 <= S_i
<= N) and has length L_i (1 <= L_i <= 2,000).
Bull i resides in pasture P_i (1 <= P_i <= N) and wishes to give a
chocolate to the cow in pasture Q_i (1 <= Q_i <= N).
Help the bulls find the shortest path from their current pasture
to the barn (which is located at pasture 1) and then onward to the
pasture where their special cow is grazing. The barn connects, one
way or another (potentially via other cowpaths and pastures) to
every pasture.
As an example, consider a farm with 6 pastures, 6 paths, and 3 bulls
(in pastures 2, 3, and 5) who wish to bestow chocolates on their
love-objects:
*1 <-- Bull wants chocolates for pasture 1 cow
[4]--3--[5] <-- [5] is the pasture ID
/ |
/ |
4 2 <-- 2 is the cowpath length
/ | between [3] and [4]
[1]--1--[3]*6
/ \ /
9 3 2
/ \/
[6] [2]*4
* The Bull in pasture 2 can travel distance 3 (two different ways)
to get to the barn then travel distance 2+1 to pastures [3] and
[4] to gift his chocolate. That's 6 altogether.
* The Bull in pasture 5 can travel to pasture 4 (distance 3), then
pastures 3 and 1 (total: 3 + 2 + 1 = 6) to bestow his chocolate
offer.
* The Bull in pasture 3 can travel distance 1 to pasture 1 and then
take his chocolate 9 more to pasture 6, a total distance of 10.
Input
* Line 1: Three space separated integers: N, M, and B
* Lines 2..M+1: Line i+1 describes cowpath i with three
space-separated integers: R_i, S_i, and L_i
* Lines M+2..M+B+1: Line M+i+1 contains two space separated integers:
P_i and Q_i
Output
* Lines 1..B: Line i should contain a single integer, the smallest
distance that the bull in pasture P_i must travel to get
chocolates from the barn and then award them to the cow of his
dreams in pasture Q_i
Sample Input
6 7 3 1 2 3 5 4 3 3 1 1 6 1 9 3 4 2 1 4 4 3 2 2 2 4 5 1 3 6
Sample Output
6 6 10
题目翻译:
Farmer John有B头奶牛(1<=B<=25000),有N(2*B<=N<=50000)个农场,编号1-N,有M(N-1<=M<=100000)条双向边,第i条边连接农场R_i和S_i(1<=R_i<=N;1<=S_i<=N),该边的长度是L_i(1<=L_i<=2000)。居住在农场P_i的奶牛A(1<=P_i<=N),它想送一份新年礼物给居住在农场Q_i(1<=Q_i<=N)的奶牛B,但是奶牛A必须先到FJ(居住在编号1的农场)那里取礼物,然后再送给奶牛B。你的任务是:奶牛A至少需要走多远的路程?
输入格式:
第一行:三个用空格隔开的整数N,M和B。
第二到M+1行:第i+1行用R_i,S_i和L_i三个用空格隔开的整数描述双向边i。
第M+2到M+B+1行:第M+i+1行包含两个用空格隔开的整数P_i和Q_i。
输出格式:
第一到B行:第i行包括一个整数,居住在农场P_i的公牛从FJ那里取得情人节巧克力后送给他居住在农场Q_i的梦中情牛至少需要走的距离。
题解:
我觉得算是裸的最短路。
这数据量用dij或者SPFA都是极好的。
就是这个变量名非常不友好...
NMB??
不要在意这些细节。
我们处理一遍SPFA就可以把dist数组处理出来,然后询问的时候去调用就可以。
代码:
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int n,m,b;
int tot,to[200001],val[200001],nxt[200001],head[100001];
int v[100001],dist[100001];
void add(int x,int y,int z)
{
to[++tot]=y;
val[tot]=z;
nxt[tot]=head[x];
head[x]=tot;
}
void spfa()
{
memset(dist,0x3f,sizeof(dist));
memset(v,0,sizeof(v));
queue<int> q;
q.push(1);
v[1]=1;
dist[1]=0;
while(!q.empty())
{
int x=q.front();
q.pop();
v[x]=0;
for(int i=head[x];i;i=nxt[i])
{
int y=to[i];
if(dist[y]>dist[x]+val[i])
{
dist[y]=dist[x]+val[i];
if(v[y]==0)
q.push(y),v[y]=1;
}
}
}
}
int main()
{
scanf("%d%d%d",&n,&m,&b);
for(int i=1;i<=m;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);
add(y,x,z);
}
spfa();
for(int i=1;i<=b;i++)
{
int p,q;
scanf("%d%d",&p,&q);
int ans=dist[p]+dist[q];
printf("%d\n",ans);
}
return 0;
}
USACO Chocolate Giving的更多相关文章
- BZOJ 2015: [Usaco2010 Feb]Chocolate Giving( 最短路 )
裸最短路.. ------------------------------------------------------------------------------------ #include ...
- 2015: [Usaco2010 Feb]Chocolate Giving
2015: [Usaco2010 Feb]Chocolate Giving Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 269 Solved: 1 ...
- 洛谷 P2984 [USACO10FEB]给巧克力Chocolate Giving
题目描述 Farmer John is distributing chocolates at the barn for Valentine's day, and B (1 <= B <= ...
- 洛谷——P2984 [USACO10FEB]给巧克力Chocolate Giving
https://www.luogu.org/problem/show?pid=2984 题目描述 Farmer John is distributing chocolates at the barn ...
- bzoj2015 [Usaco2010 Feb]Chocolate Giving
Description Farmer John有B头奶牛(1<=B<=25000),有N(2*B<=N<=50000)个农场,编号1-N,有M(N-1<=M<=10 ...
- 【BZOJ】2015: [Usaco2010 Feb]Chocolate Giving(spfa)
http://www.lydsy.com/JudgeOnline/problem.php?id=2015 这种水题真没啥好说的.. #include <cstdio> #include & ...
- 【luogu P2984 [USACO10FEB]给巧克力Chocolate Giving】 题解
题目链接:https://www.luogu.org/problemnew/show/P2984 练习SPFA,把FJ当做起点,求出到所有牛的最短路,再把两个牛的相加. #include <cs ...
- bzoj 2015: [Usaco2010 Feb]Chocolate Giving【spfa】
因为是双向边,所以相当于两条到1的最短路和,先跑spfa然后直接处理询问即可 #include<iostream> #include<cstdio> #include<q ...
- [USACO10FEB]给巧克力Chocolate Giving
题意简叙: FarmerFarmerFarmer JohnJohnJohn有B头奶牛(1<=B<=25000)(1<=B<=25000)(1<=B<=25000), ...
随机推荐
- [转载]3.2 UiPath鼠标操作文本的介绍和使用
一.鼠标(mouse)操作的介绍 模拟用户使用鼠标操作的一种行为,例如单击,双击,悬浮.根据作用对象的不同我们可以分为对元素的操作.对文本的操作和对图像的操作 二.鼠标对文本的操作在UiPath中的使 ...
- c# 自动给版本升级,遇9变0且前面一个版本加1
/// <summary> /// 版本辅助类 /// 创建人: /// 创建时间:2019-11-18 13:53:55 /// </summary> public clas ...
- Zookeeper 运维实践手册
Zookeeper是一个高可用的分布式数据管理与协调框架,该框架能很好地保证分布式环境中数据一致性.一般用来实现服务发现(类似DNS),配置管理,分布式锁,leader选举等. 一.生产环境中Zook ...
- c#的IDisposable
尽量在using中使用非托管资源 1.实现Dispose方法 2.提取一个受保护的Dispose虚方法,在该方法中实现具体的释放资源的逻辑 3.添加析构函数 4.添加一个私有的bool类型的字段,作为 ...
- Vue.js 源码分析(十三) 基础篇 组件 props属性详解
父组件通过props属性向子组件传递数据,定义组件的时候可以定义一个props属性,值可以是一个字符串数组或一个对象. 例如: <!DOCTYPE html> <html lang= ...
- sql server获取格式化的当前日期
在SQL Server中获取格式化成yyyy-MM-dd的当前日期: ), ); -- 2019-06-17 首先我们知道getdate()函数是用来获取当前日期的,它返回的类型是datetime类型 ...
- sql server 中取前n行字段最大值问题
例子 取前三行最大ID ID from Students)AS A 这样写得到的却是整个表的最大ID值,并不是我们需要的值 要在句中加入order by ID ID from Students ord ...
- 转 Sqlserver_left join 、right join、 inner join 用法
https://www.cnblogs.com/ingstyle/p/4368064.html left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 right joi ...
- 2019 企叮咚java面试笔试题 (含面试题解析)
本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.企叮咚等公司offer,岗位是Java后端开发,因为发展原因最终选择去了企叮咚,入职一年时间了,也成为了面试官 ...
- Linux 集群概念 , wsgi , Nginx负载均衡实验 , 部署CRM(Django+uwsgi+nginx), 部署学城项目(vue+uwsgi+nginx)
Linux 集群概念 , wsgi , Nginx负载均衡实验 , 部署CRM(Django+uwsgi+nginx), 部署学城项目(vue+uwsgi+nginx) 一丶集群和Nginx反向代理 ...