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:  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肯定会使总路程最小,输出最小值。

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

一道坑爹的最短路……意思是从s出发,要求走过t1、t2两个点的最短路

做法不难想,分别以t1、t2为起点跑最短路,然后min(dis1[s]+dis1[t2],dis2[s]+dis2[t1])即是所求

DIj+堆就不讲了,不加slf优化的spfa会超时

我发现我对slf的理解好像是错的,因为我一直以为队头就是当前处理的那个点,实际上从队头提出来之后就要出队了,这时的队头应该是当前这个点的下一个点

#include<cstdio>
#include<cstring>
#define N 100010
#define M 200010
#define mod 100001
using namespace std;
const int inf=0x7fffffff/11.27;
struct edge{
int to,next,v;
}e[4*M];
int n,m,s,t1,t2,cnt,ans,t,w;
int head[N],dis1[N],dis2[N],q[3*N];
bool mrk[N];
inline int min(int a,int b)
{return a<b?a:b;}
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
inline void ins(int u,int v,int w)
{
e[++cnt].to=v;
e[cnt].v=w;
e[cnt].next=head[u];
head[u]=cnt;
}
inline void insert(int u,int v,int w)
{
ins(u,v,w);
ins(v,u,w);
}
inline void spfa(int S,int *dist)
{
for (int i=1;i<=n;i++)mrk[i]=0;
for (int i=1;i<=n;i++)dist[i]=inf;
memset(q,0,sizeof(q));
q[0]=S;mrk[S]=1;dist[S]=0;
t=0;w=0;
do
{
int now=q[t];
t=(t+1)%mod;
for (int i=head[now];i;i=e[i].next)
if (dist[e[i].to]>dist[now]+e[i].v)
{
dist[e[i].to]=dist[now]+e[i].v;
if (!mrk[e[i].to])
{
mrk[e[i].to]=1;
if (dist[q[t]]>dist[e[i].to])
{
t=(t-1+mod)%mod;
q[t]=e[i].to;
}
else
{
w=(w+1)%mod;
q[w]=e[i].to;
}
}
}
mrk[now]=0;
}
while (t!=w);
}
int main()
{
m=read();n=read();s=read();t1=read();t2=read();
int x,y,z;
for (int i=1;i<=m;i++)
{
x=read();y=read();z=read();
insert(x,y,z);
}
spfa(t1,dis1);
spfa(t2,dis2);
ans=min(dis1[s]+dis1[t2],dis2[s]+dis2[t1]);
printf("%d",ans);
}

bzoj2100 [Usaco2010 Dec]Apple Delivery的更多相关文章

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

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

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

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

  3. 【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 ...

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

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

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

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

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

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

  7. USACO Apple Delivery

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

  8. BZOJ2097[Usaco2010 Dec] 奶牛健美操

    我猜我这样继续做水题会狗带 和模拟赛的题很像,贪心搞一下. #include<bits/stdc++.h> using namespace std; int read(){ ,f=;cha ...

  9. BZOJ2101: [Usaco2010 Dec]Treasure Chest 藏宝箱

    2101: [Usaco2010 Dec]Treasure Chest 藏宝箱 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 327  Solved:  ...

随机推荐

  1. 《Algorithms 4th Edition》读书笔记——2.4 优先队列(priority queue)-Ⅱ

    2.4.2初级实现 我们知道,基础数据结构是实现优先队列的起点.我们可以是使用有序或无序的数组或链表.在队列较小时,大量使用两种主要操作之一时,或是所操作元素的顺序已知时,它们十分有用.因为这些实现相 ...

  2. [置顶] 【其他部分 第一章 矩阵】The C Programming Language 程序研究 【持续更新】

    其他部分 第一章 矩阵 一.矩阵的转置   问题描述: 编写函数,把给定的任意一个二维整型矩阵转换为其转置矩阵. 输入: 1 2 3 4 5 6 输出: 1 4 2 5 3 6 分析 题目要求编写一个 ...

  3. linux 虚拟机设置IP访问外网

    1 设置网络为桥接模式:(Vmware为例,安装过程中也可以设置) 选中当前的操作系统,点击虚拟机-->设置-->硬件-->网络设备器,勾选桥接模式 2 修改网络配置在命令行界面输入 ...

  4. 表单验证插件jquery.validate的使用方法演示

    jQueryValidate表单验证效果 jquery.validate验证错误信息的样式控制 <!--validate验证插件的基础样式--> input.error{border: 1 ...

  5. Uninstall or Disable Java on a Mac

    You can run Java apps in two ways. The first is to run Java applets inside your Web browser with a p ...

  6. JS 数组扩展函数--求起始项到终止项和

    Array.prototype.sum= function(l,r){ l=l==undefined ? 0 : l; r=r==undefined ? this.length - 1 : r; va ...

  7. linux用户创建删除以及文件权限查看修改

    一. 1.查看用户 命令如下:whoami 2.创建用户 创建用户命令:sudo adduser hello 超级用户是 root 删除用户名命令:sudo deluser hello --remov ...

  8. SQL Server 存储过程之基础知识(转)

    什么是存储过程呢?存储过程就是作为可执行对象存放在数据库中的一个或多个SQL命令. 通俗来讲:存储过程其实就是能完成一定操作的一组SQL语句. 那为什么要用存储过程呢?1.存储过程只在创造时进行编译, ...

  9. SQL Server根据列名查表

    select a.name, b.name from syscolumns a, sysobjects b where a.name = 'XXXX' and a.id = b.id and b.xt ...

  10. mvc原理和mvc模式的优缺点

    一.mvc原理   mvc是一种程序开发设计模式,它实现了显示模块与功能模块的分离.提高了程序的可维护性.可移植性.可扩展性与可重用性,降低了程序的开发难度.它主要分模型.视图.控制器三层. 1.模型 ...