题目描述

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.

一张P个点的无向图,C条正权路。
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 最短路的更多相关文章

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

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

  2. bzoj2100 [Usaco2010 Dec]Apple Delivery

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

  3. bzoj2100 [Usaco2010 DEC]Apple Delivery苹果贸易

    题目描述 一张P个点的无向图,C条正权路.CLJ要从Pb点(家)出发,既要去Pa1点NOI赛场拿金牌,也要去Pa2点CMO赛场拿金牌.(途中不必回家)可以先去NOI,也可以先去CMO.当然神犇CLJ肯 ...

  4. BZOJ 2100: [Usaco2010 Dec]Apple Delivery spfa

    由于是无向图,所以可以枚举两个终点,跑两次最短路来更新答案. #include <queue> #include <cstdio> #include <cstring&g ...

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

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

  6. bzoj 2100: [Usaco2010 Dec]Apple Delivery【spfa】

    洛谷数据好强啊,普通spfa开o2都过不了,要加双端队列优化 因为是双向边,所以dis(u,v)=dis(v,u),所以分别以pa1和pa2为起点spfa一遍,表示pb-->pa1-->p ...

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

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

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

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

  9. USACO Apple Delivery

    洛谷 P3003 [USACO10DEC]苹果交货Apple Delivery 洛谷传送门 JDOJ 2717: USACO 2010 Dec Silver 1.Apple Delivery JDOJ ...

随机推荐

  1. linux怎么把英文版火狐浏览器改成中文

    一.按alt打开菜单栏,点击help>about firefox查看自己的火狐浏览器版本 比如我的是52.4.0版本 二打开网址 http://ftp.mozilla.org/pub/firef ...

  2. 20145234黄斐《Java程序设计》第二周学习总结

    教材学习内容总结 类型 Java可区分为基本类型(Primitive Type)和类类型(Class Type),其中类类型也叫参考类型(Reference Type). 字节类型,也叫byte类型, ...

  3. ORB-SLAM(五)KeyFrameDataBase类

    关键帧数据库通过预先训练好的词典,维护一个向量std::vector<list<KeyFrame*> > mvInvertedFile; 该向量中mvInvertedFile[ ...

  4. Kubernetes 在网易云中的落地优化实践

    本文来自网易云社区 今天我跟大家讲的是 Kubernetes 在网易的一些实践,目的是抛砖引玉,看看大家在这个方向有没有更好的实践方法.简单介绍一下网易云.网易云是从最早 Kubernetes 1.0 ...

  5. spark 执行架构

    术语定义 Application:Spark Application的概念和Hadoop MapReduce中的类似,指的是用户编写的Spark应用程序,包含了一个Driver 功能的代码和分布在集群 ...

  6. Putty远程连接Ubuntu14.04

    步骤一.在ubuntu系统中安装ssh,可使用如下的命令进行安装: sudo apt-get install openssh-server 步骤二.为了保险起见,安装完成后重启一下ssh服务,命令如下 ...

  7. Python类对象

    python类对象 python类对象支持两种操作:属性引用和实例化. 属性引用 使用 Python 中所有属性引用所使用的标准语法: obj.name. 有效的属性名称是类对象被创建时存在于类命名空 ...

  8. 在几份docx文档中里查找某个值

    import docx, os def readDocx(fileName): doc = docx.Document(fileName) content = '\n'.join([para.text ...

  9. 《Effective C++》读书笔记 资源管理

    C++程序中最常用的资源包括动态分配的内存,文件描述器,互斥锁,数据库连接,网络socket等等.不论哪种资源,重要的是,当你不再使用他时,必须将他归还给系统. 一个很好的做法是以对象管理资源.把资源 ...

  10. ionic 获取input的值

    1.参数传递法 例子:获取input框内容 这里有个独特的地方,直接在input处使用 #定义参数的name值,注意在ts中参数的类型 在html页面中 <ion-input type=&quo ...