BZOJ2100: [Usaco2010 Dec]Apple Delivery Spfa+优化
来源:互联网 发布:物理数据模型转换为sql 编辑:程序博客网 时间:2024/06/14 17:49
100: [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
题解:
两次Spfa
先以p1为原点跑出p1和p2之间的最短路径
再以pb为原点跑出pb和p1,pb和p2之间的最短路径
答案就是min(d[pb,p1],d[pb,p2])+d[p1,p2];
(但是交上去之后发现超时,在网上学习了Spfa队列优化,改用了双向队列(deque))
#include<cstdio>#include<iostream>#include<algorithm>#include<cstring>#include<cmath>#include<queue>using namespace std;const int M=600005;const int N=200005;const int inf=2130000000;int n,m,p0,p1,p2;int nxt[M],to[M],w[M],lj[N],cnt;void add(int f,int t,int p){to[++cnt]=t;nxt[cnt]=lj[f];lj[f]=cnt;w[cnt]=p;}deque<int>Q;int d[N];bool inq[N];void Spfa(int S){for(int i=1;i<=n;i++) d[i]=inf;d[S]=0;Q.push_back(S);while(!Q.empty()){int x=Q.front();Q.pop_front();inq[x]=false;for(int i=lj[x];i;i=nxt[i])if(d[to[i]]>d[x]+w[i]){d[to[i]]=d[x]+w[i];if(!inq[to[i]]){inq[to[i]]=true;if(!Q.empty()&&d[to[i]]<d[Q.front()]) Q.push_front(to[i]);else Q.push_back(to[i]);} }}}void work(){Spfa(p1);int ans=d[p2];memset(inq,0,sizeof(inq));Spfa(p0);printf("%d",min(d[p1],d[p2])+ans);}int main(){scanf("%d%d%d%d%d",&m,&n,&p0,&p1,&p2);int x,y,p;for(int i=1;i<=m;i++){scanf("%d%d%d",&x,&y,&p);add(x,y,p),add(y,x,p);}work();}
- BZOJ2100: [Usaco2010 Dec]Apple Delivery Spfa+优化
- bzoj2100 [Usaco2010 Dec]Apple Delivery(slf优化的spfa)
- bzoj2100 [Usaco2010 Dec]Apple Delivery
- 【bzoj2100】[Usaco2010 Dec]Apple Delivery 最短路
- BZOJ 2100 [Usaco2010 Dec]Apple Delivery 最短路
- spfa的2种优化——洛谷P3003 [USACO10DEC]苹果交货Apple Delivery
- Usaco2010 Dec Treasure Chest
- Usaco2010 Dec Treasure Chest
- BZOJ 2100 Apple Delivery
- bzoj2102 [Usaco2010 Dec]The Trough Game
- bzoj2101【Usaco2010 Dec】Treasure Chest 藏宝箱
- 2102: [Usaco2010 Dec]The Trough Game
- BZOJ2101: [Usaco2010 Dec]Treasure Chest 藏宝箱
- BZOJ 2101: [Usaco2010 Dec]Treasure Chest 藏宝箱
- bzoj2097[Usaco2010 Dec]Exercise 奶牛健美操
- BZOJ2101: [Usaco2010 Dec]Treasure Chest 藏宝箱
- 【bzoj2097】[Usaco2010 Dec]Exercise 奶牛健美操
- [bzoj2097][Usaco2010 Dec]Exercise 奶牛健美操
- final-finally-finalize 区别
- [Android Pro] AIDL进程间传递自定义类型参数
- Tyvj 1041 表达式计算2
- hdu-4793-Collision
- [LeetCode]--25. Reverse Nodes in k-Group
- BZOJ2100: [Usaco2010 Dec]Apple Delivery Spfa+优化
- (二十六)、正则表达式
- 1的个数问题、数组最大最小值java---编程之美
- Android 开发实践 自定义控件 详解
- 【51node】-1006- 最长公共子序列Lcs(LCS,输出序列,模板)
- 执行远程服务器上的脚本失败?(环境变量引起的问题)
- Java泛型
- 树-堆结构练习——合并果子之哈夫曼树
- 解决缺失libgtk-x11-2.0.so.0的问题