| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
这个猥琐的dp,我用暴长的spfa终于过了。回馈社会,给大家一组我发现bug的数据还有我的程序。//自己调了2个晚自习,不容易呀。
1
8 50 99 50
0 100 88
-50 20 77
40 100 77
-200 100 66
-300 100 50
150 300 50
-999 400 30
-1000 1000 25
ans=1049
//一下是c++的code:
/*
ID: talenth1
PROG: p1661
LANG: C++
*/
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cmath>
#define maxn 2000
#define oo 99999999
#define left(x) (2*x-1)
#define right(x) (2*x)
using namespace std;
struct Record{
int x1,x2,h;
};
struct Node{
int v,w;
Node* next;
};
int t,n,x,y,lim,n1,d[maxn*2],que[maxn*2];
bool vis[maxn*2];
Record a[maxn];
Node *list[maxn*2];
void datain()
{
scanf("%d%d%d%d",&n,&x,&y,&lim);
for(int i=1;i<=n;i++)
scanf("%d%d%d",&a[i].x1,&a[i].x2,&a[i].h);
}
int MyCmp(const void *x1,const void *x2)
{
Record *xa,*xb;
xa=(Record*)x1;
xb=(Record*)x2;
return(xb->h-xa->h);
}
void insert(int tp,int xp,int yp,int ta,int xa,int ya)
{
Node *p;
p=new(Node);
p->v=ta;
p->w=abs(yp-ya)+abs(xp-xa);
p->next=list[tp];
list[tp]=p;
}
bool relax(int u,int v,int w)
{
if(d[u]+w<d[v]){
d[v]=d[u]+w;
return true;
}
else return false;
}
void spfa(int v0)
{
int head=-1,tail=0;
memset(vis,0,sizeof(vis));
vis[v0]=true;
for(int i=0;i<=n1;i++)
d[i]=oo;
d[v0]=0;
que[0]=v0;
while(head!=tail){
head++;if(head>n1)head=0;
int vi=que[head];
Node* p=list[vi];
while(p!=NULL){
if(relax(vi,p->v,p->w))
if(!vis[p->v]){
tail++;if(tail>n1)tail=0;
que[tail]=p->v;
vis[p->v]=true;
}
p=p->next;
}
vis[vi]=false;
}
}
void destory()
{
Node *p,*np;
for(int i=0;i<=n1;i++){
p=list[i];
while(p!=NULL){
np=p->next;
delete(p);
p=np;
}
}
}
void work()
{
n1=2*n+1;
qsort(&a[1],n,sizeof(Record),MyCmp);
for(int i=0;i<=n1;i++)
list[i]=NULL;
for(int i=1;i<=n;i++){
bool sg1=true,sg2=true;
for(int j=i+1;j<=n;j++){
if(a[i].h-a[j].h>lim)break;
if(a[i].x1>=a[j].x1&&a[i].x1<=a[j].x2&&sg1){
insert(left(i),a[i].x1,a[i].h,left(j),a[j].x1,a[j].h);
insert(left(i),a[i].x1,a[i].h,right(j),a[j].x2,a[j].h);
if(a[i].h!=a[j].h)sg1=false;
}
if(a[i].x2>=a[j].x1&&a[i].x2<=a[j].x2&&sg2){
insert(right(i),a[i].x2,a[i].h,left(j),a[j].x1,a[j].h);
insert(right(i),a[i].x2,a[i].h,right(j),a[j].x2,a[j].h);
if(a[i].h!=a[j].h)sg2=false;
}
if(!sg1&&!sg2)break;
}
if(sg1&&a[i].h<=lim)insert(left(i),a[i].x1,a[i].h,2*n+1,a[i].x1,0);
if(sg2&&a[i].h<=lim)insert(right(i),a[i].x2,a[i].h,2*n+1,a[i].x2,0);
}
bool sg=true;
for(int i=1;i<=n;i++){
if(y-a[i].h>lim)break;
if(x>=a[i].x1&&x<=a[i].x2&&y>=a[i].h){
insert(0,x,y,left(i),a[i].x1,a[i].h);
insert(0,x,y,right(i),a[i].x2,a[i].h);
sg=false;
break;
}
}
if(sg&&y<=lim){
printf("%d",y);
return;
}
spfa(0);
printf("%d",d[2*n+1]);
destory();
}
int main()
{
scanf("%d",&t);
for(int i=1;i<=t;i++){
datain();
work();
printf("\n");
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator