Test instructions: Give you point, edge, the shortest distance from the beginning to the end.
The problem: Because the data volume of the topic is particularly large, so need to use adjacency table to save the edge, then the Dijkstra algorithm a little magic change can be, it would be time-out, do a heap optimization preparation, the result card time is over, can say very happy.
Note that SPFA will time out.
#include <queue> #include <cmath> #include <stack> #include <cstdio> #include <cstring># Include <cstdlib> #include <iostream> #include <algorithm>using namespace std;const int maxn = 20050; const int INF = 1e9 + 7;struct node{int next,to,w;} S[50050*2];int head[maxn],f[maxn],dis[maxn],num,n,m,s,t;void Add (int u,int v,int w) {s[num].to = v; S[num].next = Head[u]; S[NUM].W = W; Head[u] = num++;} void Spfa () {int i,j,min,k,u; for (i=0;i<n;i++) {dis[i] = INF; F[i] = 0; } for (i=head[s];i!=-1;i=s[i].next) {u = s[i].to; Dis[u] = S[I].W; } F[s] = 1; for (i=0;i<n;i++) {MIN = INF; K =-1; for (j=0;j<n;j++) {if (!f[j]&&dis[j]<min) {MIN = Dis[j]; K = J; }} if (Min==inf) break; F[k] = 1; for (J=head[k];j!=-1;j=s[j].next) { u = s[j].to; if (!f[u]&&dis[k]+s[j].w<dis[u]) dis[u] = dis[k] + s[j].w; }} if (Dis[t]!=inf) printf ("%d\n", dis[t]); else printf ("unreachable\n");} int main () {int t,i,a,b,c,k = 1; scanf ("%d", &t); while (t--) {scanf ("%d%d%d%d", &n,&m,&s,&t); for (i=0;i<n;i++) head[i] =-1; num = 0; for (i=0;i<m;i++) {scanf ("%d%d%d", &a,&b,&c); Add (A,B,C); Add (B,A,C); } printf ("Case #%d:", k++); SPFA (); } return 0;}
Uva-10986_sending Email (forward star +dijkstra)