标签:des blog http io os ar for 数据 art
http://www.lydsy.com/JudgeOnline/problem.php?id=2100
这题我要吐血啊
我交了不下10次tle。。
噗
果然是写挫了。
一开始没加spfa优化果断t
然后看了题解加了(加错了T_T)还是tle。。我就怀疑数据了。。。
噗
原来我有个地方打错了。。
这个spfa的队列优化真神。。
#include <cstdio> #include <cstring> using namespace std; #define rep(i, n) for(int i=0; i<(n); ++i) #define for1(i,a,n) for(int i=(a);i<=(n);++i) #define for2(i,a,n) for(int i=(a);i<(n);++i) #define for3(i,a,n) for(int i=(a);i>=(n);--i) #define for4(i,a,n) for(int i=(a);i>(n);--i) #define CC(i,a) memset(i,a,sizeof(i)) #define read(a) a=getint() #define print(a) printf("%d", a) #define dbg(x) cout << #x << " = " << x << endl #define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; } inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<‘0‘||c>‘9‘; c=getchar()) if(c==‘-‘) k=-1; for(; c>=‘0‘&&c<=‘9‘; c=getchar()) r=r*10+c-‘0‘; return k*r; } inline const int max(const int &a, const int &b) { return a>b?a:b; } inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=100005, M=400005; int ihead[N], cnt, q[N], front, tail, d[N], n, m, x, xx, xxx; bool vis[N]; struct ED { int to, next, w; }e[M]; inline void add(const int &u, const int &v, const int &w) { e[++cnt].next=ihead[u]; ihead[u]=cnt; e[cnt].to=v; e[cnt].w=w; e[++cnt].next=ihead[v]; ihead[v]=cnt; e[cnt].to=u; e[cnt].w=w; } inline void spfa(const int &s) { memset(d, 0x3f, sizeof(int)*(n+3)); d[s]=0; vis[s]=1; front=tail=0; q[tail++]=s; while(tail!=front) { int u=q[front++], v; if(front==N) front=0; vis[u]=0; for(int i=ihead[u]; i; i=e[i].next) if(d[v=e[i].to]>d[u]+e[i].w) { d[v]=d[u]+e[i].w; if(!vis[v]) { vis[v]=1; if(d[v]<d[q[front]]) { --front; if(front<0) front+=N; q[front]=v; } else { q[tail++]=v; if(tail==N) tail=0; } } } } } int main() { read(m); read(n); read(x); read(xx); read(xxx); for1(i, 1, m) { int u=getint(), v=getint(), w=getint(); add(u, v, w); } spfa(xx); int ans=d[x]+d[xxx]; spfa(xxx); if(ans>d[x]+d[xx]) ans=d[x]+d[xx]; print(ans); return 0; }
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.
* 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
求翻译.........站内PM我吧.........
【BZOJ】2100: [Usaco2010 Dec]Apple Delivery(spfa+优化)
标签:des blog http io os ar for 数据 art
原文地址:http://www.cnblogs.com/iwtwiioi/p/3972095.html