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

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

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

 1 #include <cstdio>
 2 #include <queue>
 3 #include <iostream>
 4 #include <algorithm>
 5 #include <cstring>
 6 using namespace std;
 7 const int maxn = 1000000;
 8 const int inf = 0x7fffffff;
 9 int n, m, b, dis[maxn];
10 bool vis[maxn];
11 struct edge{
12     int len, to, next, from;
13 }e[maxn];
14 int head[maxn], cnt;
15 queue<int> q;
16 void add(int u, int v, int w)
17 {
18     e[++cnt].from = u;
19     e[cnt].to = v;
20     e[cnt].len = w;
21     e[cnt].next = head[u];
22     head[u] = cnt;
23 }
24 int SPFA()
25 {
26     while(!q.empty())
27     {
28         int now = q.front();
29         q.pop();
30         vis[now] = 0;
31         for(int i = head[now]; i ; i = e[i].next)
32         {
33             if(dis[e[i].to] > dis[now] + e[i].len)
34             {
35                 dis[e[i].to] = dis[now] + e[i].len;
36                 if(!vis[e[i].to])
37                 {
38                     vis[vis[e[i].to]] = 1;
39                     q.push(e[i].to);
40                 }
41             }
42         }
43     }
44 }
45 int main()
46 {
47     scanf("%d%d%d",&n,&m,&b);
48     for(int i = 1; i <= m; i++)
49     {
50         int u,v,w;
51         scanf("%d%d%d",&u,&v,&w);
52         add(u,v,w);
53         add(v,u,w);
54     }
55     for(int i = 1; i <= n; i++)
56     dis[i] = inf;
57     dis[1] = 0;
58     q.push(1);
59     vis[1] = 1;
60     SPFA();
61     for(int i = 1; i <= b; i++)
62     {
63         int p,q;
64         scanf("%d%d",&p,&q);
65         printf("%d\n",dis[p]+dis[q]);
66     }
67     return 0;
68 }

原文地址:https://www.cnblogs.com/MisakaAzusa/p/8947222.html

时间: 2024-10-10 09:14:48

【luogu P2984 [USACO10FEB]给巧克力Chocolate Giving】 题解的相关文章

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

https://www.luogu.org/problem/show?pid=2984 题目描述 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 graz

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

题目描述 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 <=

luogu P2985 [USACO10FEB]吃巧克力Chocolate Eating

题目描述 Bessie拿到了N (1 <= N <= 50,000)块巧克力.她决定想个办法吃掉这些巧克力,使得它在吃巧克力的这段时间里,最不开心的一天尽可能的开心.并且一共吃D (1 <= D <= 50,000)天. 每块巧克力有一个开心值H_i (1 <= H_i <= 1,000,000),当某天你吃下那块巧克力时,你将获得那块巧克力的开心值.每一天的开心值是所有当天吃掉的巧克力的总开心值之和.每天晚上Bessie睡觉之后,它的开心值会减半.也就是说,比如昨天B

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

https://www.luogu.org/problem/show?pid=2983 题目描述 Bessie and the herd love chocolate so Farmer John is buying them some. The Bovine Chocolate Store features N (1 <= N <= 100,000) kinds of chocolate in essentially unlimited quantities. Each type i of

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

题目描述 Bessie and the herd love chocolate so Farmer John is buying them some. The Bovine Chocolate Store features N (1 <= N <= 100,000) kinds of chocolate in essentially unlimited quantities. Each type i of chocolate has price P_i (1 <= P_i <= 1

[USACO10FEB]吃巧克力Chocolate Eating

题目:洛谷P2985. 题目大意:有n块巧克力要吃d天,并且只能按顺序吃.一块巧克力有一个开心值,吃了就能增加开心值.一个人初始开心值为0,且每天早上开心值变为原来的一半.问如何吃巧克力才能使开心值最小的一天开心值最大(每天都按吃完巧克力后计算),且需要输出方案. 解题思路:最大化最小值问题,用二分答案. 贪心地扫描,对于一个答案,如果开心值不到这个答案,就一直吃巧克力即可. 最后输出吃的方案时也用此种贪心法. 注意如果最后巧克力没有吃完,则在最后一天全部吃掉. C++ Code: #inclu

【[USACO 2010 FEB】给巧克力Chocolate Giving(最短路)

题目描述 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,但是奶

2015: [Usaco2010 Feb]Chocolate Giving

2015: [Usaco2010 Feb]Chocolate Giving Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 269  Solved: 183[Submit][Status] Description 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&

bzoj2015[Usaco2010 Feb]Chocolate Giving*

bzoj2015[Usaco2010 Feb]Chocolate Giving 题意: n点m边无向图,有k头奶牛要送礼,它必须去农场(1号节点)拿礼物然后到目的地送.问每只奶牛的最短距离.n≤50000 题解: 以1号节点为源点spfa求一次最短路即可(反正是无向边). 代码: 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <queue> 5 #defi