[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的更多相关文章

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

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

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

    题目描述 Farmer John is distributing chocolates at the barn for Valentine's day, and B (1 <= B <= ...

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

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

  4. USACO Chocolate Giving

    洛谷 P2984 [USACO10FEB]给巧克力Chocolate Giving 洛谷传送门 JDOJ 2680: USACO 2010 Feb Silver 2.Chocolate Giving ...

随机推荐

  1. 如何使用Mac远程控制Windows电脑?

    如何使用Mac远程控制Windows电脑?在你开始之前,设置您要远程处理的 Windows 计算机. 先安装 Microsoft Remote Desktop. 打开 Microsoft Remote ...

  2. 内网渗透 Metasploit(MSF)基础使用

    免责申明 以下内容仅供学习使用,非法使用造成的问题由使用人承担 攻击思路 漏洞探测(信息收集) <- fsacn,namp | 漏洞利用 <- 工具(msf等) | 获取服务器权限 MSF ...

  3. RESTful风格openapi接口设计+openapi远程服务调用

    我们平常开发一般只使用GET.POST方法.而对于HTTP给出的PUT.DELETE等其他方法都没使用.以RESTful风格设计接口就能全部用上这些方法. 按照RESTful理查德森成熟度模型改造接口 ...

  4. DRF之通过GenericAPIView的视图子类实现数据的增删改查接口

    1.安装DRF pip install djangorestframework 2.将DRF注册到APP中 INSTALLED_APPS = [ 'django.contrib.admin', 'dj ...

  5. CSS——阴影

    <!DOCTYPE html> <html> <head> <style> p.one { text-shadow: 3px 5px 5px #FF00 ...

  6. C# wpf之控制屏幕显示方向旋转

    using System;using System.Collections.Generic;using System.Linq;using System.Runtime.InteropServices ...

  7. 阿里云ECS后台CPU占用100%,top却找不到

    上周公司阿里云服务器后台报警,CPU占用瞬间飙升到100%: 首先想到使用top命令查询CPU占用详情: 发现进程占用CPU都比较低,在CPU占用一栏发现只有ni的占用比较高. 先了解一下CPU相关监 ...

  8. Node安装mongodb

    Node操作mongodb Mongoose介绍 网址:http://www.mongoosejs.net/docs/index.html mongoose是Node环境下异步操作mongodb数据库 ...

  9. ETL工具-nifi干货系列 第十二讲 nifi处理器UpdateRecord使用教程

    1.上一节课我们讲解了nifi处理器UpdateAttribute,专门用来更新flowFile的属性字段.本节课我们一起来学习UpdateRecord,该处理器用来更新flowFile的流文件内容数 ...

  10. Scrapy框架(九)--分布式爬虫

    分布式爬虫 - 概念:我们需要搭建一个分布式的机群,让其对一组资源进行分布联合爬取. - 作用:提升爬取数据的效率 - 如何实现分布式? - 安装一个scrapy-redis的组件 爬取到的数据自动存 ...