【bzoj2100】[Usaco2010 Dec]Apple Delivery 最短路
题目描述
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:
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.
CLJ要从Pb点(家)出发,既要去Pa1点NOI赛场拿金牌,也要去Pa2点CMO赛场拿金牌。(途中不必回家)
可以先去NOI,也可以先去CMO。
当然神犇CLJ肯定会使总路程最小,输出最小值。
输入
* 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
输出
* Line 1: The shortest distance Bessie must travel to deliver both apples
样例输入
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
样例输出
12
题解
最短路
分先到pa1和先到pa2两种情况作讨论。
由于是无向图,所以可以以pa1和pa2分别为起点跑Spfa(当然需要双头队列优化)
然后取一下min即可。
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
deque<int> q;
int head[100010] , to[400010] , len[400010] , next[400010] , cnt , dis[100010][2] , inq[100010] , n;
inline int read()
{
int ret = 0; char ch = getchar();
while(ch < '0' || ch > '9') ch = getchar();
while(ch >= '0' && ch <= '9') ret = (ret << 3) + (ret << 1) + ch - '0' , ch = getchar();
return ret;
}
void add(int x , int y , int z)
{
to[++cnt] = y;
len[cnt] = z;
next[cnt] = head[x];
head[x] = cnt;
}
void spfa(int s , int p)
{
int i , x;
for(i = 1 ; i <= n ; i ++ )
dis[i][p] = 0x3fffffff;
dis[s][p] = 0;
q.push_front(s);
while(!q.empty())
{
x = q.front();
q.pop_front();
inq[x] = 0;
for(i = head[x] ; i ; i = next[i])
{
if(dis[to[i]][p] > dis[x][p] + len[i])
{
dis[to[i]][p] = dis[x][p] + len[i];
if(!inq[to[i]])
{
inq[to[i]] = 1;
int t;
if(!q.empty()) t = q.front();
if(!q.empty() && dis[to[i]][p] < dis[t][p]) q.push_front(to[i]);
else q.push_back(to[i]);
}
}
}
}
}
int main()
{
int m , s , t1 , t2 , i , x , y , z;
m = read() , n = read() , s = read() , t1 = read() , t2 = read();
for(i = 1 ; i <= m ; i ++ )
x = read() , y = read() , z = read() , add(x , y , z) , add(y , x , z);
spfa(t1 , 0);
spfa(t2 , 1);
printf("%d\n" , min(dis[s][0] + dis[t2][0] , dis[s][1] + dis[t1][1]));
return 0;
}
【bzoj2100】[Usaco2010 Dec]Apple Delivery 最短路的更多相关文章
- BZOJ 2100: [Usaco2010 Dec]Apple Delivery( 最短路 )
跑两遍最短路就好了.. 话说这翻译2333 ---------------------------------------------------------------------- #includ ...
- bzoj2100 [Usaco2010 Dec]Apple Delivery
Description Bessie has two crisp red apples to deliver to two of her friends in the herd. Of course, ...
- bzoj2100 [Usaco2010 DEC]Apple Delivery苹果贸易
题目描述 一张P个点的无向图,C条正权路.CLJ要从Pb点(家)出发,既要去Pa1点NOI赛场拿金牌,也要去Pa2点CMO赛场拿金牌.(途中不必回家)可以先去NOI,也可以先去CMO.当然神犇CLJ肯 ...
- BZOJ 2100: [Usaco2010 Dec]Apple Delivery spfa
由于是无向图,所以可以枚举两个终点,跑两次最短路来更新答案. #include <queue> #include <cstdio> #include <cstring&g ...
- 【BZOJ】2100: [Usaco2010 Dec]Apple Delivery(spfa+优化)
http://www.lydsy.com/JudgeOnline/problem.php?id=2100 这题我要吐血啊 我交了不下10次tle.. 噗 果然是写挫了. 一开始没加spfa优化果断t ...
- bzoj 2100: [Usaco2010 Dec]Apple Delivery【spfa】
洛谷数据好强啊,普通spfa开o2都过不了,要加双端队列优化 因为是双向边,所以dis(u,v)=dis(v,u),所以分别以pa1和pa2为起点spfa一遍,表示pb-->pa1-->p ...
- 洛谷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.必须使用优先队列+ ...
- USACO Apple Delivery
洛谷 P3003 [USACO10DEC]苹果交货Apple Delivery 洛谷传送门 JDOJ 2717: USACO 2010 Dec Silver 1.Apple Delivery JDOJ ...
随机推荐
- Java设计模式(21)——行为模式之备忘录模式(Memento)
一.概述 概念 UML简图 角色 根据下图得到角色 备忘录角色(Memento).发起人角色(Originator).负责人角色(Caretaker) 二.实践 使用白箱实现,给出角色的代码: 发起人 ...
- 成都Uber优步司机奖励政策(3月26日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 成都Uber优步司机奖励政策(1月29日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- day 6 老王开枪打人
1.图示 2 程序 1)版本1:框架的搭建 def main(): '''用来控制这个程序的流程''' pass #1.创建alex对象 #2.创建1个枪对象 #3.创建1个弹夹对象 #4.创建子弹对 ...
- 学习Drupal一个容易被忽视的问题
刚刚修复了一个问题,一个非常小的问题,但我花了2-3小时才查明原因并修复. 总结下来我忽视了一个非常常见的问题或者没有养成一个好的习惯. 问题现象是:论坛发帖,只有editor以上权限的人可以发帖,也 ...
- 通过反编译看Java String及intern内幕--费元星站长
通过反编译看Java String及intern内幕 一.字符串问题 字符串在我们平时的编码工作中其实用的非常多,并且用起来也比较简单,所以很少有人对其做特别深入的研究.倒是面试或者笔试的时候,往 ...
- css 网站常用
简单的loading效果 .progressBar { border: solid 1px #303031; font: bold 20px/22px Arial, sans-serif; backg ...
- 用PowerDesign反向生成数据库Sql语句问题
在用Pd15反向生成数据库时,生成的Sql语句在Sql Server Manager Studio里面报错,根本就执行不了.数据库用的是Sql Server 2008 R2.经过一番修 ...
- records_in_range start_key, end_key
select * from federatedTest where name='aaa';(gdb) p *start_key$2 = {key = 0x7f64f4103be8 "&quo ...
- 百度地图标注及结合ECharts图谱数据可视化
本示例中根据企业位置经纬度,在页面右侧百度地图中标注企业名称.同时页面左侧ECharts图谱饼状图用于统计企业行业与注册资本.当右侧百度地图缩放拖拽,左侧ECharts图谱根据右侧地图上出现的企业动态 ...