USACO Apple Delivery
洛谷 P3003 [USACO10DEC]苹果交货Apple Delivery
JDOJ 2717: USACO 2010 Dec Silver 1.Apple Delivery
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模板(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的更多相关文章
- BZOJ 2100: [Usaco2010 Dec]Apple Delivery( 最短路 )
跑两遍最短路就好了.. 话说这翻译2333 ---------------------------------------------------------------------- #includ ...
- 洛谷P3003 [USACO10DEC]苹果交货Apple Delivery
P3003 [USACO10DEC]苹果交货Apple Delivery 题目描述 Bessie has two crisp red apples to deliver to two of her f ...
- 洛谷——P3003 [USACO10DEC]苹果交货Apple Delivery
P3003 [USACO10DEC]苹果交货Apple Delivery 这题没什么可说的,跑两遍单源最短路就好了 $Spfa$过不了,要使用堆优化的$dijkstra$ 细节:1.必须使用优先队列+ ...
- 洛谷 P3003 [USACO10DEC]苹果交货Apple Delivery
洛谷 P3003 [USACO10DEC]苹果交货Apple Delivery 题目描述 Bessie has two crisp red apples to deliver to two of he ...
- bzoj2100 [Usaco2010 Dec]Apple Delivery
Description Bessie has two crisp red apples to deliver to two of her friends in the herd. Of course, ...
- P3003 [USACO10DEC]苹果交货Apple Delivery
题目描述 Bessie has two crisp red apples to deliver to two of her friends in the herd. Of course, she tr ...
- 【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 ...
- 【BZOJ】2100: [Usaco2010 Dec]Apple Delivery(spfa+优化)
http://www.lydsy.com/JudgeOnline/problem.php?id=2100 这题我要吐血啊 我交了不下10次tle.. 噗 果然是写挫了. 一开始没加spfa优化果断t ...
- Dijkstra【p3003(bzoj2100)】[USACO10DEC]苹果交货Apple Delivery
Description 贝西有两个又香又脆的红苹果要送给她的两个朋友.当然她可以走的C(1<=C<=200000)条"牛路"都被包含在一种常用的图中,包含了P(1< ...
随机推荐
- 题解 P2719 【搞笑世界杯】
其实懂了之后很简单,但是刚开始真的很难想.. d[a][b]表示剩a张A类票和b张B类票时,最后两张票相同的概率 那么此时的排队的第一个人只有两种选择 拿A类票或者B类票 抛硬币得到的可能性当然是二分 ...
- [LOJ 2134][UOJ 132][BZOJ 4200][NOI 2015]小园丁与老司机
[LOJ 2134][UOJ 132][BZOJ 4200][NOI 2015]小园丁与老司机 题意 给定平面上的 \(n\) 个整点 \((x_i,y_i)\), 一共有两个问题. 第一个问题是从原 ...
- 阻止iOS Web APP中点击链接跳转到Safari 浏览器新标签页
问题:ios封装完之后,点击里边的按钮会跳转到网页上 ——小卡遇到这个问题就是这样解决的↓↓↓ 解决方法:建议将代码放到</head>标签前,当然,另外存为一个js 文件引用也是可以的呦~ ...
- FineUIPro v6.0.0 大版本更新,快来围观!
本月末(2019-09-20),我们会发布激动人心的 FineUI v6.0.0 版本,这个版本将带来一系列的重要更新! 在列举新版本特性之前,我们先来回顾下每次发布大版本的关键时间点: v1.0.0 ...
- 爬虫——控制台抓包和requests.post()发送请求
控制台抓包 打开方式及常用选项 1.打开浏览器,F12打开控制台,找到Network选项卡 2.控制台常用选项 1.Network: 抓取网络数据包 1.ALL: 抓取所有的网络数据包 2.XHR:抓 ...
- jdk自带监控程序jvisualvm的使用
监控小程序的配置 生产环境tomcat的配置 编辑应用所在的tomcat服务器下的bin目录下的catalina.sh文件,修改如下: 配置如下内容: export JAVA_OPTS="- ...
- geth 基本使用
概要 geth 是以太坊的官方 golang 客户端. 通过 geth 的使用可以直观的了解以太坊, 乃至区块链的运作. 下面, 通过 geth 来构造一次搭建私链, 创建账户, 挖矿, 交易的流程. ...
- 软件----- idea 配置创建一个简单javase项目
1.显示工具栏和工具按钮,勾选上 如图,在左侧会增加对应的 2.设置项目结构,选择jdk 点击new 选择需要jdk 3.创建一个简单的java文件,和eclipse与myeslipse 差不多, ...
- 搜索旋转排序数组II
题目 假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [,,,,,,] 可能变为 [,,,,,,] ). 编写一个函数来判断给定的目标值是否存在于数组中.若存在返回 true, ...
- 解决 Windows Server 2008 R2 上 Windows Update 无法失败,提示 8024402F
1.同步服务器时间 2.打开 services.msc,停止 Windows Update Service 3.手动下载 KB3138615 补丁:https://support.microsoft. ...