题目描述

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.

贝西有两个又香又脆的红苹果要送给她的两个朋友。当然她可以走的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各不相同。

输入输出格式

输入格式:

  • 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

输入输出样例

输入样例#1:

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
输出样例#1:

 /*这道题很好做,直接套slf优化的模板,跑两次取最小值,就好了,本人先开始把slf和lll优化一起加了进去,但是由于lll被常数卡爆了,卡到指数级去了,然后怎么优化都会T掉第二个点,如果用lll优化会T掉4个点(本人亲手实验),用slf可以A掉,而且很快。直接上代码吧。*/

 #include<algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <cstdio>
#include <string>
#include <cmath>
#include <stack>
#include <queue>
#include <deque>
using namespace std;
const int gg=;
const int INF=1e9;
int head[gg];
struct node
{
int next;
int w;
int to;
} a[gg];
int dis[gg];
bool vis[gg];
int c,p,pb,pa1,pa2,cnt;
int ans;
int ans2;
int sum,tot;
int rans;
inline void add(int i,int j,int w)
{
a[++cnt].to=j;
a[cnt].next=head[i];
a[cnt].w=w;
head[i]=cnt;
}
inline void spfa(int s)
{
deque<int>q;
memset(vis,false,sizeof(vis));
memset(dis,0x7f,sizeof(dis));
dis[s]=;
vis[s]=true;
q.push_back(s);
while(!q.empty())
{
int u=q.front();
q.pop_front();
vis[u]=false;
for(register int i=head[u]; i; i=a[i].next)
{
int v=a[i].to;
if(dis[v]>dis[u]+a[i].w)
{
dis[v]=dis[u]+a[i].w;
if(!vis[v])
{
vis[v]=true;
if(q.empty()||dis[v]>dis[q.front()])
{
q.push_back(v);
}
else
q.push_front(v);
}
}
}
}
}
int main()
{
scanf("%d%d%d%d%d",&c,&p,&pb,&pa1,&pa2);
for(register int i=; i<=c; i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);
add(y,x,z);
}
spfa(pa1);
ans+=dis[pa2]+dis[pb];
spfa(pa2);
ans2+=dis[pa1]+dis[pb];
rans=min(ans,ans2);
printf("%d\n",rans);
return ;
}
12 

P3003 [USACO10DEC]苹果交货Apple Delivery的更多相关文章

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

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

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

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

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

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

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

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

  5. luoguP3003 [USACO10DEC]苹果交货Apple Delivery

    LOL新英雄卡莎点击就送 一句话题意: 三个点a1,a2,b,求从b到a1和a2的最短路 做法:求出a1->b和a2->b的最短路,两者取min,之后再加上a1->a2的最短路 为啥 ...

  6. 洛谷P3003 苹果交货Apple Delivery

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

  7. USACO Apple Delivery

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

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

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

  9. iOS - 苹果官方Apple Pay开发文档(中文版)- Apple Pay(1)

    翻译自苹果官方Apple Pay开发文档.目前版本为1.0 概览: Apple Pay为用户从你的App里购买实际的物品和服务提供简单而安全的方法.通过Touch ID,用户可使用储存在iPhone ...

随机推荐

  1. bzoj1036 [ZJOI2008]树的统计

    一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u t : 把结点u的权值改为t II. QMAX u v: 询问从 ...

  2. http://codeforces.com/contest/610/problem/D

    D. Vika and Segments time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  3. c#中常量、ReadOnly和Static ReadOnly的差异

    不定时更新翻译系列,此系列更新毫无时间规律,文笔菜翻译菜求各位看官老爷们轻喷,如觉得我翻译有问题请挪步原博客地址 本博文翻译自: http://www.arungudelli.com/tutorial ...

  4. dotweb框架之旅 [二] - 常用对象-App(dotweb)

    dotweb属于一个Web框架,希望通过框架行为,帮助开发人员快速构建Web应用,提升开发效率,减少不必要的代码臃肿. dotweb包含以下几个常用对象: App(dotweb) App容器,为Web ...

  5. 谦先生的程序员日志之我的hadoop大数据生涯一

    从一个初级程序员到高级程序员的经历 你好!我是谦先生,我是茫茫程序猿中的一猿,平凡又执着. 刚入行的时候说实话,啥都不懂,就懂点皮毛的java,各种被虐狗的感觉.又写js又写css又写后台...慢慢被 ...

  6. JAVA继承:编译与运行的关系(编译看左边,运行看右边)

    "成员变量,静态方法看左边:非静态方法:编译看左边,运行看右边." 意思是:当父类变量引用子类对象时(Fu f = new Zi();),在这个引用变量f指向的对象中,他的成员变量 ...

  7. Html 初识样式表&选择器

    样式表&类选择器分类 样式表分类: 1.内联式样式表:在标签内部写样式代码,精确但不方便,增加工作量,后期修改麻烦. 2.嵌入式样式表:一般写在head内 以<style>.... ...

  8. 基于HTML5和WebGL的碰撞测试

    这是公司大神写的一个放官网上给用户学习的例子,我一开始真的不知道这是在干嘛,就只是将三个形状图元组合在一起,然后可以同时旋转.放大缩小这个三个图形,点击"Animate"就能让中间 ...

  9. win10 edge扩展

    安装红石预览版 https://dev.windows.com/zh-cn/microsoft-edge/extensions/#available-extensions 微软翻译 鼠标手势

  10. socket__服务端于客户端

    #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2017/8/23 15:33 # @Author : Mr_zhang # @Site ...