Dijkstra【p3003(bzoj2100)】[USACO10DEC]苹果交货Apple Delivery
Description
贝西有两个又香又脆的红苹果要送给她的两个朋友。当然她可以走的C(1<=C<=200000)条“牛路”都被包含在一种常用的图中,包含了P(1<=P<=100000)个牧场,分别被标为1..P。没有“牛路”会从一个牧场又走回它自己。“牛路”是双向的,每条牛路都会被标上一个距离。最重要的是,每个牧场都可以通向另一个牧场。每条牛路都连接着两个不同的牧场P1_i和P2_i(1<=P1_i,p2_i<=P),距离为D_i。所有“牛路”的距离之和不大于2000000000。
现在,贝西要从牧场PB开始给PA_1和PA_2牧场各送一个苹果(PA_1和PA_2顺序可以调换),那么最短的距离是多少呢?当然,PB、PA_1和PA_2各不相同。
Input
第一行,5个整数,\(C,P,PB,PA1,PA2\)(含义如题所述)
接下来\(C\)行,每行三个整数\(x,y,z\)描述一条无向边。
Output
一个整数,代表最小距离.
难得遇到一个这么裸的最短路题。
貌似\(Spfa\)会被\(Hack\)?
直接写\(Dijkstra\)。
分别以\(PA1\),\(PA2\)为起点跑\(Dijkstra\)。
每次对于到达其他两个点的距离取\(min\)即可。
代码
#include<cstdio>
#include<algorithm>
#include<queue>
#include<iostream>
#define R register
using namespace std;
const int gz=200008;
inline void in(int &x)
{
int f=1;x=0;char s=getchar();
while(!isdigit(s)){if(s=='-')f=-1;s=getchar();}
while(isdigit(s)){x=x*10+s-'0';s=getchar();}
x*=f;
}
int m,n,s,a,b,ans=214748364;
int head[gz],tot,dis[gz];
bool vis[gz];
struct hop
{
int u,d;
bool operator <(const hop&a)const
{
return d>a.d;
}
};
struct cod{int u,v,w;}edge[gz<<2];
inline void add(R int x,R int y,R int z)
{
edge[++tot].u=head[x];
edge[tot].v=y;
edge[tot].w=z;
head[x]=tot;
}
inline void dijkstra(R int s)
{
for(R int i=1;i<=n;i++)dis[i]=214748364,vis[i]=false;
priority_queue<hop>q;
q.push((hop){s,dis[s]=0});
while(!q.empty())
{
R int u=q.top().u;q.pop();
if(vis[u])continue;
vis[u]=true;
for(R int i=head[u];i;i=edge[i].u)
{
if(dis[edge[i].v]>dis[u]+edge[i].w)
{
dis[edge[i].v]=dis[u]+edge[i].w;
q.push((hop){edge[i].v,dis[edge[i].v]});
}
}
}
}
int main()
{
in(m),in(n),in(s),in(a),in(b);
for(R int i=1,x,y,z;i<=m;i++)
{
in(x),in(y),in(z);
add(x,y,z);add(y,x,z);
}
dijkstra(a);
ans=min(ans,dis[b]+dis[s]);
dijkstra(b);
ans=min(ans,dis[a]+dis[s]);
printf("%d",ans);
}
Dijkstra【p3003(bzoj2100)】[USACO10DEC]苹果交货Apple Delivery的更多相关文章
- 洛谷——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 her f ...
- 洛谷 P3003 [USACO10DEC]苹果交货Apple Delivery
洛谷 P3003 [USACO10DEC]苹果交货Apple Delivery 题目描述 Bessie has two crisp red apples to deliver to two of he ...
- P3003 [USACO10DEC]苹果交货Apple Delivery
题目描述 Bessie has two crisp red apples to deliver to two of her friends in the herd. Of course, she tr ...
- luoguP3003 [USACO10DEC]苹果交货Apple Delivery
LOL新英雄卡莎点击就送 一句话题意: 三个点a1,a2,b,求从b到a1和a2的最短路 做法:求出a1->b和a2->b的最短路,两者取min,之后再加上a1->a2的最短路 为啥 ...
- 洛谷P3003 苹果交货Apple Delivery
题目描述 贝西有两个又香又脆的红苹果要送给她的两个朋友.当然她可以走的\(C(1 \leq C \leq 200000)\)条"牛路"都被包含在一种常用的图中,包含了\(P(1 \ ...
- 【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 ...
- USACO Apple Delivery
洛谷 P3003 [USACO10DEC]苹果交货Apple Delivery 洛谷传送门 JDOJ 2717: USACO 2010 Dec Silver 1.Apple Delivery JDOJ ...
- BZOJ 2100: [Usaco2010 Dec]Apple Delivery( 最短路 )
跑两遍最短路就好了.. 话说这翻译2333 ---------------------------------------------------------------------- #includ ...
随机推荐
- 【南开OJ2264】节操大师(贪心+二分+并查集/平衡树)
好久没更新了,今天就随便写一个吧 题目内容 MK和他的小伙伴们(共n人,且保证n为2的正整数幂)想要比试一下谁更有节操,于是他们组织了一场节操淘汰赛.他们的比赛规则简单而暴力:两人的节操正面相撞,碎的 ...
- [POJ 1204]Word Puzzles(Trie树暴搜&AC自己主动机)
Description Word puzzles are usually simple and very entertaining for all ages. They are so entertai ...
- BZOJ_DAY6???
昨天没睡好啊啊啊,真是要命,睡不着,今天状态爆炸...34题击破. 下一步目标:网络流24题,树链剖分. (洛谷比赛了好开心,希望这次能比以前强吧,嗯)
- HDU 3446 有贪心思想的01背包
Proud Merchants Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) ...
- Covered Points Count(思维题)
C. Covered Points Count time limit per test 3 seconds memory limit per test 256 megabytes input stan ...
- 1、linux下mysql5.5.20安装过程报错汇总
1.Access denied for user 'root'@'localhost' (using password: YES) 这个提示是因为root帐户默认不开放远程访问权限,所以需要修改一下相 ...
- oracle导入和导出和授权
导入数据库: imp demo@orcl file=d:/bak_1023.dmp full=y ignore=y 导出数据库: @orcl file=d:/bak_1023.dmpexp yhtj/ ...
- NodeJS概述
NodeJS中文API 一.概述 Node.js 是一种建立在Google Chrome’s v8 engine上的 non-blocking (非阻塞), event-driven (基于事件的) ...
- hive向表格中插入数据并分析语句
1,---导入mds_imei_month_info ; //最大的动态分区表 set hive.support.concurrency=false; //是否支持并发 ; //each mapper ...
- bzoj 1500 修改区间 splay
内个我也不知道哪儿不对,TLE了,说说思路吧 其实思路也没什么说的,就是裸的splay,对于最后一个操作 我们记下每个区间的最长前缀,最长后缀,那么最长子序列就是 前缀,后缀,左子树的后缀+右子树的前 ...