题目描述

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.

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至少需要走多远的路程?

输入输出格式

输入格式:

  • 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

输出格式:

  • 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

输入输出样例

输入样例#1:

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
输出样例#1:

6
6
10
 
最短路
堆优化dijkstra练习 
但是 !
啊啊啊啊啊 堆优化dijkstra没过
就写了个spfa。。
#include <ctype.h>
#include <cstring>
#include <cstdio>
#include <queue>
#define M 500005
using namespace std;
struct node
{
int x,y;
bool operator<(node a)const
{
return y>=a.y;
}
};
priority_queue<node>q;
void read(int &x)
{
x=;bool f=;
register char ch=getchar();
for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=;
for(; isdigit(ch);ch=getchar()) x=x*+ch-'';
x=f?-x:x;
}
bool vis[M];
int head[M],Next[M],to[M],dist[M],cnt,dis[M],n,m,b;
int main(int argc,char *argv[])
{
read(n);
read(m);
read(b);
for(int x,y,z;m--;)
{
read(x);
read(y);
read(z);
Next[++cnt]=head[x];to[cnt]=y;dist[cnt]=z;head[x]=cnt;
Next[++cnt]=head[y];to[cnt]=x;dist[cnt]=z;head[y]=cnt;
}
memset(dis,,sizeof(dis));
dis[]=;
node a;
a.x=;
a.y=dis[];
q.push(a);
for(;!q.empty();)
{
node b=q.top();q.pop();
if(vis[b.x]) continue;
vis[b.x]=;
for(int i=head[b.x];i;i=Next[i])
{
int v=to[i];
if(dis[v]>dis[b.x]+dist[i])
{
dis[v]=dis[b.x]+dist[i];
node a;
a.x=v;
a.y=dis[v];
q.push(a);
}
}
}
for(int x,y;b--;)
{
read(x);
read(y);
printf("%d\n",dis[x]+dis[y]);
}
return ;
}

90分的堆优化

#include <cstdio>
#include <queue>
#define N 400005 using namespace std;
queue<int>q;
bool vis[N];
int n,m,b,cnt,Next[N],to[N],head[N],dist[N],dis[N];
void ins(int u,int v,int w)
{
Next[++cnt]=head[u];to[cnt]=v;dist[cnt]=w;head[u]=cnt;
Next[++cnt]=head[v];to[cnt]=u;dist[cnt]=w;head[v]=cnt;
}
void spfa(int s)
{
for(int i=;i<=n;i++) dis[i]=0x7fffffff;
dis[s]=;
vis[s]=;
q.push(s);
while(!q.empty())
{
int now=q.front();
q.pop();
vis[now]=;
for(int i=head[now];i;i=Next[i])
{
int v=to[i];
if(dis[v]>dis[now]+dist[i])
{
dis[v]=dis[now]+dist[i];
if(!vis[v])
{
vis[v]=;
q.push(v);
}
}
}
}
}
int main()
{
scanf("%d%d%d",&n,&m,&b);
for(int x,y,z;m--;)
{
scanf("%d%d%d",&x,&y,&z);
ins(x,y,z);
}
spfa();
for(int x,y;b--;)
{
scanf("%d%d",&x,&y);
printf("%d\n",dis[x]+dis[y]);
}
return ;
}

