题目描述

一张P个点的无向图,C条正权路。CLJ要从Pb点(家)出发,既要去Pa1点NOI赛场拿金牌,也要去Pa2点CMO赛场拿金牌。(途中不必回家)可以先去NOI,也可以先去CMO。当然神犇CLJ肯定会使总路程最小,输出最小值。

题解:做两遍spfa,找出从起点开始先去pa1或者先去pa2的最小值

需要用一下spfa的优化,每次进行入队的时候都与队头进行比较,如果比队头小就放在队头,否则放队尾。

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define maxn 100005
  4. #define maxm 200005
  5. int dis[maxn],head[maxn],q[maxn];
  6. bool vis[maxn];
  7. struct edge{
  8. int next,to,w;
  9. }e[maxm*];
  10. int n,m,s,t1,t2;
  11. int ans=,cnt;
  12. inline int read(){
  13. int x=,f=;char ch=getchar();
  14. while(ch>''||ch<''){if(ch=='-')f=-;ch=getchar();}
  15. while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
  16. return x*f;
  17. }
  18. void insert(int u,int v,int w){
  19. cnt++;
  20. e[cnt].next=head[u];e[cnt].to=v;e[cnt].w=w;
  21. head[u]=cnt;
  22. }
  23. void spfa(int x){
  24. memset(dis,,sizeof dis);
  25. vis[x]=;dis[x]=;q[]=x;
  26. int top=,tail=;
  27. while(top!=tail)
  28. {
  29. int now=q[top];top++;
  30. if(top==)top=;
  31. for(int i=head[now];i;i=e[i].next){
  32. int p=e[i].to;
  33. if(dis[p]>dis[now]+e[i].w){
  34. dis[p]=dis[now]+e[i].w;
  35. if(!vis[p]){
  36. vis[p]=;
  37. if(dis[p]<dis[q[top]]){
  38. top--;if(top<)top=;
  39. q[top]=p;
  40. }
  41. else{
  42. q[tail++]=p;
  43. if(tail==)tail=;
  44. }
  45. }
  46. }
  47. }
  48. vis[now]=;
  49. }
  50. }
  51. int main(){
  52. m=read();n=read();s=read();t1=read();t2=read();
  53. int u,v,w;
  54. for(int i=;i<=m;i++){
  55. u=read();v=read();w=read();
  56. insert(u,v,w);insert(v,u,w);
  57. }
  58. spfa(t1);
  59. ans=dis[s]+dis[t2];
  60. spfa(t2);
  61. ans=min(ans,dis[s]+dis[t1]);
  62. printf("%d",ans);
  63. }

bzoj2100 [Usaco2010 DEC]Apple Delivery苹果贸易的更多相关文章

  1. bzoj2100 [Usaco2010 Dec]Apple Delivery

    Description Bessie has two crisp red apples to deliver to two of her friends in the herd. Of course, ...

  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. 洛谷P3003 [USACO10DEC]苹果交货Apple Delivery

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

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

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

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

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

随机推荐

  1. [JZOJ 5910] [NOIP2018模拟10.18] DuLiu 解题报告 (并查集+思维)

    题目链接: https://jzoj.net/senior/#contest/show/2530/0 题目: LF是毒瘤出题人中AK IOI2019,不屑于参加NOI的唯一的人.他对人说话,总是满口垃 ...

  2. windows快速找到host文件

    https://jingyan.baidu.com/article/1e5468f96f7345484961b71e.html

  3. Thread Control Block

    Thread Control Block The following is the declaration of the Thread Control Block. struct tcb { u32_ ...

  4. 存储Hyper-V虚拟机的硬盘空间不足时的处理

    存储Hyper-V虚拟机的硬盘空间严重不足时的处理   ==先导出虚拟机到空间足够的硬盘,再在空间足够的分区上导入虚拟机 方法如下: 导出虚拟机: 导出之前,我们先删除不需要的快照. 在Hyper-V ...

  5. 查看Linux 服务器是 32位还是64位的

    查看Linux 服务器是 32位还是64位的 getconf LONG_BIT 返回 64 代表就是 64位的: 返回 32 代表就是 32位的:

  6. django框架-DRF工程之权限功能

    1.相对于flask,原生而言django,DRF做的则更加的合理化,想要给予用户相应的权限,首先需要在settings中进行配置 REST_FRAMEWORK = { 'DEAFAULT_PERMI ...

  7. Django综合基础知识

    Django框架简介 MVC框架和MTV框架 MVC,全名是Model View Controller,是软件工程中的一种软件架构模式,把软件系统分为三个基本部分:模型(Model).视图(View) ...

  8. 【Paper Reading】Object Recognition from Scale-Invariant Features

    Paper: Object Recognition from Scale-Invariant Features Sorce: http://www.cs.ubc.ca/~lowe/papers/icc ...

  9. Qt之QNetworkProxy(网络代理)

    简述 QNetworkProxy类提供了一个网络层代理. QNetworkProxy提供了配置网络层代理支持Qt网络类的方法.目前支持的类有QAbstractSocket.QTcpSocket.QUd ...

  10. 第8章2节《MonkeyRunner源代码剖析》MonkeyRunner启动执行过程-解析处理命令行參数

    MonkeyRunnerStarter是MonkeyRunner启动时的入口类,由于它里面包括了main方法.它的整个启动过程主要做了以下几件事情: 解析用户启动MonkeyRunner时从命令行传输 ...