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 ...
随机推荐
- [JZOJ 5910] [NOIP2018模拟10.18] DuLiu 解题报告 (并查集+思维)
题目链接: https://jzoj.net/senior/#contest/show/2530/0 题目: LF是毒瘤出题人中AK IOI2019,不屑于参加NOI的唯一的人.他对人说话,总是满口垃 ...
- windows快速找到host文件
https://jingyan.baidu.com/article/1e5468f96f7345484961b71e.html
- Thread Control Block
Thread Control Block The following is the declaration of the Thread Control Block. struct tcb { u32_ ...
- 存储Hyper-V虚拟机的硬盘空间不足时的处理
存储Hyper-V虚拟机的硬盘空间严重不足时的处理 ==先导出虚拟机到空间足够的硬盘,再在空间足够的分区上导入虚拟机 方法如下: 导出虚拟机: 导出之前,我们先删除不需要的快照. 在Hyper-V ...
- 查看Linux 服务器是 32位还是64位的
查看Linux 服务器是 32位还是64位的 getconf LONG_BIT 返回 64 代表就是 64位的: 返回 32 代表就是 32位的:
- django框架-DRF工程之权限功能
1.相对于flask,原生而言django,DRF做的则更加的合理化,想要给予用户相应的权限,首先需要在settings中进行配置 REST_FRAMEWORK = { 'DEAFAULT_PERMI ...
- Django综合基础知识
Django框架简介 MVC框架和MTV框架 MVC,全名是Model View Controller,是软件工程中的一种软件架构模式,把软件系统分为三个基本部分:模型(Model).视图(View) ...
- 【Paper Reading】Object Recognition from Scale-Invariant Features
Paper: Object Recognition from Scale-Invariant Features Sorce: http://www.cs.ubc.ca/~lowe/papers/icc ...
- Qt之QNetworkProxy(网络代理)
简述 QNetworkProxy类提供了一个网络层代理. QNetworkProxy提供了配置网络层代理支持Qt网络类的方法.目前支持的类有QAbstractSocket.QTcpSocket.QUd ...
- 第8章2节《MonkeyRunner源代码剖析》MonkeyRunner启动执行过程-解析处理命令行參数
MonkeyRunnerStarter是MonkeyRunner启动时的入口类,由于它里面包括了main方法.它的整个启动过程主要做了以下几件事情: 解析用户启动MonkeyRunner时从命令行传输 ...