Test instructions
Give a graph and Q queries, each asking for the minimum value of two points (distance + path maximum) in the query graph.
Analysis:
Enumerate the maximum values on the path to do SPFA, the problem of the card constant ... The queue with STL is waiting for tle.
Code:
POJ 4046//sep9#include <iostream> #define INF ((~ (0ULL)) >>1) using namespace Std;const int Maxn=1024;const int maxm=20012;typedef Long Long ll;struct edge{int v,next;ll W;} E[maxm*2];int n,m,ecnt,q;int head[maxn],a[maxm],b[maxm];ll d[maxn],ans[maxm],price[maxn];bool inq[maxN];int myque[ Maxm*4];void addedge (int a,int b,ll c) {e[ecnt].v=b;e[ecnt].w=c;e[ecnt].next=head[a];head[a]=ecnt++;e[ecnt].v=a;e[ ecnt].w=c;e[ecnt].next=head[b];head[b]=ecnt++;} void query (int s) {int i;for (i=1;i<=n;++i) {d[i]=inf;inq[i]=0;} D[s]=0,inq[s]=1;int H=0,r=0;myque[r++]=s;while (h<r) {int u=myque[h++];inq[u]=0;for (i=head[u];i!=-1;i=e[i].next {if (Price[e[i].v]>price[s]) continue;if (D[U]+E[I].W<D[E[I].V]) {d[e[i].v]=d[u]+e[i].w;if (inq[e[i].v]==0) { INQ[E[I].V]=1;MYQUE[R++]=E[I].V;}}}} for (I=1;i<=q;++i) if (D[a[i]]!=inf&&d[b[i]]!=inf&&ans[i]>d[a[i]]+d[b[i]]+price[s]) Ans[i]=d [A[i]]+d[b[i]]+price[s];} int main () {while (scanf ("%d%d", &n,&m)) {if (m==0&&n==0) Break;int I;foR (I=1;i<=n;++i) scanf ("%lld", &price[i]), Ecnt=0;memset (Head,-1,sizeof (head)), while (m--) {int A,b;ll c;scanf ( "%d%d%lld", &a,&b,&c); Addedge (a,b,c);} scanf ("%d", &q); for (i=1;i<=q;++i) {scanf ("%d%d", &a[i],&b[i]); ans[i]=inf;} for (i=1;i<=n;++i) query (i), for (I=1;i<=q;++i) if (ans[i]==inf) printf (" -1\n"), elseprintf ("%i64d\n", Ans[i]); printf ("\ n");} return 0; }
Application of POJ 4046 sightseeing enumeration thought in SPFA