题目描述

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 

说明/提示

输入格式:

第一行:三个用空格隔开的整数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的梦中情牛至少需要走的距离。

坑点:N尽量比题目所给大一点,如果N=50000时,第二个测试点wrong,改成50005后,AC了,啦啦啦啦!!!

AC代码:

#include<iostream>
#include<cstring>
#include<queue>
#include<cstdio>
#include<vector>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ull;
int n,k,m;
const int N=50005;
struct node{int to;ull data;
};
vector<node>mp[N];
ull low[N];
void spfa(int s){memset(low,INF,sizeof(low));int start,next;start=s;low[s]=0;queue<int>Q;Q.push(start);while(!Q.empty()){start=Q.front();Q.pop();for(int i=0;i<mp[start].size();i++){next=mp[start][i].to;if(low[next]>low[start]+mp[start][i].data){low[next]=low[start]+mp[start][i].data;Q.push(next);}}}
}
int main(){scanf("%d%d%d",&n,&m,&k);for(int i=1;i<=m;i++){int u,v,w;scanf("%d%d%d",&u,&v,&w);node temp;temp.to=v;temp.data=w;mp[u].push_back(temp);temp.to=u;mp[v].push_back(temp);}spfa(1);for(int i=0;i<k;i++){int x,y;scanf("%d%d",&x,&y);ull sum=low[x]+low[y];printf("%lld\n",sum);}
}

P2984 [USACO10FEB]Chocolate Giving S【SPFA】相关推荐

  1. 数青蛙​、[USACO10FEB]Chocolate Giving S

    一.1419. 数青蛙 思路 这道题有俩种解法,一是记数,二是贪心 记数: 这是官方的题解 我们用frog_ num来表示现在正在发出蛙鸣声的青蛙数目,用cnt[c] 示已经发出-次有效蛙鸣中的字符c ...

  2. 洛谷 P3371 【模板】单源最短路径(弱化版)【最短路】【spfa】

    洛谷 P3371 [模板]单源最短路径(弱化版) 一.题目链接 二.题目分析 (一)算法标签 (二)解题思路 三.AC代码 四.其它题解 一.题目链接 洛谷 P3371 [模板]单源最短路径(弱化版) ...

  3. 【SPFA】桐人的约会

    桐人的约会 题目大意: 删掉一条边,让一个图中的最短路最长 原题: 题目描述 这是一个风和日丽的日子,桐人和诗乃在约会.他们所在的城市共有N个街区,和M条道路,每条道路连接两个不同的街区,并且通过一条 ...

  4. 【SPFA】最优贸易(luogu 1073)

    最优贸易 luogu 1073 题目大意: 有n个城市和m条线路连接着这些城市(当编号为1时是有向,2时是无向),从1城市出发到n城市,每个城市都有固定的水晶球价格(进价和售价一样),从路过的一个城市 ...

  5. 【SPFA】遭遇战 VijosP1404

    [问题描述] 背景 你知道吗,SQ Class的人都很喜欢打CS.(不知道CS是什么的人不用参加这次比赛). 描述 今天,他们在打一张叫DUSTII的地图,万恶的恐怖分子要炸掉藏在A区的SQC论坛服务 ...

  6. 【枚举】【SPFA】Urozero Autumn Training Camp 2016 Day 5: NWERC-2016 Problem I. Iron and Coal

    那个人派出的队伍的行走的路径一定前半程是重合的,后半程分叉开来. 于是预处理每个点离1号点的最短路,到最近的铁的最短路,到最近的煤的最短路.(三次BFS / SPFA)然后枚举分岔点,尝试更新答案即可 ...

  7. 【最短路】【spfa】小vijos P1447 Updown

    小vijos P1447 Updown 背景 开启了升降梯的动力之后,探险队员们进入了升降梯运行的那条竖直的隧道,映入眼帘 的是一条直通塔顶的轨道.一辆停在轨道底部的电梯.和电梯内一杆控制电梯升降的巨 ...

  8. HDOJ1874最短路【spfa】

    //不知道切过这道题目几次了,这次又wa了...双向路啊....这波简直无奈了,今晚又是浪成狗!!!! #include<cstdio> #include<vector> #i ...

  9. P3385-[模板]负环【SPFA】

    正题 题目大意 求点1可不可以走到负环. 解题思路 用cnticnt_icnti​表示到iii的最短路经过了多少个点,然后求若cnti≥ncnt_i\geq ncnti​≥n且这条路是负数那么就有负环 ...

最新文章

  1. 2012年技术图书大盘点
  2. 238. Product of Array Except Self
  3. 第三话 开关说它不认识“2”
  4. java web视频_超全面的JavaWeb视频教程
  5. java nio2 iocp_基于JDK7 NIO2的高性能web服务器实践之二(转)
  6. CM: Relationship between Note created in Fiori UI and webclient UI
  7. 如何优雅的理解HBase和BigTable
  8. Dom-Attribute对象
  9. 二、套接字类型与协议设置
  10. DM860步进电机接线及拨码
  11. 基于R语言利用QQ群进行数据挖掘案例整理
  12. 第五讲 中外数学名题趣题欣赏与解析
  13. 计算机组策略定时开机脚本,简单几步实现电脑定时开机
  14. 虚拟主播合成视频使用教程
  15. 大学c语言程序设计听不懂,C语言听不懂?那你还不点进来看看?
  16. Hotkeycontrol录制宏
  17. 小白装系统(超详细)
  18. 一文详解 URLEncode
  19. 怎么做成html网页,html网页制作的基本步骤?怎么用html做一个网页?
  20. android 刷机原理,分区。

热门文章

  1. 未知标记 html问题,显示错误:未知的HTML标记_phpstorm_开发99编程知识库
  2. Cocos平台集成AGC性能管理(一)—— Cocos应用发布
  3. 世纪互联开启数据中心精品时代
  4. php熔断,限流、熔断、降级
  5. python画图代码的输入数据可以取出来_python 导入数据及作图的实现
  6. 【第3版emWin教程】第34章 emWin6.x的AppWizard界面开发工具使用方法
  7. nginx 正則匹配與一般匹配競合
  8. 现在java现在发展前景怎么样?
  9. 光线控股猫眼 改变不了猫眼的宿命
  10. EmEditor注册码及EmEditor注册机下载