Luogu P1339 heat wave Heat Wave

Luogu P1339 heat wave Heat Wave

• Single-source shortest bare.
but!
There are pit:

  1. Calculated that complexity does not find Floyd ran past I do not use the.
  2. If you build a two-way side edge, the edge of the array size to open twice!
  3. If the examination room and then initialized \ (dis [i] = INF \) written \ (DIS [n-] = INF \) , or forget \ (dis [s] = 0 \) or the like, then, directly crematory ......
#include<bits/stdc++.h>
#define N 2510
#define M 6210
#define INF 0x3f3f3f3f

using namespace std;

int n,m,s,e,cnt;
int head[M*2],dis[N];

struct node {
    int nxt,to,val;
    bool operator < (const node& rhs) const {
        return rhs.val<val;
    }
}edge[M*2];

struct headnode {
    int u,d;
    bool operator < (const headnode& rhs) const {
        return rhs.d<d;
    }
};

void addEdge(int u,int v,int w) {
    edge[++cnt]=(node){head[u],v,w};
    head[u]=cnt;
    return;
}

void Read() {
    scanf("%d%d%d%d",&n,&m,&s,&e);
    for(int i=1;i<=m;i++) {
        int u,v,w;
        scanf("%d%d%d",&u,&v,&w);
        addEdge(u,v,w);
        addEdge(v,u,w);
    }
    return;
}

void Init() {
    for(int i=1;i<=n;i++) {
        dis[i]=INF;
    }
    dis[s]=0;
    return;
}

void Dijkstra() {
    priority_queue <headnode> q;
    q.push((headnode){s,dis[s]});
    while(!q.empty()) {
        headnode x=q.top();
        q.pop();
        int u=x.u;
        if(x.d<dis[u]) {
            continue;
        }
        for(int i=head[u];i;i=edge[i].nxt) {
            int v=edge[i].to,w=edge[i].val;
            if(dis[u]+w<dis[v]) {
                dis[v]=dis[u]+w;
                q.push((headnode){v,dis[v]});
            }
        }
    }
    return;
}

void Print() {
    printf("%d",dis[e]);
    return;
}

int main()
{
    Read();
    Init();
    Dijkstra();
    Print();
    return 0;
}

Guess you like

Origin www.cnblogs.com/luoshui-tianyi/p/11688702.html