洛谷 P2984 [USACO10FEB]给巧克力Chocolate Giving的更多相关文章

  1. 洛谷——P2984 [USACO10FEB]给巧克力Chocolate Giving

    https://www.luogu.org/problem/show?pid=2984 题目描述 Farmer John is distributing chocolates at the barn ...

  2. 洛谷——P2983 [USACO10FEB]购买巧克力Chocolate Buying

    P2983 [USACO10FEB]购买巧克力Chocolate Buying 题目描述 Bessie and the herd love chocolate so Farmer John is bu ...

  3. 洛谷 P2983 [USACO10FEB]购买巧克力Chocolate Buying 题解

    P2983 [USACO10FEB]购买巧克力Chocolate Buying 题目描述 Bessie and the herd love chocolate so Farmer John is bu ...

  4. 洛谷 P2983 [USACO10FEB]购买巧克力Chocolate Buying

    购买巧克力Chocolate Buying 乍一看以为是背包,然后交了一个感觉没错的背包上去. #include <iostream> #include <cstdio> #i ...

  5. 洛谷P2983 [USACO10FEB]购买巧克力Chocolate Buying

    题目描述 Bessie and the herd love chocolate so Farmer John is buying them some. The Bovine Chocolate Sto ...

  6. 洛谷—— P2983 [USACO10FEB]购买巧克力Chocolate Buying

    https://www.luogu.org/problem/show?pid=2983 题目描述 Bessie and the herd love chocolate so Farmer John i ...

  7. 【luogu P2984 [USACO10FEB]给巧克力Chocolate Giving】 题解

    题目链接:https://www.luogu.org/problemnew/show/P2984 练习SPFA,把FJ当做起点,求出到所有牛的最短路,再把两个牛的相加. #include <cs ...

  8. [USACO10FEB]给巧克力Chocolate Giving

    题意简叙: FarmerFarmerFarmer JohnJohnJohn有B头奶牛(1<=B<=25000)(1<=B<=25000)(1<=B<=25000), ...

  9. P2985 [USACO10FEB]吃巧克力Chocolate Eating

    P2985 [USACO10FEB]吃巧克力Chocolate Eating 题目描述 Bessie has received N (1 <= N <= 50,000) chocolate ...

随机推荐

  1. Firebug 的脚本页面不能用

    1.遇到这种情况,一般重置firebug,然后开启“脚本“功能,刷新页面,就能显示正常 2.要不就是 版本问题,50.0不行,下载回49版本就可以了

  2. [hdu2457]DNA repair(AC自动机+dp)

    题意:给出一些不合法的模式DNA串,给出一个原串,问最少需要修改多少个字符,使得原串中不包含非法串. 解题关键:多模式串匹配->AC自动机,求最优值->dp,注意在AC自动机上dp的套路. ...

  3. [小记]Windows下配置环境变量和需不需要重启问题

    经常看到一些软件的安装说明上写着,修改Windows的环境变量,然后重新启动计算机.这让人不禁产生疑问,修改环境变量之后真的要重启吗? 其实只要理解了环境变量的原理就可以做出正确的判断.环境变量是一些 ...

  4. CodeForces 1091G. New Year and the Factorisation Collaboration

    题目简述:若你获得“超能力”:固定$n$,对任意$a$,可以快速求出$x \in [0, n)$(若存在),使得$x^2 \equiv a \pmod n$,若存在多个$x$满足条件,则返回其中一个( ...

  5. Flask_restful 插件实战笔记——基本概念与使用

       最近在Resetful接口设计上想法还是挺多的,也实现了一些需求!想着整理下Flask_restful插件的基本知识,方便日后的复习!   官方地址:https://flask-restful. ...

  6. eclipse + tomcat 开发环境配置

    一. 下载tomcat和Eclipse 下载tomcat 下载地址:http://tomcat.apache.org/download-70.cgi 下载后解压如下图 下载eclipse 下载地址:h ...

  7. 看后端程序员调试CORS的姿势

    # 目录 为什么有同源策略? 需要解决的问题 CORS跨域请求方案 preflight withCredentials 附:高效.优雅地调试CORS实现 为什么有同源策略?       同源策略Sam ...

  8. 2014-10-23 NOIP模拟赛

    NOIP2014模拟赛 -----lwher 时限均为1s,内存 256MB 1.Jams倒酒(pour) Jams是一家酒吧的老板,他的酒吧提供2种体积的啤酒,a ml 和 b ml,分别使用容积为 ...

  9. Integrative Analysis of MicroRNAome, Transcriptome, and Proteome during the Limb Regeneration of Cynops orientalis (文献分享一组-翁海玉)

    文献名:Integrative Analysis of MicroRNAome, Transcriptome, and Proteome during the Limb Regeneration of ...

  10. 阿里云物联网 .NET Core 客户端 | CZGL.AliIoTClient:6. 设备事件上报

    文档目录: 说明 1. 连接阿里云物联网 2. IoT 客户端 3. 订阅Topic与响应Topic 4. 设备上报属性 4.1 上报位置信息 5. 设置设备属性 6. 设备事件上报 7. 服务调用 ...