码迷,mamicode.com
首页 > 其他好文 > 详细

P1339 热浪【最短路】

时间:2020-02-18 15:08:20      阅读:73      评论:0      收藏:0      [点我收藏+]

标签:def   read   scanf   inline   return   false   pac   temp   clu   

#include <bits/stdc++.h>
#define dbg(x) cout << #x << "=" << x << endl
#define eps 1e-8
#define pi acos(-1.0)

using namespace std;
typedef long long LL;

template<class T>inline void read(T &res)
{
    char c;T flag=1;
    while((c=getchar())<0||c>9)if(c==-)flag=-1;res=c-0;
    while((c=getchar())>=0&&c<=9)res=res*10+c-0;res*=flag;
}

namespace _buff {
    const size_t BUFF = 1 << 19;
    char ibuf[BUFF], *ib = ibuf, *ie = ibuf;
    char getc() {
        if (ib == ie) {
            ib = ibuf;
            ie = ibuf + fread(ibuf, 1, BUFF, stdin);
        }
        return ib == ie ? -1 : *ib++;
    }
}

int qread() {
    using namespace _buff;
    int ret = 0;
    bool pos = true;
    char c = getc();
    for (; (c < 0 || c > 9) && c != -; c = getc()) {
        assert(~c);
    }
    if (c == -) {
        pos = false;
        c = getc();
    }
    for (; c >= 0 && c <= 9; c = getc()) {
        ret = (ret << 3) + (ret << 1) + (c ^ 48);
    }
    return pos ? ret : -ret;
}

const int maxn = 1e5 + 7;
const int inf  = 0x3f3f3f3f;

int head[maxn << 1], edge[maxn << 1], w[maxn << 1], nxt[maxn << 1];
int cnt;

inline void BuildGraph (int u, int v, int c) {
    cnt++;
    edge[cnt] = v;
    nxt[cnt]  = head[u];
    w[cnt] = c;
    head[u] = cnt;
}

struct node {
    int u,v;
    bool operator <(const node &t) const {
        return u > t.u;
    }
};

int n,m,s,t;
int dis[maxn];

priority_queue < node > q;

void dijkstra(int s) {
    for (int i = 1; i <= n; ++i) {
        dis[i] = inf;
    }
    dis[s] = 0;
    node temp;
    temp.u = 0;
    temp.v = s;
    q.push(temp);
    while(!q.empty()) {
        int u = q.top().v;
        int d = q.top().u;
        q.pop();
        if(d != dis[u]) continue;
        for (int i = head[u]; i; i = nxt[i]) {
            int v = edge[i];
            int c = w[i];
            if(dis[v] > dis[u] + c) {
                dis[v] = dis[u] + c;
                node p;
                p.u = dis[v], p.v = v;
                q.push(p);
            }
        }
    }
}

int main()
{
    scanf("%d %d %d %d",&n, &m, &s, &t);
    int a, b, c;
    for (int i = 1; i <= m; ++i) {
        scanf("%d %d %d",&a, &b, &c);
        BuildGraph(a, b, c);
        BuildGraph(b, a, c);
    }
    dijkstra(s);
    printf("%d\n",dis[t]);
    return 0;
}

 

P1339 热浪【最短路】

标签:def   read   scanf   inline   return   false   pac   temp   clu   

原文地址:https://www.cnblogs.com/orangeko/p/12326092.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!