洛谷 P3003 [USACO10DEC]苹果交货Apple Delivery

洛谷传送门

JDOJ 2717: USACO 2010 Dec Silver 1.Apple Delivery

JDOJ传送门

Description

Bessie has two crisp red apples to deliver to two of her friends

in the herd. Of course, she travels the C (1 <= C <= 200,000)

cowpaths which are arranged as the usual graph which connects P (1

<= P <= 100,000) pastures conveniently numbered from 1..P: no cowpath

leads from a pasture to itself, cowpaths are bidirectional, each

cowpath has an associated distance, and, best of all, it is always

possible to get from any pasture to any other pasture. Each cowpath

connects two differing pastures P1_i (1 <= P1_i <= P) and P2_i (1

<= P2_i <= P) with a distance between them of D_i. The sum of all

the distances D_i does not exceed 2,000,000,000.

What is the minimum total distance Bessie must travel to deliver

both apples by starting at pasture PB (1 <= PB <= P) and visiting

pastures PA1 (1 <= PA1 <= P) and PA2 (1 <= PA2 <= P) in any order.

All three of these pastures are distinct, of course.

Consider this map of bracketed pasture numbers and cowpaths with

distances:

                3        2       2
[1]-----[2]------[3]-----[4]
\ / \ /
7\ /4 \3 /2
\ / \ /
[5]-----[6]------[7]
1 2

If Bessie starts at pasture [5] and delivers apples to pastures [1]

and [4], her best path is:

      5 -> 6-> 7 -> 4* -> 3 -> 2 -> 1*

with a total distance of 12.

Input

* Line 1: Line 1 contains five space-separated integers: C, P, PB,

PA1, and PA2

* Lines 2..C+1: Line i+1 describes cowpath i by naming two pastures it

connects and the distance between them: P1_i, P2_i, D_i

Output

* Line 1: The shortest distance Bessie must travel to deliver both

apples

Sample Input

9 7 5 1 4 5 1 7 6 7 2 4 7 2 5 6 1 5 2 4 4 3 2 1 2 3 3 2 2 2 6 3

Sample Output

12

题目翻译:

贝西有两个又香又脆的红苹果要送给她的两个朋友。当然她可以走的C(1<=C<=200000)条“牛路”都被包含在一种常用的图中,包含了P(1<=P<=100000)个牧场,分别被标为1..P。没有“牛路”会从一个牧场又走回它自己。“牛路”是双向的,每条牛路都会被标上一个距离。最重要的是,每个牧场都可以通向另一个牧场。每条牛路都连接着两个不同的牧场P1_i和P2_i(1<=P1_i,p2_i<=P),距离为D_i。所有“牛路”的距离之和不大于2000000000。

现在,贝西要从牧场PB开始给PA_1和PA_2牧场各送一个苹果(PA_1和PA_2顺序可以调换),那么最短的距离是多少呢?当然,PB、PA_1和PA_2各不相同。

题解:

这道题如果用裸的SPFA做会TLE,所以要加优化。

如果有像20分钟之前的我一样对SPFA算法优化一无所知的人,请移步我的上一篇博客:

优化SPFA基础知识传送门

但是光知道怎么优化是不够的,我们还要就这个题想一想怎么写。

首先,这个题的源点不再是一,所以SPFA的时候要传参数。

其次,最后统计答案的时候要比较一下。

最后,拍一遍SPFA模板(SLF LLL优化版都可以)(本蒟蒻比较喜欢SLF),AC。

注意双向边的问题。

CODE:

#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
int m,n,s,t1,t2;
int tot,to[400001],val[400001],nxt[400001],head[100001];
int dist[100001],v[100001];
void add(int x,int y,int z)
{
to[++tot]=y;
val[tot]=z;
nxt[tot]=head[x];
head[x]=tot;
}
void spfa(int start)
{
memset(v,0,sizeof(v));
memset(dist,0x3f,sizeof(dist));
deque<int> q;
dist[start]=0;
v[start]=1;
q.push_front(start);
while(!q.empty())
{
int x=q.front();
q.pop_front();
v[x]=0;
for(int i=head[x];i;i=nxt[i])
{
int y=to[i];
if(dist[x]+val[i]<dist[y])
{
dist[y]=dist[x]+val[i];
if(!v[y])
{
if(!q.empty() && dist[y]<dist[q.front()])
q.push_front(y);
else
q.push_back(y);
v[y]=1;
}
}
}
}
}
int main()
{
scanf("%d%d%d%d%d",&m,&n,&s,&t1,&t2);
for(int i=1;i<=m;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);
add(y,x,z);
}
int ans1=0;
int ans2=0;
spfa(t1);
ans1+=dist[t2]+dist[s];
spfa(t2);
ans2+=dist[t1]+dist[s];
int ans=min(ans1,ans2);
printf("%d",ans);
return 0;
}

