zoukankan      html  css  js  c++  java
  • bzoj 1975: [Sdoi2010]魔法猪学院 [k短路]

    1975: [Sdoi2010]魔法猪学院

    裸题...

    被double坑死了

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <queue>
    using namespace std;
    typedef long long ll;
    #define pdi pair<double, int>
    #define fir first
    #define sec second
    const int N = 5005, M = 2e5+5;
    inline int read(){
        char c=getchar(); int x=0,f=1;
        while(c<'0'||c>'9') {if(c=='-')f=-1;c=getchar();}
        while(c>='0'&&c<='9') {x=x*10+c-'0';c=getchar();}
        return x*f;
    }
     
    int n, m, u, v, s, t; double en, w;
     
    namespace I {
        struct edge {int v, ne; double w;} e[M];
        int cnt, h[N];
        inline void ins(int u, int v, double w) {
            e[++cnt] = (edge){v, h[u], w}; h[u] = cnt;
        }
        priority_queue<pdi, vector<pdi>, greater<pdi> > q;
        int vis[N];
        void dij(int s, int t, double *d) {
            for(int i=1; i<=n; i++) d[i] = 1e15, vis[i] = 0;
            d[s] = 0; q.push(make_pair(0, s));
            while(!q.empty()) {
                int u = q.top().sec; q.pop();
                if(vis[u]) continue; vis[u] = 1;
                for(int i=h[u];i;i=e[i].ne) {
                    int v = e[i].v;
                    if(d[v] > d[u] + e[i].w) 
                        d[v] = d[u] + e[i].w, q.push(make_pair(d[v], v));
                }
            }
        }
    }
     
    struct edge {int v, ne; double w;} e[M];
    int cnt, h[N];
    inline void ins(int u, int v, double w) {
        e[++cnt] = (edge){v, h[u], w}; h[u] = cnt;
    }
    double d_t[N]; int vis[N];
    priority_queue<pdi, vector<pdi>, greater<pdi> > q;
    void a_star(int s, int t) {
        I::dij(t, s, d_t);
        q.push(make_pair(d_t[s], s));
        while(!q.empty()) {
            int u = q.top().sec; double d = q.top().fir; q.pop();
            if(u == t) if((en -= d) < 0) return;
            vis[u]++;
            for(int i=h[u];i;i=e[i].ne) {
                int v = e[i].v;
                q.push(make_pair(d - d_t[u] + d_t[v] + e[i].w, v));
            }
        }
    }
    int main() {
        freopen("in", "r", stdin);
        n=read(); m=read(); scanf("%lf", &en); s=1; t=n;
        for(int i=1; i<=m; i++) u=read(), v=read(), scanf("%lf", &w), I::ins(v, u, w), ins(u, v, w);
        a_star(s, t);
        printf("%d
    ", vis[t]);
    }
    
    
  • 相关阅读:
    [leetcode] Copy List with Random Pointer
    [leetcode] Single Number II
    团队项目NABCD模型的需求分析
    团队项目的分工及绩效评估方法
    软件工程结对作业实验报告
    Java jdbc链接 mySQL 写的crud
    从高版本JDK换成低版本JDK报错Unsupported major.minor version 52.0的解决方案
    红黑树简介
    再学HTML之一
    Java script 的dom编程
  • 原文地址:https://www.cnblogs.com/candy99/p/6793141.html
Copyright © 2011-2022 走看看