标签:des style blog color io os ar strong for
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 37139 | Accepted: 10721 |
Description
Input
Output
Sample Input
1 4 10000 3 2 2 8000 3 5000 1000 2 1 4 200 3000 2 1 4 200 50 2 0
Sample Output
5250
初看这道题时我很激动,终于看见一道中文题了。。。略加思考,必定是广搜。。然后就激动万分的敲代码,敲完后提交。。提交了一遍又一遍总是wa。。然后在dicuss中找样例,全部过了还是wa,于是看代码,发现广搜时if中多了一个条件,我的代码就带有贪心的意思了,而贪心解未必就是最优解,提交就过了。。。
代码:
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <iostream> 5 #include <queue> 6 #include <vector> 7 8 using namespace std; 9 #define N 2205 10 #define inf 999999999 11 12 struct node{ 13 int y, p, mx, mn, z; 14 }; 15 16 vector<node>ve[N]; 17 int price[N], l[N], visited[N]; 18 int n, m; 19 20 void init(){ 21 int i; 22 for(i=0;i<=n;i++){ 23 ve[i].clear(); 24 } 25 memset(visited,0,sizeof(visited)); 26 memset(price,0,sizeof(price)); 27 memset(l,0,sizeof(l)); 28 } 29 30 int bfs(){ 31 int i, j, k, u, v, ans; 32 node p, q; 33 queue<node>Q; 34 p.y=1;p.p=0;p.mx=p.mn=l[1];p.z=price[p.y]; 35 ans=p.z; 36 visited[p.y]=1; 37 Q.push(p); 38 while(!Q.empty()){ 39 p=Q.front(); 40 Q.pop(); 41 42 visited[p.y]=0; 43 for(i=0;i<ve[p.y].size();i++){ 44 q=ve[p.y][i]; 45 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去掉就会超时 46 q.z=price[q.y]+q.p+p.p; 47 ans=min(ans,q.z); 48 q.p=q.p+p.p; 49 q.mx=max(p.mx,l[q.y]); 50 q.mn=min(p.mn,l[q.y]); 51 // printf(" %d %d %d %d\n",p.y,q.y,q.mx,q.mn); 52 Q.push(q); 53 54 } 55 } 56 } 57 58 59 //cout<<endl; 60 return ans; 61 } 62 63 64 main() 65 { 66 int i, j, k, x, y, z; 67 while(scanf("%d %d",&m,&n)==2){ 68 init(); 69 node p; 70 for(i=1;i<=n;i++){ 71 scanf("%d %d %d",&price[i],&l[i],&z); 72 while(z--){ 73 scanf("%d %d",&x,&y); 74 p.y=x;p.p=y; 75 ve[i].push_back(p); 76 } 77 } 78 printf("%d\n",bfs()); 79 } 80 }
标签:des style blog color io os ar strong for
原文地址:http://www.cnblogs.com/qq1012662902/p/3966478.html