SPFA to find the shortest path. When you see the picture, pay attention to the inverse building plan.
1 /*3696*/2#include <iostream>3#include <queue>4#include <vector>5#include <algorithm>6#include <cstdio>7#include <cstring>8#include <cmath>9#include <cstdlib>Ten using namespacestd; One A #defineMAXN 10005 - #defineMAXV 10005 - #defineMaxe 50005 the - -typedefstruct { - intV, next; + DoubleR; - } edge_t; + A Const DoubleINF =9999999.; at edge_t E[maxe]; - intL, HEAD[MAXN]; - DoubleW[MAXN], P[MAXN]; - DoubleRATE[MAXN]; - BOOLVISIT[MAXN]; - intN, M; in - voidAddedge (intUintVDoubler) { toE[L].V =v; +E[l].next =Head[u]; -E[L].R =R; theHead[u] = l++; * } $ Panax Notoginseng voidinit () { -Memset (Head,-1,sizeof(int) * (n+1)); theL =0; + } A the voidSPFA (intu) { + intI, J, K, V; -queue<int>Q; $ $ for(i=0; i<=n; ++i) -Rate[i] =-INF; -memset (Visit,false,sizeof(visit)); theRate[u] =0.0; -Visit[u] =true;Wuyi q.push (u); the - while(!P.empty ()) { WuU =Q.front (); - Q.pop (); AboutVisit[u] =false; $ for(I=head[u]; i!=-1; I=E[i].next) { -v =e[i].v; - if(Rate[v] < rate[u]+E[I].R) { -RATE[V] = Rate[u] +E[I].R; A if(!Visit[v]) { +VISIT[V] =true; the Q.push (v); - } $ } the } the } the } the - intMain () { in intI, J, K; the intu, v; the DoubleR, ans; About the #ifndef Online_judge theFreopen ("data.in","R", stdin); theFreopen ("Data.out","W", stdout); + #endif - the while(SCANF ("%d", &n)!=eof &&N) {Bayi init (); the for(i=1; i<=n; ++i) { thescanf"%LF%LF", &p[i], &w[i]); -Addedge (0, I, log (p[i])); - } thescanf"%d", &m); the while(m--) { thescanf"%d%d", &k, &u); the--K; - while(k--) { thescanf"%lf%d", &r, &v); the Addedge (V, U, log (r)); theU =v;94 } the } theSPFA (0); theAns =0.0;98 for(i=1; i<=n; ++i) { AboutR =exp (rate[i]); - if(P[i] <R)101P[i] =R;102Ans + = p[i]*W[i];103 }104printf"%.2lf\n", ans); the }106 107 return 0;108}
"HDU" 3696 Farm Game