bzoj2100 [Usaco2010 DEC]Apple Delivery苹果贸易
题目描述
一张P个点的无向图,C条正权路。CLJ要从Pb点(家)出发,既要去Pa1点NOI赛场拿金牌,也要去Pa2点CMO赛场拿金牌。(途中不必回家)可以先去NOI,也可以先去CMO。当然神犇CLJ肯定会使总路程最小,输出最小值。
题解:做两遍spfa,找出从起点开始先去pa1或者先去pa2的最小值
需要用一下spfa的优化,每次进行入队的时候都与队头进行比较,如果比队头小就放在队头,否则放队尾。
#include<bits/stdc++.h>
using namespace std;
#define maxn 100005
#define maxm 200005
int dis[maxn],head[maxn],q[maxn];
bool vis[maxn];
struct edge{
int next,to,w;
}e[maxm*];
int n,m,s,t1,t2;
int ans=,cnt;
inline int read(){
int x=,f=;char ch=getchar();
while(ch>''||ch<''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
void insert(int u,int v,int w){
cnt++;
e[cnt].next=head[u];e[cnt].to=v;e[cnt].w=w;
head[u]=cnt;
}
void spfa(int x){
memset(dis,,sizeof dis);
vis[x]=;dis[x]=;q[]=x;
int top=,tail=;
while(top!=tail)
{
int now=q[top];top++;
if(top==)top=;
for(int i=head[now];i;i=e[i].next){
int p=e[i].to;
if(dis[p]>dis[now]+e[i].w){
dis[p]=dis[now]+e[i].w;
if(!vis[p]){
vis[p]=;
if(dis[p]<dis[q[top]]){
top--;if(top<)top=;
q[top]=p;
}
else{
q[tail++]=p;
if(tail==)tail=;
}
}
}
}
vis[now]=;
}
}
int main(){
m=read();n=read();s=read();t1=read();t2=read();
int u,v,w;
for(int i=;i<=m;i++){
u=read();v=read();w=read();
insert(u,v,w);insert(v,u,w);
}
spfa(t1);
ans=dis[s]+dis[t2];
spfa(t2);
ans=min(ans,dis[s]+dis[t1]);
printf("%d",ans);
}
bzoj2100 [Usaco2010 DEC]Apple Delivery苹果贸易的更多相关文章
- bzoj2100 [Usaco2010 Dec]Apple Delivery
Description Bessie has two crisp red apples to deliver to two of her friends in the herd. Of course, ...
- 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 ...
- 洛谷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.必须使用优先队列+ ...
- 洛谷 P3003 [USACO10DEC]苹果交货Apple Delivery
洛谷 P3003 [USACO10DEC]苹果交货Apple Delivery 题目描述 Bessie has two crisp red apples to deliver to two of he ...
随机推荐
- JavaScript中Array方法总览
title: JavaScript中Array方法总览 toc: true date: 2018-10-13 12:48:14 push(x) 将x添加到数组最后,可添加多个值,返回数组长度.改变原数 ...
- CDN(Content Distribution Network)概念
CDN的全称是Content Delivery Network,即内容分发网络.其基本思路是尽可能避开互联网上有可能影响数据传输速度和稳定性的瓶颈和环节,使内容传输的更快.更稳定.通过在网络各处放置节 ...
- HttpWebRequest 表单提交
/// <summary> /// http请求 /// </summary> public static class ZkWebRequestHelp { /// <s ...
- python 3.x 学习笔记13 (网络编程socket)
1.协议http.smtp.dns.ftp.ssh.snmp.icmp.dhcp....等具体自查 2.OSI七层应用.表示.会话.传输.网络.数据链路.物理 3.socket: 对所有上层协议的封装 ...
- canvas图片滚动
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- [BeiJing2006]狼抓兔子 dijkstra+平面图最小割
一眼裸的最大流求最小割,然而数据范围过大,跑不下来. 我们可以将平面图转成对偶图,并进行连边. 这样,每条边的长度就对应原图中的割边长度. 起点到终点的最短路即为最小割. 别用SPFA,会死的很惨 C ...
- for 的相关用法
forEach() <!DOCTYPE html> <html> <head> <meta charset="utf-8"> < ...
- WPF内嵌WCF服务对外提供接口
要测试本帖子代码请记得管理员权限运行vs. 我写这个帖子的初衷是在我做surface小车的时候有类似的需求,感觉这个功能还挺有意思的,所以就分享给大家,网上有很多关于wcf的文章 我就不一一列举了.公 ...
- mysql日期加减运算
MySQL 日期类型MySQL 日期类型:日期格式.所占存储空间.日期范围 比较. 日期类型 存储空间 日期格式 日期范围 --------- ...
- Python seed() 函数--每次产生一样的随机数系列
import random random.seed( 10 ) print("Random number with seed 10 : ", random.random()) #0 ...