USACO Apple Delivery的更多相关文章

  1. BZOJ 2100: [Usaco2010 Dec]Apple Delivery( 最短路 )

    跑两遍最短路就好了.. 话说这翻译2333 ---------------------------------------------------------------------- #includ ...

  2. 洛谷P3003 [USACO10DEC]苹果交货Apple Delivery

    P3003 [USACO10DEC]苹果交货Apple Delivery 题目描述 Bessie has two crisp red apples to deliver to two of her f ...

  3. 洛谷——P3003 [USACO10DEC]苹果交货Apple Delivery

    P3003 [USACO10DEC]苹果交货Apple Delivery 这题没什么可说的,跑两遍单源最短路就好了 $Spfa$过不了,要使用堆优化的$dijkstra$ 细节:1.必须使用优先队列+ ...

  4. 洛谷 P3003 [USACO10DEC]苹果交货Apple Delivery

    洛谷 P3003 [USACO10DEC]苹果交货Apple Delivery 题目描述 Bessie has two crisp red apples to deliver to two of he ...

  5. bzoj2100 [Usaco2010 Dec]Apple Delivery

    Description Bessie has two crisp red apples to deliver to two of her friends in the herd. Of course, ...

  6. P3003 [USACO10DEC]苹果交货Apple Delivery

    题目描述 Bessie has two crisp red apples to deliver to two of her friends in the herd. Of course, she tr ...

  7. 【bzoj2100】[Usaco2010 Dec]Apple Delivery 最短路

    题目描述 Bessie has two crisp red apples to deliver to two of her friends in the herd. Of course, she tr ...

  8. 【BZOJ】2100: [Usaco2010 Dec]Apple Delivery(spfa+优化)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2100 这题我要吐血啊 我交了不下10次tle.. 噗 果然是写挫了. 一开始没加spfa优化果断t ...

  9. Dijkstra【p3003(bzoj2100)】[USACO10DEC]苹果交货Apple Delivery

    Description 贝西有两个又香又脆的红苹果要送给她的两个朋友.当然她可以走的C(1<=C<=200000)条"牛路"都被包含在一种常用的图中,包含了P(1< ...

随机推荐

  1. wcf callback在主线程被调用

    记得当时往wcf迁移了部分service以后,tester发现有时候系统会没有任何反应,cpu占用倒是没有问题,可就是做不了事情. 感觉是哪里死锁了,但也不是每次都能再现,后来发现如果把callbac ...

  2. 用Node.js给邮箱发送邮件

    首先我们需要做的是下载发送邮件的包 cnpm install nodemailer --save 然后写发送邮件的代码,代码如下: 实现原理是:用你的邮箱给其他邮箱发送邮件,所以这里需要填写你的邮箱和 ...

  3. 【2019.7.25 NOIP模拟赛 T3】树(tree)(dfs序列上开线段树)

    没有换根操作 考虑如果没有换根操作,我们该怎么做. 我们可以求出原树的\(dfs\)序列,然后开线段树维护. 对于修改操作,我们可以倍增求\(LCA\),然后在线段树上修改子树内的值. 对于询问操作, ...

  4. MySQL实战45讲学习笔记:第三十七讲

    一.本节概况 今天是大年初二,在开始我们今天的学习之前,我要先和你道一声春节快乐! 在第 16和第 34篇文章中,我分别和你介绍了 sort buffer.内存临时表和 join buffer.这三个 ...

  5. 运维相关指标数据采集并ES入仓 - 运维笔记

    为了进行数字化IT治理,需要对一些应用进程相关指标进行采集并入库.收集到的应用指标数据最好要进行ES入仓,入到Kafka里面,并通过Kibana可视化展示. 需要进行采集的应用进程相关指标如下: ES ...

  6. Vue.js 源码分析(二十六) 高级应用 作用域插槽 详解

    普通的插槽里面的数据是在父组件里定义的,而作用域插槽里的数据是在子组件定义的. 有时候作用域插槽很有用,比如使用Element-ui表格自定义模板时就用到了作用域插槽,Element-ui定义了每个单 ...

  7. 三维网格精简算法(Quadric Error Metrics)附源码(转载)

    转载:  https://www.cnblogs.com/shushen/p/5311828.html 在计算机图形应用中,为了尽可能真实呈现虚拟物体,往往需要高精度的三维模型.然而,模型的复杂性直接 ...

  8. GAC 解释&路径

    GAC 全称是 Global Assembly Cache 作用是可以存放一些有很多程序都要用到的公共 Assembly ,例如 System.Data .System.Windows.Form 等等 ...

  9. 关于 L3 缓存行 cacheLIne 的研究!还是对程序有举足轻重的作用!

    https://www.cnblogs.com/PurpleTide/archive/2010/11/25/1887506.html CLR via C# 读书笔记 2-3 Cache Lines a ...

  10. Asp.Net SignalR 使用记录

    工作上遇到一个推送消息的功能的实现.本着面向百度编程的思想.网上百度了一大堆.主要的实现方式是原生的WebSocket,和SignalR,再次写一个关于Asp.Net SignalR 的demo 这里 ...