bzoj2100 [Usaco2010 Dec]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: 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肯定会使总路程最小,输出最小值。
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
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
一道坑爹的最短路……意思是从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的更多相关文章
- bzoj2100 [Usaco2010 DEC]Apple Delivery苹果贸易
题目描述 一张P个点的无向图,C条正权路.CLJ要从Pb点(家)出发,既要去Pa1点NOI赛场拿金牌,也要去Pa2点CMO赛场拿金牌.(途中不必回家)可以先去NOI,也可以先去CMO.当然神犇CLJ肯 ...
- BZOJ 2100: [Usaco2010 Dec]Apple Delivery( 最短路 )
跑两遍最短路就好了.. 话说这翻译2333 ---------------------------------------------------------------------- #includ ...
- 【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 ...
- bzoj 2100: [Usaco2010 Dec]Apple Delivery【spfa】
洛谷数据好强啊,普通spfa开o2都过不了,要加双端队列优化 因为是双向边,所以dis(u,v)=dis(v,u),所以分别以pa1和pa2为起点spfa一遍,表示pb-->pa1-->p ...
- BZOJ 2100: [Usaco2010 Dec]Apple Delivery spfa
由于是无向图,所以可以枚举两个终点,跑两次最短路来更新答案. #include <queue> #include <cstdio> #include <cstring&g ...
- USACO Apple Delivery
洛谷 P3003 [USACO10DEC]苹果交货Apple Delivery 洛谷传送门 JDOJ 2717: USACO 2010 Dec Silver 1.Apple Delivery JDOJ ...
- BZOJ2097[Usaco2010 Dec] 奶牛健美操
我猜我这样继续做水题会狗带 和模拟赛的题很像,贪心搞一下. #include<bits/stdc++.h> using namespace std; int read(){ ,f=;cha ...
- BZOJ2101: [Usaco2010 Dec]Treasure Chest 藏宝箱
2101: [Usaco2010 Dec]Treasure Chest 藏宝箱 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 327 Solved: ...
随机推荐
- php引用计数与变量引用
每个php5.5变量都存储在一个叫做zval的变量容器中. 一个zval变量容器,除了包含变量的类型与值外,还包含两个字节的额外信息: 1.第一个是“is_ref”,是个bool型,用来标识这个变量是 ...
- mook_百度百科
mook_百度百科 mook
- python高级编程之选择好名称:完2
# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' #分解代码 #小就是美,这也适用所有级别的代码,当一个函数,类或者一 ...
- 检测iOS系统的定位服务
[CLLocationManager locationServicesEnabled]检测的是整个iOS系统的位置服务开关
- 关于NetBeans IDE的配置优化
首先,IDE的版本最好对应着JDK的版本. NetBeans优化的目的是提高NetBeans的启动速度和运行速度.下面介绍的NetBeans优化技巧是在版本6.0beta2上的优化.经过实验,大大提高 ...
- yii使用寻呼功能
CDbCriteria这是类包使用,包是yii自带专门用来处理类似分类这种功能的. 而我们使用yii框架然后调用这种方法会起到事半功倍的效果,会发现使用这个可以节省非常多的时间.让你高速的使用PHP中 ...
- Servlet实现Session
(1)首先看一下项目的结构 是在tomcat--webaps下的myWebSites项目 在myWebSites下有仅仅有WEB-INF目录 在WEB-INF目录中有 一下目录(在classes目录 ...
- BOOST 线程完全攻略 - 扩展 - 事务线程
扩展threadtimermoduleexceptionsocket 什么叫事务线程 举个例子: 我们写一个IM客户端的登录子线程,则该子线程会有这么几个事务要处理 No.1 TCP Socket物理 ...
- ubuntu apache2配置详解(含虚拟主机配置方法)
ubuntu apache2配置详解(含虚拟主机配置方法) 在Windows下,Apache的配置文件通常只有一个,就是httpd.conf.但我在Ubuntu Linux上用apt-get inst ...
- CSS基础知识笔记(四)
元素分类 标签元素大体被分为三种不同的类型:块状元素.内联元素(又叫行内元素)和内联块状元素. 常用的块状元素有: <div>.<p>.<h1>...<h6& ...