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就是超时。。附上AC代码//我错的原因是代码中多了点贪心性质,而贪心所得的结果未必是最优解 #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <queue> #include <vector> using namespace std; #define N 2205 #define inf 999999999 struct node{ int y, p, mx, mn, z; }; vector<node>ve[N]; int price[N], l[N], visited[N]; int n, m; void init(){ int i; for(i=0;i<=n;i++){ ve[i].clear(); } memset(visited,0,sizeof(visited)); memset(price,0,sizeof(price)); memset(l,0,sizeof(l)); } int bfs(){ int i, j, k, u, v, ans; node p, q; queue<node>Q; p.y=1;p.p=0;p.mx=p.mn=l[1];p.z=price[p.y]; ans=p.z; visited[p.y]=1; Q.push(p); while(!Q.empty()){ p=Q.front(); Q.pop(); visited[p.y]=0; for(i=0;i<ve[p.y].size();i++){ q=ve[p.y][i]; if((abs(l[q.y]-p.mx)<=m&&abs(l[q.y]-p.mn)<=m)&&(p.p+q.p)<=ans){//(p.q+q.p)<=ans去掉就会超时 q.z=price[q.y]+q.p+p.p; ans=min(ans,q.z); q.p=q.p+p.p; q.mx=max(p.mx,l[q.y]); q.mn=min(p.mn,l[q.y]); // printf(" %d %d %d %d\n",p.y,q.y,q.mx,q.mn); Q.push(q); } } } //cout<<endl; return ans; } main() { int i, j, k, x, y, z; while(scanf("%d %d",&m,&n)==2){ init(); node p; for(i=1;i<=n;i++){ scanf("%d %d %d",&price[i],&l[i],&z); while(z--){ scanf("%d %d",&x,&y); p.y=x;p.p=y; ve[i].push_back(p); } } printf("%d\n",bfs()); } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator