您的位置:首页 > 其它

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