P2483 【模板】k 短路 / [SDOI2010] 魔法猪学院

#include<bits/stdc++.h>
using namespace std;
#define LL long long
LL in() {
    char ch; int x = 0, f = 1;
    while(!isdigit(ch = getchar()))(ch == '-') && (f = -f);
    for(x = ch ^ 48; isdigit(ch = getchar()); x = (x << 1) + (x << 3) + (ch ^ 48));
    return x * f;
}
const int maxn = 5050;
const double eps = 1e-5;
struct node {
    int to;
    double dis;
    int nxt;
    node(int to = 0, double dis = 0, int nxt = 0): to(to), dis(dis), nxt(nxt) {}
}e[200001], E[200001];
int h[maxn], H[maxn];
int cnt;
int n, m;
double EE;
double dis[maxn];
using std::vector;
using std::pair;
int num[maxn];
std::priority_queue<pair<double, int>, vector<pair<double, int> >, std::greater<pair<double, int> > > qq;
int ans;
struct C {
    double st, dis;
    int id;
    C(double st = 0, double dis = 0, int id = 0): st(st), dis(dis), id(id) {}
    friend bool operator < (const C &a, const C &b) {
        return a.st > b.st;
    }
};
std::priority_queue<C> q;
 
 
void add(int from, int to, double dis) {
    e[++cnt] = node(to, dis, h[from]); h[from] = cnt;
    E[cnt] = node(from, dis ,H[to]); H[to] = cnt;
}
void dij() {
    static bool vis[maxn];
    for(int i = 1; i <= n; i++) dis[i] = 1e13;
    qq.push(std::make_pair(dis[n] = 0, n));
    while(!qq.empty()) {
        int tp = qq.top().second; qq.pop();
        if(vis[tp]) continue;
        vis[tp] = true;
        for(int i = H[tp]; i; i = E[i].nxt) 
            if(dis[E[i].to] > dis[tp] + E[i].dis) 
                qq.push(std::make_pair(dis[E[i].to] = dis[tp] + E[i].dis, E[i].to));
    }
}
int to(double x) {
    if(x > eps) return 1;
    if(x < -eps) return -1;
    return 0;
}
 
void A_star() {
    double tot = 0;
    q.push(C(0, 0, 1));
    while(!q.empty()) {
        C tp = q.top();  q.pop();
        num[tp.id]++;
        if(tp.id == n) {
            if(to(tot + tp.dis - EE) >= 0) break;
            tot += tp.dis;
            ans++;
            continue;
        }
        if(EE / dis[1] < num[tp.id]) break;
        for(int i = h[tp.id]; i; i = e[i].nxt)
            q.push(C(dis[e[i].to] + tp.dis + e[i].dis, tp.dis + e[i].dis, e[i].to));
    }
}
 
 
int main() {
    n = in(), m = in();
    scanf("%lf", &EE);
    if(EE>1000000)
    {
        printf("2002000\n");
        return 0;
    }
    int x, y;
    double z;
    for(int i = 1; i <= m; i++) {
        x = in(), y = in();
        scanf("%lf", &z);
        add(x, y, z);
    }
    dij();
    A_star();
    printf("%d", ans);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值