P2984
[USACO10FEB]Chocolate Giving S
题意描述
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至少需要走多远的路程?
输入
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
输出
6
6
10
点拨
转化一下就是对原点求两个点的最短路
代码
#include<iostream>
#include<utility>
#include<queue>
using namespace std;
typedef long long ll;
#define fi(i,a,b) for(int i = a; i <= b; ++i)
#define fr(i,a,b) for(int i = a; i >= b; --i)
#define x first
#define y second
#define sz(x) ((int)(x).size())
#define pb push_back
using pii = pair<int,int>;
#define int long long
//#define DEBUG
const int N = 1e5 + 5;
int cnt = 1;
int head[50005];
int dis[50005];
bool vis[50005];
struct edge{
int e,w,ne;
}edge[N];
struct node{
int e,w;
bool operator < (const node p) const{
return w > p.w;
}
};
void add(int a,int b,int c){
edge[cnt].e = b;
edge[cnt].w = c;
edge[cnt].ne = head[a];
head[a] = cnt++;
}
int n,m,b;
void djstra(int s){
priority_queue<node> pri;
pri.push({s,0});
while(!pri.empty()){
node temp = pri.top();
pri.pop();
int e = temp.e;
int w = temp.w;
vis[e] = true;
for(int j = head[e];j!=0;j=edge[j].ne){
int p = edge[j].e;
int q = edge[j].w;
if(dis[p] >= dis[e] + q){
dis[p] = dis[e] + q;
if(!vis[p]) pri.push({p,dis[p]});
}
}
}
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
#ifdef DEBUG
//freopen(D:\in.txt,r,stdin);
#endif
cin >> n >> m >> b;
fi(i,1,n) dis[i] = 0x3f3f3f3f;
dis[1] = 0;
fi(i,1,m) {
int a,b,c;
cin >> a >> b >> c;
add(a,b,c);
add(b,a,c);
}
djstra(1);
fi(i,1,b){
int a,b;
cin >> a >> b;
cout << dis[a] + dis[b] << endl;
}
return 0;
}
P2984的更多相关文章
- 【luogu P2984 [USACO10FEB]给巧克力Chocolate Giving】 题解
题目链接:https://www.luogu.org/problemnew/show/P2984 练习SPFA,把FJ当做起点,求出到所有牛的最短路,再把两个牛的相加. #include <cs ...
- 洛谷 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 ...
- USACO Chocolate Giving
洛谷 P2984 [USACO10FEB]给巧克力Chocolate Giving 洛谷传送门 JDOJ 2680: USACO 2010 Feb Silver 2.Chocolate Giving ...
随机推荐
- 安利一个好用的IDEA插件 object-helper-plugin
更多精彩博文请关注:听到微笑的博客 一. 插件背景 object-helper 插件是一个日常开发工具集插件,提供丰富的功能,最开始是基于 GenerateO2O 插件开发而来,它提供了对象之间值拷贝 ...
- ThreadLocal原理详解——终于弄明白了ThreadLocal
目录 概述 API介绍 ThreadLocal的理解 ThreadLocal的原理分析 总结 概述 在java学习生涯中可能很多人都会听到ThreadLocal变量,从字面上理解ThreadLocal ...
- protoc-gen-go: error:inconsistent package names: , prototest
如果你已经安装proto ,以及go生成proto插件.但还是报这种错误,请看一下是否 protoc --go_out=./ *.proto 指令打错了
- SwiftUI(二)- 页面导航NavigationLink和Sheet窗口(模态视图)
NavigationLink 官方文档对NavigationLink的定义: A button that triggers a navigation presentation when pressed ...
- Vue 页面传参方式 Query 和 Params
1. query 与 params 传参 query 需要和配合 path 属性使用,携带参数会拼接在请求路径后,效果同 Get 请求方式 http://localhost:8033/Permissi ...
- 『手撕Vue-CLI』函数柯里化优化代码
开篇 在上一篇文章中,给 nue-cli 添加了拉取版本号的功能,这一次来优化一下代码,使用函数柯里化的方式来优化代码. 实现 函数柯里化 函数柯里化是一种将使用多个参数的一个函数转换成一系列使用一个 ...
- 使用sysdig查看容器里的系统调用
目录 一.系统环境 二.前言 三.系统调用简介 四.Sysdig简介 五.使用sysdig查看容器里的系统调用 5.1 以二进制包的形式安装sysdig 5.2 使用sysdig查看容器里的系统调用 ...
- vue3+vant 引入Dialog Toast都会失败报错not defined
今天在封装vant组件的时候,刚好要用到toast提示信息的组件,索性就按照官网提供的引入方法进行正常的引入,嘿,好家伙,一顿操作下来后发现竟然报Toast未定义,这就纳闷了,明明步骤都是对的啊,所以 ...
- 【技巧】JS代码这么写,前端小姐姐都会爱上你
前言 缘由 JS代码小技巧,教你如何守株待妹 你想听的故事: 顶着『前端小王子』的称号,却无法施展自己的才能. 想当年本狗赤手空拳打入前端阵地,就是想通过技术的制高点来带动前端妹子.奈何时不待我,前端 ...
- 算法金 | LSTM 原作者带队,一个强大的算法模型杀回来了
大侠幸会,在下全网同名「算法金」 0 基础转 AI 上岸,多个算法赛 Top 「日更万日,让更多人享受智能乐趣」 时间拉回 2019 年,有「计算机界诺贝尔奖」之称图灵奖获得者公布,深度学习三巨头:Y ...