bzoj1975: [Sdoi2010]魔法猪学院
2017-05-11 08:28
302 查看
题解
这题就是裸的k短路啊。贪心不停地选非严格k短路直到路径的长度和大于所给的总能量。
代码
//k短路 #include <cstdio> #include <algorithm> #include <queue> #define maxn 400010 #define heap priority_queue #define finf 1e100 #define eps 1e-9 using namespace std; struct node{double f; int id;}; bool operator<(const node &a, const node &b){return a.f>b.f;} heap<node> h; int N, head[6000], to[maxn], nex[maxn], cnt, tot=1, counter; double w[maxn], dist[6000], E; bool in[6000]; void adde(int a, int b, double v){to[++tot]=b;w[tot]=v;nex[tot]=head[a];head[a]=tot;} void spfa() { queue<int> q; in =1; q.push(N); int x, p; for(int i=1;i<N;i++)dist[i]=finf; while(!q.empty()) { in[x=q.front()]=0;q.pop(); for(p=head[x];p;p=nex[p]) if(p&1 and dist[x]+w[p]<dist[to[p]]) { dist[to[p]]=dist[x]+w[p]; if(!in[to[p]])q.push(to[p]),in[to[p]]=1; } } } void work() { heap<node> h; h.push((node){dist[1],1}); node x; int p; double f; while(!h.empty()) { x=h.top(), h.pop(); if(x.id==N) { if(x.f<E+eps)cnt++, E-=x.f; else return; continue; } for(p=head[x.id];p;p=nex[p]) if(~p&1) { f=x.f-dist[x.id]+w[p]+dist[to[p]]; if(f<E+eps)h.push((node){f,to[p]}); } } } void input() { int a, b, m; double v; scanf("%d%d%lf",&N,&m,&E); for(int i=1;i<=m;i++) { scanf("%d%d%lf",&a,&b,&v); adde(a,b,v);adde(b,a,v); } } int main() { input(); spfa(); work(); printf("%d",cnt); return 0; }
相关文章推荐
- [SDOI2010][BZOJ1975] 魔法猪学院|A*|K短路
- 【BZOJ1975】[Sdoi2010]魔法猪学院 A*
- 【BZOJ】1975: [Sdoi2010]魔法猪学院
- BZOJ 1975: [Sdoi2010]魔法猪学院——K短路,A*
- bzoj 1975 [Sdoi2010]魔法猪学院
- bzoj 1975 [Sdoi2010]魔法猪学院(k短路)
- BZOJ 1975 SDOI 2010 魔法猪学院 A*求K短路
- BZOJ1975: [Sdoi2010]魔法猪学院
- BZOJ 1975 [Sdoi2010]魔法猪学院
- BZOJ 1975: [Sdoi2010]魔法猪学院 K短路 dijkstra A*
- bzoj 1975: [Sdoi2010]魔法猪学院 (k短路)
- [bzoj1975][Sdoi2010]魔法猪学院
- bzoj 1975: [Sdoi2010]魔法猪学院 [k短路]
- 【bzoj1975】【K短路】[Sdoi2010]魔法猪学院
- [A* k短路] BZOJ 1975 [Sdoi2010]魔法猪学院
- [bzoj1975][Sdoi2010]魔法猪学院 k短路 dijkstra
- 【bzoj1975】[Sdoi2010]魔法猪学院
- [BZOJ1975][SDOI2010]魔法猪学院(k短路,A*)
- 【BZOJ1975】【SDOI2010】魔法猪学院 [A*搜索]
- BZOJ 1975: [Sdoi2010]魔法猪学院