Uva-10986_sending Email (forward star +dijkstra)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.