您的位置:首页 > 其它

BZOJ1975【左偏树】

2015-07-12 11:35 357 查看
卡Priority_queue的内存简直是不忍直视.

/* I will wait for you */

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <string>
#define make make_pair
#define fi first
#define se second

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef map<int, int> mii;

const int maxn = 200010;
const int maxm = 1010;
const int maxs = 26;
const int inf = 0x3f3f3f3f;
const int P = 1000000007;
const double eps = 1e-6;

inline ll read()
{
ll x = 0, f = 1; char ch = getchar();
while (ch < '0' || ch > '9')
f = (ch == '-' ? -1 : 1), ch = getchar();
while (ch >= '0' && ch <= '9')
x = x * 10 + ch - '0', ch = getchar();
return x * f;
}

typedef pair<double, pair<int, double> > Pii;

struct Heap
{
Pii val; int dis, size; Heap *ls, *rs;
} *null;

void init()
{
null = new Heap();
null -> val = make(1e12, make(0, 1e12));
null -> dis = -1, null -> size = 0;
null -> ls = null -> rs = null;
}

Heap *marge(Heap *a, Heap *b)
{
if (a == null) return b;
if (b == null) return a;

if (a -> val > b -> val) swap(a, b);
a -> rs = marge(a -> rs, b);

if (a -> ls -> dis < a -> rs -> dis)
swap(a -> ls, a -> rs);
a -> dis = a -> ls -> dis + 1;
a -> size = a -> ls -> size + a -> rs -> size;
return a;
}

struct _Heap
{
Heap *Q;
void init() {
Q = null;
}
bool empty() {
return Q -> size == 0;
}
Pii top() {
return Q -> val;
}
void pop() {
Heap* _del = Q;
Q = marge(Q -> ls, Q -> rs), delete(_del);
}
void push(Pii a) {
Heap *_Q = new Heap();
_Q -> val = a, _Q -> dis = 0, _Q -> size = 1;
_Q -> ls = _Q -> rs = null, Q = marge(Q, _Q);
}
} q;

struct edge
{
int v; double w; int next;
} e[maxn], _e[maxn];

int n, m, cnt, _cnt, ans = -1, head[maxn], _head[maxn], vis[maxn];
double E, h[maxn];

void insert(int u, int v, double w)
{
e[cnt] = (edge) {v, w, head[u]}, head[u] = cnt++;
}

void _insert(int u, int v, double w)
{
_e[_cnt] = (edge) {v, w, _head[u]}, _head[u] = _cnt++;
}

void spfa()
{
for (int i = 1; i <= n; i++)
h[i] = i == n ? 0 : 1e12;
queue<int> _q; _q.push(n);

while (!_q.empty()) {
int u = _q.front();
_q.pop(), vis[u] = 0;

for (int i = _head[u]; ~i; i = _e[i].next) {
int v = _e[i].v;
if (h[v] > h[u] + _e[i].w) {
h[v] = h[u] + _e[i].w;
if (!vis[v]) vis[v] = 1, _q.push(v);
}
}
}
}

void Astar()
{
q.init(), q.push(make(h[1], make(1, 0)));

while (!q.empty() && E >= -eps) {
Pii sta = q.top(); q.pop();

int u = sta.se.fi; double w = sta.se.se;
if (u == n) ans += 1, E -= w;

for (int i = head[u]; ~i; i = e[i].next) {
int v = e[i].v; double _w = w + e[i].w;
q.push(make(_w + h[v], make(v, _w)));
}
}
}

int main()
{
scanf("%d%d%lf", &n, &m, &E);

memset(head, -1, sizeof head);
memset(_head, -1, sizeof _head);
for (int i = 1; i <= m; i++) {
int u, v; double w;
scanf("%d%d%lf", &u, &v, &w);
insert(u, v, w), _insert(v, u, w);
}

init(), spfa(), Astar();
printf("%d\n", ans);

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: