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

题目描述

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:

               3        2       2
[1]-----[2]------[3]-----[4]
\ / \ /
7\ /4 \3 /2
\ / \ /
[5]-----[6]------[7]
1 2

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.

贝西有两个又香又脆的红苹果要送给她的两个朋友。当然她可以走的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各不相同。

输入输出格式

输入格式:

* 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

输出格式:

* Line 1: The shortest distance Bessie must travel to deliver both apples

输入输出样例

输入样例#1: 复制

9 7 5 1 4
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
输出样例#1: 复制

12 

思路:SPFA + SLF 优化(以前从来没使过)        难度:提高+/省选- (自认为难度应该再低点)
接下来讲一下我做这道题的(被坑)历程
首先我看了看这道题 不就是跑两边SPFA吗,然后。。。
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#define M 200005
#define MAXN 0x7fffffff
using namespace std;
queue<int> q;
int m, n, s, t1, t2;
int tot, minn;
int dis[M], vis[M];
int to[M*], net[M*], head[M*], cap[M*]; void add(int u, int v, int w) {
to[++tot] = v; net[tot] = head[u]; head[u] = tot; cap[tot] = w;
to[++tot] = u; net[tot] = head[v]; head[v] = tot; cap[tot] = w;
} void spfa(int x) {
for(int i = ; i <= n; i++) vis[i] = , dis[i] = MAXN;
dis[x] = ; vis[x] = ; q.push(x);
while(!q.empty()) {
int y = q.front(); q.pop(); vis[y] = ;
for(int i = head[y]; i; i = net[i]) {
int t = to[i];
if(dis[t] > dis[y]+cap[i]) {
dis[t] = dis[y]+cap[i];
if(!vis[t]) vis[t] = , q.push(t);
}
}
}
} int main() {
scanf("%d%d%d%d%d", &m, &n, &s, &t1, &t2);
for(int i = ; i <= m; i++) {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
add(a, b, c);
}
spfa(t1);
minn = dis[s] + dis[t2];
spfa(t2);
minn = min(minn, dis[s]+dis[t1]);
printf("%d", minn);
return ;
}

裸的SPFA,RE了两个点

居然RE了!我应该开的够大啊,然后数组多开了一个0,统统开long long,and then。。。

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#define LL long long
#define M 2000005
#define MAXN 0x7fffffff
using namespace std;
queue<LL> q;
LL m, n, s, t1, t2;
LL tot, minn;
LL dis[M], vis[M];
LL to[M*], net[M*], head[M*], cap[M*]; void add(LL u, LL v, LL w) {
to[++tot] = v; net[tot] = head[u]; head[u] = tot; cap[tot] = w;
to[++tot] = u; net[tot] = head[v]; head[v] = tot; cap[tot] = w;
} void spfa(LL x) {
for(LL i = ; i <= n; i++) vis[i] = , dis[i] = MAXN;
dis[x] = ; vis[x] = ; q.push(x);
while(!q.empty()) {
int y = q.front(); q.pop(); vis[y] = ;
for(LL i = head[y]; i; i = net[i]) {
LL t = to[i];
if(dis[t] > dis[y]+cap[i]) {
dis[t] = dis[y]+cap[i];
if(!vis[t]) vis[t] = , q.push(t);
}
}
}
} int main() {
scanf("%lld%lld%lld%lld%lld", &m, &n, &s, &t1, &t2);
for(LL i = ; i <= m; i++) {
LL a, b, c;
scanf("%lld%lld%lld", &a, &b, &c);
add(a, b, c);
}
spfa(t1);
minn = dis[s] + dis[t2];
spfa(t2);
minn = min(minn, dis[s]+dis[t1]);
printf("%lld", minn);
return ;
}

结果并没有什么卵用,继续RE两个点。。

然后我明智(实在没办法)的问了学姐,结果她告诉我:“这个题我记得要用SLF优化” QAQ

但是我不会啊

然后学姐讲了加了SLF之后的变化,但是不知道为啥,我样例也过不了了 (大哭)

通过比较发现,不开long long的时候结果还是对滴。。

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<deque>
#define M 200005
#define MAXN 0x7fffffff
using namespace std;
deque<int> q;
int m, n, s, t1, t2;
int tot, minn;
int dis[M], vis[M];
int to[M*], net[M*], head[M*], cap[M*]; void add(int u, int v, int w) {
to[++tot] = v; net[tot] = head[u]; head[u] = tot; cap[tot] = w;
to[++tot] = u; net[tot] = head[v]; head[v] = tot; cap[tot] = w;
} void spfa(int x) {
for(int i = ; i <= n; i++) vis[i] = , dis[i] = MAXN;
dis[x] = ; vis[x] = ; q.push_back(x);
while(!q.empty()) {
int y = q.front(); q.pop_front(); vis[y] = ;
for(int i = head[y]; i; i = net[i]) {
int t = to[i];
if(dis[t] > dis[y]+cap[i]) {
dis[t] = dis[y]+cap[i];
if(!vis[t]) {
vis[t] = ;
if(q.empty() || dis[t]<dis[q.front()]) q.push_front(t);
else q.push_back(t);
}
}
}
}
} int main() {
scanf("%d%d%d%d%d", &m, &n, &s, &t1, &t2);
for(int i = ; i <= m; i++) {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
add(a, b, c);
}
spfa(t1);
minn = dis[s] + dis[t2];
spfa(t2);
minn = min(minn, dis[s]+dis[t1]);
printf("%d", minn);
return ;
}

long long改回int,然后就A了。。

内心一万头 * * * 奔过。。。。

不过幸好最后还是A了

洛谷 P3003 [USACO10DEC]苹果交货Apple Delivery的更多相关文章

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

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

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

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

  3. P3003 [USACO10DEC]苹果交货Apple Delivery

    题目描述 Bessie has two crisp red apples to deliver to two of her friends in the herd. Of course, she tr ...

  4. Dijkstra【p3003(bzoj2100)】[USACO10DEC]苹果交货Apple Delivery

    Description 贝西有两个又香又脆的红苹果要送给她的两个朋友.当然她可以走的C(1<=C<=200000)条"牛路"都被包含在一种常用的图中,包含了P(1< ...

  5. luoguP3003 [USACO10DEC]苹果交货Apple Delivery

    LOL新英雄卡莎点击就送 一句话题意: 三个点a1,a2,b,求从b到a1和a2的最短路 做法:求出a1->b和a2->b的最短路,两者取min,之后再加上a1->a2的最短路 为啥 ...

  6. 洛谷P3003 苹果交货Apple Delivery

    题目描述 贝西有两个又香又脆的红苹果要送给她的两个朋友.当然她可以走的\(C(1 \leq C \leq 200000)\)条"牛路"都被包含在一种常用的图中,包含了\(P(1 \ ...

  7. 洛谷P3004 [USACO10DEC]宝箱Treasure Chest

    P3004 [USACO10DEC]宝箱Treasure Chest 题目描述 Bessie and Bonnie have found a treasure chest full of marvel ...

  8. 洛谷——P2386 放苹果

    P2386 放苹果 题目背景 (poj1664) 题目描述 把M个同样的苹果放在N个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分发(5,1,1和1,1,5是同一种方法) 输入输出格式 输入 ...

  9. 洛谷——P2690 接苹果

    P2690 接苹果 题目背景 USACO 题目描述 很少有人知道奶牛爱吃苹果.农夫约翰的农场上有两棵苹果树(编号为1和2), 每一棵树上都长满了苹果.奶牛贝茜无法摘下树上的苹果,所以她只能等待苹果 从 ...

随机推荐

  1. PPAPI中使用Chromium的3D图形接口

    使用PPAPI的Graphics 3D接口做了一个小演示样例,鼠标点击插件区域.绘制颜色,效果与ppapi_simple相似. foruok原创,如需转载请关注foruok的微信订阅号"程序 ...

  2. BZOJ 1578 DP

    思路:裸的完全背包 //By SiriusRen #include <cstdio> #include <cstring> #include <algorithm> ...

  3. Gym - 100203G Good elements 水+模拟

    题意:good element的定义是a[i]在1~i-1中任取三个数(可以重复)的和能等于a[i] 思路:vis[x]标记一下任两个数的和,处理a[i]时枚举1~i-1判断vis[a[i] - a[ ...

  4. RAID信息存放位置!

    今天偶然的机会,客户打电话说有一台DELL T110的服务器换了主板电池RAID信息没了进不去系统了,问我怎么处理,T110的RAID是主板集成的S100的RAID卡(算是软RAID,通过BIOS配置 ...

  5. 5G时代即将到来,有线网络WiFi会消失不见吗?

    说到WiFi大家都不陌生了,特别是智能手机出现后,WiFi发展的速度更是可以用“神速”来形容,几乎到处都有WiFi覆盖.以致于现在大家无论去到哪里,往往第一句话就是问“这里有没有WiFi?”或者“Wi ...

  6. 今日SGU 5.17

    SGU 119 题意:给你一个0-15组成的4*4的矩形,问你能不能回到正常 收获:把矩形变成一维数组,然后判断当前矩形状态到目标状态(逆序对为15)逆序对和0到目标的奇偶性是否不相同,证明题,引荐大 ...

  7. tcp_tw_recycle检查tcp_timestamps的内核代码

    注意:本文档中的内核代码的版本号:linux-4.0.5 /************************************************* * Author : Samson * ...

  8. 使用 Facebook开源动画库 POP 实现真实衰减动画

    1. POP动画基于底层刷新原理.是基于CADisplayLink,1秒钟运行60秒,接近于游戏开发引擎 @interface ViewController () @property (nonatom ...

  9. js函数的属性和方法

    js函数的属性和方法 前面的话 函数是javascript中特殊的对象,可以拥有属性和方法,就像普通的对象拥有属性和方法一样.甚至可以用Function()构造函数来创建新的函数对象.本文是深入理解j ...

  10. assert增强宏的实现

    作者:朱金灿 来源:http://blog.csdn.net/clever101 标准c的assert宏和MFC的ASSERT宏都不支持输出太多的信息.今天实现了一个assert增强宏,可以输出更多的 ...