【SDOI2010】魔法猪学院

题面

题解

$k$短路模板,使用$A^*$算法

代码

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
#include<bits/stdc++.h>
#define RG register
#define file(x) freopen(#x".in", "r", stdin);freopen(#x".out", "w", stdout);
#define clear(x, y) memset(x, y, sizeof(x));
using namespace std;

template<typename T = int>
inline T read()
{
T data=0, w=1;
char ch=getchar();
while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
if(ch=='-') w=-1, ch=getchar();
while(ch>='0'&&ch<='9') data=(data<<3)+(data<<1)+(ch^48), ch=getchar();
return data*w;
}

const int maxn(5010);
struct edge { int to; double dis; bool operator < (const edge &x) const { return x.dis<dis; } };
int n, m, ans, tot[maxn];
double en, dis[maxn];

vector<edge> g[maxn], r[maxn];
priority_queue<edge> q;

inline void dijkstra()
{
fill(dis+1, dis+n+1, INT_MAX);
dis[n]=0; q.push({n, 0});
while(!q.empty())
{
edge x=q.top(); q.pop();
for(auto i : r[x.to])
if(dis[i.to]>dis[x.to]+i.dis)
{
dis[i.to]=dis[x.to]+i.dis;
q.push({i.to, dis[i.to]});
}
}
}

inline void astar()
{
double I=en/dis[1];
q.push({1, dis[1]});
while(!q.empty())
{
edge x=q.top(); q.pop();
if(x.dis>en) return;
if(++tot[x.to]>I) continue;
if(x.to==n) { en-=x.dis; ans++; continue; }
for(auto i : g[x.to]) q.push({i.to, x.dis-dis[x.to]+i.dis+dis[i.to]});
}
}

int a, b; double c;

int main()
{
scanf("%d%d%lf", &n, &m, &en);
while(m--)
{
scanf("%d%d%lf", &a, &b, &c);
g[a].push_back({b, c});
r[b].push_back({a, c});
}
dijkstra(); astar();
printf("%d\n", ans);
return 0;
}
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×