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 |
有向无环图,直接记忆化搜索,最后如果负的话就输出负的,不用取绝对值。这儿WA了一次In Reply To:死活不知道错在哪儿 Posted by:HeartRush at 2007-07-11 12:33:47 > #include<stdio.h> > #include<string.h> > #define maxn 100005 > int n,m; > int v[maxn]; > struct node > {int vex; > node * next; > }; > node * side[maxn]; > > int que[maxn]; > int ind[maxn]; > int outd[maxn]; > int res[maxn]; > int mark[maxn]; > > int max(int a,int b) > {return a>b?a:b; > } > void bfs() > { > int closed=-1,open=-1; > memset(mark,0,sizeof(mark)); > int i,j; > for(j=1;j<=n;j++) > res[j]=v[j]; > > for(i=1;i<=n;i++) > if(outd[i]==0){ > closed++; > que[closed]=i; > mark[i]=1; > } > while(open<closed) > { > open++; > j=que[open]; > node *p=side[j]; > while(p!=NULL) > { > i=p->vex; > res[i]=max(res[i],res[j]+v[i]); > if(!mark[i]){ > closed++; > que[closed]=i; > mark[i]=1; > } > p=p->next; > } > } > } > > int main() > { > //freopen("file.in","r",stdin); > > int i,j,k; > > while(scanf("%d%d",&n,&m)!=EOF) > { > for(i=1;i<=n;i++){ > scanf("%d",&v[i]); > } > memset(ind,0,sizeof(ind)); > memset(outd,0,sizeof(outd)); > for(i=1;i<=n;i++) { > side[i]=NULL; > } > for(k=0;k<m;k++) > { > scanf("%d%d",&i,&j); > ind[j]++; > outd[i]++; > node *q; > q=new node; > q->vex=i; > q->next=side[j]; > side[j]=q; > } > > int best=-0x7fffffff; > bfs(); > for(k=1;k<=n;k++) > if(ind[k]==0){ > best=max(best,res[k]); > } > printf("%d\n",best); > } > return 0; > } > > > > > > Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator