【[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,但是奶牛A必须先到FJ(居住在编号1的农场)那里取礼物,然后再送给奶牛B。

你的任务是:奶牛A至少需要走多远的路程?

输入

第1行:三个整数:N,M,B。

第2..M+1行:每行三个整数:R_i,S_i和L_i,描述一条边的信息。

第M+2..M+B+1行:共B行,每行两个整数P_i和Q_i,表示住在P_i农场的奶牛送礼物给住在Q_i农场的奶牛。

输出

共B行,每行一个整数,表示住在P_i农场的奶牛送礼给住在Q_i农场的奶牛至少需要走的路程

最短路超级大水题,跑一遍SPFA,奶牛要走的路径两点到起点最短路长度和。

 1 #include <cstdio>
 2 #include <queue>
 3
 4 struct edge{
 5     int to,dist;
 6 };
 7
 8 int n,m,b,r,s,l,p,q,dis[50001];
 9 bool vis[50001];
10 std::vector<edge> g[50001];
11 std::queue<int> que;
12
13 void spfa(void){
14     for(int i=2;i<=n;++i)
15         dis[i]=0x7fffffff;
16     que.push(1);
17     while(!que.empty()){
18         int u=que.front();
19         que.pop();
20         vis[u]=false;
21         for(int i=0;i<g[u].size();++i){
22             int v=g[u][i].to;
23             if(dis[u]+g[u][i].dist<dis[v]){
24                 dis[v]=dis[u]+g[u][i].dist;
25                 if(!vis[v]){
26                     vis[v]=true;
27                     que.push(v);
28                 }
29             }
30         }
31     }
32 }
33
34 int main(void){
35     scanf("%d%d%d",&n,&m,&b);
36     for(int i=1;i<=m;++i){
37         scanf("%d%d%d",&r,&s,&l);
38         g[r].push_back((edge){s,l});
39         g[s].push_back((edge){r,l});
40     }
41     spfa();
42     for(int i=1;i<=b;++i){
43         scanf("%d%d",&p,&q);
44         printf("%d\n",dis[p]+dis[q]);
45     }
46 }

原文地址:https://www.cnblogs.com/gzh01/p/9385267.html

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

【[USACO 2010 FEB】给巧克力Chocolate Giving(最短路)的相关文章

BZOJ 2015: [Usaco2010 Feb]Chocolate Giving( 最短路 )

裸最短路.. ------------------------------------------------------------------------------------ #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<queue> #define rep( i , n ) for( int i = 0 ; i <

洛谷 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 <=

洛谷——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

【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

[ USACO 2010 FEB ] Slowing Down

\(\\\) \(Description\) 给出一棵 \(N\) 个点的树和 \(N\) 头牛,每头牛都要去往一个节点,且每头牛去往的点一定互不相同. 现在按顺序让每一头牛去往自己要去的节点,定义一头牛的代价为,路径上经过的已经有牛的节点数,求每一头牛的代价. \(N\le 10^5\) \(\\\) \(Solution\) 注意到一个节点的影响只是在其子树内,考虑 \(DFS\) 的时候直接统计答案. 在每个节点上记录到这个点的牛的编号.那么对当前点有影响的点满足: 在根到这个点的路径上\

[Usaco2010 Feb]Chocolate Giving 最短路dijkstra+堆优化

本人水平有限,题解不到为处,请多多谅解 本蒟蒻谢谢大家观看 题目:传送门 最短路板子题:迪杰斯特拉+堆优化 注意:因为我建的是大根堆,所以要将距离取负,再存入大根堆堆中,这样队首就是最小值 直接套模板即可 code: 1 #include<bits/stdc++.h> 2 #define inf 0x3f3f3f3f 3 #pragma GCC optimize(3) 4 const int N=50005; 5 const int M=200002; 6 using namespace st

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

BZOJ 2015 Usaco2010 Feb Chocolate Giving

2015: [Usaco2010 Feb]Chocolate Giving Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 533  Solved: 348[Submit][Status][Discuss] 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