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 |
SPFA用队列超时之后,改为栈就AC了,于是突发奇想,写了个记忆化搜索dp,AC了,贴代码#include<iostream> #include<stdio.h> #include<string.h> #include<math.h> #include<vector> #include<queue> #include<stack> #include<map> int n,m; struct E { int v; int x; int next; }e[151000]; int ce; int head[31000]; void addedge(int u,int v,int x) { e[ce].v=v; e[ce].x=x; e[ce].next=head[u]; head[u]=ce++; } int d[31000]; bool used[31000]; int dp(int u) { if(used[u]) return d[u]; if(u==1) return 0; used[u]=1; d[u]=100000000; for(int i=head[u];i!=-1;i=e[i].next) { int v=e[i].v; int x=e[i].x; if(dp(v)+x<=d[u]) d[u]=dp(v)+x; } return d[u]; } int main() { freopen("3159.txt","r",stdin); scanf("%d%d",&n,&m); memset(head,-1,sizeof head); while(m--) { int a,b,c; scanf("%d%d%d",&a,&b,&c); addedge(b,a,c); } dp(n); printf("%d\n",d[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