「SDOI2010」魔法猪学院 - $k$短路(可持久化可并堆) | Bill Yang's Blog

「SDOI2010」魔法猪学院 - $k$短路(可持久化可并堆)

题目大意

    求$1\rightarrow n$的路径中,选出最多多少条路径,使得它们权值和小于$E$。


题目分析

$k$短路模板题。
学习笔记传送门


代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include<algorithm>
#include<iostream>
#include<iomanip>
#include<cstring>
#include<cstdlib>
#include<climits>
#include<vector>
#include<cstdio>
#include<cmath>
#include<queue>
using namespace std;
inline const int Get_Int() {
int num=0,bj=1;
char x=getchar();
while(x<'0'||x>'9') {
if(x=='-')bj=-1;
x=getchar();
}
while(x>='0'&&x<='9') {
num=num*10+x-'0';
x=getchar();
}
return num*bj;
}
const int maxn=5005;
const double eps=1e-8;
int dcmp(double x) {
if(fabs(x)<=eps)return 0;
if(x>eps)return 1;
return -1;
}
struct Tree {
Tree *ls,*rs;
int dist;
double val;
int back;
Tree(int d=0,double v=0,int b=0):dist(d),val(v),back(b) {
ls=rs=NULL;
}
};
struct Left_Side_Tree {
Tree* merge(Tree *x,Tree *y) {
if(!x||!y)return x?x:y;
if(x->val>y->val)swap(x,y);
Tree *New=new Tree;
*New=*x;
New->rs=merge(New->rs,y);
if(!New->ls||New->rs->dist>New->ls->dist)swap(New->ls,New->rs);
if(New->rs)New->dist=New->rs->dist+1;
return New;
}
Tree* push(Tree *x,double v,int b) {
Tree *New=new Tree(0,v,b);
return merge(x,New);
}
} heap;
Tree *root[maxn];
struct Edge {
int from,to;
double dist;
};
int n,m,father[maxn],father_e[maxn];
double energy,dist[maxn];
bool vst[maxn];
vector<Edge> edges,fedges;
vector<int> G[maxn],fG[maxn];
void AddEdge(int x,int y,double v) {
edges.push_back((Edge) {x,y,v});
G[x].push_back(edges.size()-1);
fedges.push_back((Edge) {y,x,v});
fG[y].push_back(fedges.size()-1);
}
#define pii pair<double,int>
#define mp make_pair
void Dijkstra() {
for(int i=1; i<=n; i++)dist[i]=1e18,vst[i]=0;
dist[n]=0;
father_e[n]=-1;
priority_queue<pii,vector<pii>,greater<pii> > Q;
Q.push(mp(0,n));
while(!Q.empty()) {
int Now=Q.top().second;
Q.pop();
if(vst[Now])continue;
vst[Now]=1;
for(int id:fG[Now]) {
Edge& e=fedges[id];
int Next=e.to;
if(dcmp(dist[Next]-dist[Now]-e.dist)>0) {
dist[Next]=dist[Now]+e.dist;
father[Next]=Now;
father_e[Next]=id; //重边!!!
Q.push(mp(dist[Next],Next));
}
}
}
}
void Dfs(int Now) {
if(father[Now])root[Now]=root[father[Now]];
for(int id:G[Now]) {
if(id==father_e[Now])continue;
Edge& e=edges[id];
int Next=e.to;
root[Now]=heap.push(root[Now],e.dist+dist[Next]-dist[Now],Next);
}
for(int id:fG[Now]) {
Edge& e=fedges[id];
int Next=e.to;
if(id==father_e[Next])Dfs(Next);
}
}
#undef pii
#define pii pair<double,Tree *>
int main() {
n=Get_Int();
m=Get_Int();
scanf("%lf",&energy);
for(int i=1; i<=m; i++) {
int x=Get_Int(),y=Get_Int();
double v;
scanf("%lf",&v);
AddEdge(x,y,v);
}
Dijkstra();
int ans=0;
if(dcmp(energy-dist[1])>=0) {
ans++;
energy-=dist[1];
} else {
puts("0");
return 0;
}
Dfs(n);
priority_queue<pii,vector<pii>,greater<pii> > Q;
if(root[1])Q.push(mp(dist[1]+root[1]->val,root[1]));
while(!Q.empty()) {
pii Now=Q.top();
double len=Now.first;
Tree *rt=Now.second;
Q.pop();
if(dcmp(energy-len)>=0) {
ans++;
energy-=len;
} else break;
Tree *next=root[rt->back],*ls=rt->ls,*rs=rt->rs;
if(next)Q.push(mp(len+next->val,next));
if(ls)Q.push(mp(len+ls->val-rt->val,ls));
if(rs)Q.push(mp(len+rs->val-rt->val,rs));
}
printf("%d\n",ans);
return 0;
}
本篇文章有用吗?有用还不快点击!
0%