您的位置:首页 > 其它

poj 1062 昂贵的聘礼

2017-01-18 22:53 267 查看
前几天听到小弟说,真是的,没钱还娶什么媳妇呀。。。今天来看这道题,,范围判断真是有点坑,娶媳妇不容易呀。

这个问题可以想成酋长的约定是终点。

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
using namespace std;
#define maxn 205
#define INF 0x3f3f3f3f
struct edge
{
int to,mon,next;
int dj;
}e[maxn*maxn];
struct node
{
int to,mon,dj;
bool operator < (const node &t)const{
return t.to>to;
}
};
priority_queue <node> Q;
int head[maxn];
int dis[maxn];
int ans[maxn*maxn];
int main()
{
int M,N;
scanf("%d %d",&M,&N);
int h=0;
memset(head,-1,sizeof(head));
memset(e,0,sizeof(e));
memset(ans,0,sizeof(ans));
int qiuzhang =0;
for(int i=1;i<=N;i++)
{
int p,l,x;
scanf("%d %d %d",&p,&l,&x);
if(i==1)
qiuzhang=l;
for(int j=1;j<=x;j++)
{
int t,v;
scanf("%d %d",&t,&v);
e[h].to=i;
e[h].mon=v;
e[h].dj=l;
e[h].next=head[t];
head[t]=h;
h++;
}
e[h].to=i;
e[h].mon=p;
e[h].dj=l;
e[h].next=head[0];
head[0]=h;
h++;
}
int r=0;
for(int j=qiuzhang-M;j<=qiuzhang;j++)//判断范围。。这一步很重要
{
memset(dis,INF,sizeof(dis));
dis[0]=0;
Q.push(node{0,0,qiuzhang});
while(!Q.empty())
{
node t=Q.top();
Q.pop();
if(dis[t.to]!=t.mon)
continue;
for(int i=head[t.to];i>-1;i=e[i].next)
{
edge g=e[i];
int flag=0;
if(g.dj>=j&&g.dj<=j+M)
{
flag=1;
}
if(flag&&dis[g.to]>t.mon+g.mon)
{
dis[g.to]=t.mon+g.mon;
Q.push(node{g.to,dis[g.to],g.dj});
}
}
4000

ans[r++]=dis[1];
}
}
sort(ans,ans+r);
cout<<ans[0]<<endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: