標籤:using direct bad cst -o one color space 輸出
洛穀 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 0x7fffffffusing namespace std;queue<int> q;int m, n, s, t1, t2;int tot, minn;int dis[M], vis[M];int to[M*2], net[M*2], head[M*2], cap[M*2];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 = 1; i <= n; i++) vis[i] = 0, dis[i] = MAXN; dis[x] = 0; vis[x] = 1; q.push(x); while(!q.empty()) { int y = q.front(); q.pop(); vis[y] = 0; 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] = 1, q.push(t); } } }}int main() { scanf("%d%d%d%d%d", &m, &n, &s, &t1, &t2); for(int i = 1; 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 0;}
裸的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 0x7fffffffusing namespace std;queue<LL> q;LL m, n, s, t1, t2;LL tot, minn;LL dis[M], vis[M];LL to[M*2], net[M*2], head[M*2], cap[M*2];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 = 1; i <= n; i++) vis[i] = 0, dis[i] = MAXN; dis[x] = 0; vis[x] = 1; q.push(x); while(!q.empty()) { int y = q.front(); q.pop(); vis[y] = 0; 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] = 1, q.push(t); } } }}int main() { scanf("%lld%lld%lld%lld%lld", &m, &n, &s, &t1, &t2); for(LL i = 1; 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 0;}
結果並沒有什麼卵用,繼續RE兩個點。。
然後我明智(實在沒辦法)的問了學姐,結果她告訴我:“這個題我記得要用SLF最佳化” QAQ
但是我不會啊
然後學姐講了加了SLF之後的變化,但是不知道為啥,我範例也過不了了 (大哭)
通過比較發現,不開long long的時候結果還是對滴。。
#include<algorithm>#include<iostream>#include<cstring>#include<cstdio>#include<deque>#define M 200005#define MAXN 0x7fffffffusing namespace std;deque<int> q;int m, n, s, t1, t2;int tot, minn;int dis[M], vis[M];int to[M*2], net[M*2], head[M*2], cap[M*2];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 = 1; i <= n; i++) vis[i] = 0, dis[i] = MAXN; dis[x] = 0; vis[x] = 1; q.push_back(x); while(!q.empty()) { int y = q.front(); q.pop_front(); vis[y] = 0; 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] = 1; 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 = 1; 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 0;}
long long改回int,然後就A了。。
內心一萬頭 * * * 奔過。。。。
不過幸好最後還是A了
洛穀 P3003 [USACO10DEC]蘋果交貨Apple Delivery