PAT1111. Online Map

Source: Internet
Author: User
Tags define local

Test points: Shortest path, Dijkstra algorithm, DFS

Ideas and submissions: The subject of direct DJ algorithm, the first time to submit the DJ did not write to, mainly the first cycle to find the minimum value when you forget to judge the Vis, the second time did not consider the same path, the most time is the logic of the mistake, Although the problem is to output the shortest short-circuit the fastest but in the maintenance time of this weight is not used through the fastest path to get the weight, but in the quickest path to update their own weights, but also because the weights are the same array so call two DJ method order to the title.

#define LOCAL #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> # Include <cmath> #include <string> #include <vector> #include <map> #include <set> #include  <queue> #include <stack> #define for (i, X, y) for (int i = x; l <= y; i++) #define RFOR (i, x, y) for (int i = X I >= y;
i--) #define MAXN #define OO 0x3f3f3f3f using namespace std;
int n,m;
int G[MAXN][MAXN];
int COST[MAXN][MAXN];
int NUM[MAXN];
int PRE[MAXN];
int D[MAXN];
int W[MAXN];
int VIS[MAXN];
int PRET[MAXN];
    void Dj_time (int s) {memset (vis,0,sizeof (VIS));
    Fill (W,w+maxn,oo);
    w[s]=0;
    Num[s]=1;
    memset (pre,-1,sizeof (pre));
        for (i,0,n-1) {int min=oo;
        int u=-1;
                for (j,0,n-1) {if (w[j]<min&&vis[j]==0) {min=w[j];
            U=j;
        }} if (U==-1) return;
        Vis[u]=1;
    for (j,0,n-1) {        if (cost[u][j]!=oo&&vis[j]==0) {if (W[j]>w[u]+cost[u][j]) {
                    W[J]=W[U]+COST[U][J];
                    Pre[j]=u;
                num[j]=num[u]+1;
                        }else if (W[j]==w[u]+cost[u][j]) {if (num[j]>num[u]+1) {pre[j]=u;
                    num[j]=num[u]+1;
    }}}}}} void DJ (int s) {memset (vis,0,sizeof (VIS));
    Fill (D,d+maxn,oo);
    Fill (W,w+maxn,oo);
    d[s]=0;

    w[s]=0;
    memset (pret,-1,sizeof (pret));
        for (i,0,n-1) {int min=oo;
        int u=-1;
                for (j,0,n-1) {if (d[j]<min&&vis[j]==0) {min=d[j];
            U=j;
        }} if (U==-1) return;
        Vis[u]=1; for (j,0,n-1) {if (g[u][j]!=oo&&vis[j]==0) {if (d[j]>d[u]+g[u][j
    ]){                D[J]=D[U]+G[U][J];
                    W[J]=W[U]+COST[U][J];
                Pret[j]=u; }else if (D[j]==d[u]+g[u][j]) {if (W[j]>w[u]+cost[u][j]) {w[j]=w[u]+cost[u][j
                        ];
                    Pret[j]=u;
        }}}}}} void DFS (int s,int d) {if (S==d) {printf ("%d", s);
    Return
        }else{DFS (S,pre[d]);
    printf ("-%d", d);
    }} void Dfst (int s,int d) {if (S==d) {printf ("%d", s); return;
        }else{dfst (S,pret[d]);
    printf ("-%d", d);
        }} int main () {#ifdef LOCAL freopen ("data.in", "R", stdin);
    Freopen ("Data.out", "w", stdout);
    #endif//LOCAL scanf ("%d%d", &n,&m);
    Fill (G[0],g[0]+maxn*maxn,oo);
    Fill (Cost[0],cost[0]+maxn*maxn,oo);
        for (i,0,m-1) {int u,v;
        int way,len,time; scanf ("%d%d%d%d%d", &u,&v,&amP;way,&len,&time);
            if (way==0) {G[u][v]=g[v][u]=len;
        Cost[u][v]=cost[v][u]=time;
        }else{G[u][v]=len;cost[u][v]=time;
    }} int st,dn;
    scanf ("%d%d", &AMP;ST,&AMP;DN);
    DJ (ST);

    Dj_time (ST);
    int flag=0;
    int k=dn;
    while (pre[k]==pret[k]&&k!=st) {k=pre[k]; } if (k==st) {printf ("Distance =%d;
        Time =%d: ", D[dn],w[dn]);
        DFS (ST,DN);
    return 0;
    } printf ("Distance =%d:", D[dn]);
    DFST (ST,DN);
    printf ("\ n");
    printf ("Time =%d:", W[dn]);

    DFS (ST,DN);
return 0;
 }


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.