P1339 热浪最短路

Posted orangeko

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了P1339 热浪最短路相关的知识,希望对你有一定的参考价值。

#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
",dis[t]);
    return 0;
}

 

以上是关于P1339 热浪最短路的主要内容,如果未能解决你的问题,请参考以下文章

[最短路]P1339 [USACO09OCT]热浪Heat Wave

洛谷P1339 [USACO09OCT]热浪Heat Wave

P1339 [USACO09OCT]热浪Heat Wave

题解 P1339 [USACO09OCT]热浪Heat Wave

洛谷—— P1339 [USACO09OCT]热浪Heat Wave

P1339 [USACO09OCT]热浪Heat Wave