题目链接:https://www.luogu.org/problemnew/show/P2984

练习SPFA,把FJ当做起点,求出到所有牛的最短路,再把两个牛的相加。

 #include <cstdio>
#include <queue>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn = ;
const int inf = 0x7fffffff;
int n, m, b, dis[maxn];
bool vis[maxn];
struct edge{
int len, to, next, from;
}e[maxn];
int head[maxn], cnt;
queue<int> q;
void add(int u, int v, int w)
{
e[++cnt].from = u;
e[cnt].to = v;
e[cnt].len = w;
e[cnt].next = head[u];
head[u] = cnt;
}
int SPFA()
{
while(!q.empty())
{
int now = q.front();
q.pop();
vis[now] = ;
for(int i = head[now]; i ; i = e[i].next)
{
if(dis[e[i].to] > dis[now] + e[i].len)
{
dis[e[i].to] = dis[now] + e[i].len;
if(!vis[e[i].to])
{
vis[e[i].to] = ;
q.push(e[i].to);
}
}
}
}
}
int main()
{
scanf("%d%d%d",&n,&m,&b);
for(int i = ; i <= m; i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
add(v,u,w);
}
for(int i = ; i <= n; i++)
dis[i] = inf;
dis[] = ;
q.push();
vis[] = ;
SPFA();
for(int i = ; i <= b; i++)
{
int p,q;
scanf("%d%d",&p,&q);
printf("%d\n",dis[p]+dis[q]);
}
return ;
}

【luogu 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. 洛谷 P2984 [USACO10FEB]给巧克力Chocolate Giving

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

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

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

  4. luogu P2985 [USACO10FEB]吃巧克力Chocolate Eating

    题目描述 Bessie拿到了N (1 <= N <= 50,000)块巧克力.她决定想个办法吃掉这些巧克力,使得它在吃巧克力的这段时间里,最不开心的一天尽可能的开心.并且一共吃D (1 & ...

  5. [USACO10FEB]给巧克力Chocolate Giving

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

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

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

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

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

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

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

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

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

随机推荐

  1. 生成ks.cfg文件

    RHEL 7下生成ks.cfg文件 环境 + RHEL 7 + 字符界面, 没有安装图形界面 软件包安装 + `yum install system-config-kickstart -y` + `y ...

  2. js 中onclick 事件 点击后指向自己的对象,查找或者添加属性 用关键字this 传入参数 (可以改变原标签css)

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  3. asp.net MVC3之AJAX实现(json)

    asp.net MVC3之AJAX实现(json)         分类:             Asp.net MVC              2011-08-10 13:55     2272 ...

  4. linux服务器时间自动同步

    最常用的是 rdate 服务 安装(centOs为例): yum install -y rdate 用法: rdate -s time-b.nist.gov 执行完以上方法时间就同步了.有的服务器隔一 ...

  5. 将代码托管到github,并生成链接访问

    转眼间加入github的阵营已经两年多了,看到别人建立的个人博客挺好看的,因此,自己从此喜欢上了github,总结下自己的相关知识. 推荐学习Git的网址: 猴子都能懂的GIT入门 http://ba ...

  6. -ms-zoom property

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. dataBinding与ListView及事件

    2015年Google IO大会分布了DataBinding库,能够更快捷便利的实现MVVM结构模式.但是,通过对DataBinding的学习,其中踩过得坑,今天要在这里记录一下.对于DataBind ...

  8. vue1.0 与 Vue2.0的一些区别 及用法

    1.Vue2.0的模板标记外必须使用元素包起来: eg:Vue1.0的写法 <!DOCTYPE html> <html> <head> <meta chars ...

  9. css控制文本单行或者多行溢出显示为省略号...

    p:first-child { background-color: pink; width: 150px; line-height: 28px; font-size: 20px; overflow: ...

  10. PopupWindow简单使用

    如图是效果图 当点击 “点我”的按钮是   会弹出 如图的 弹窗 补充为PopupWindow设置一个显示动画和消失的动画 先在anim的文件下分别设置显示和消失的动画 <?xml versio ...