CSU 过了 TOJ超时了 先记一下
#include <cstdio> #include <cstring> #include <queue> using namespace std; int a[100]; bool b[100010]; int c[100010]; int d[100010]; int x, y; int cas = 1; struct node { int x, t, tt; node(){} node(int x, int t, int tt) : x(x), t(t), tt(tt){} bool operator < (const node& rhs) const { if(t != rhs.t) return t > rhs.t; return tt > rhs.tt; } }; void BFS() { memset(b, false, sizeof(b)); priority_queue <node> Q; Q.push(node(x, 0, 0)); while(!Q.empty()) { node u = Q.top(); Q.pop(); //printf("%d\n", u.x); if(u.x == y) { printf("Case %d: %d %d\n", cas++, u.t, u.tt); return; } int sum = u.x; for(int i = 0; i < 30; i++) { int s = -1; if(i < 10) s = sum*10 + i; else if(i < 20) s = sum + (i-10); else if(i != 21) s = sum * (i-20); if(s < 0 || s > y) continue; if(!b[s] || c[s] > u.t + a[i] || (c[s] == u.t + a[i] && d[s] > u.tt+1)) { b[s] = true; c[s] = u.t+a[i]; d[s] = u.tt+1; Q.push(node(s, u.t+a[i], u.tt+1)); } } } } int main() { while(scanf("%d %d", &x, &y) != EOF) { for(int i = 0; i < 30; i++) scanf("%d", &a[i]); BFS(); } return 0; }
湖南省第九届大学生程序设计竞赛,布布扣,bubuko.com
原文地址:http://blog.csdn.net/u011686226/article/details/26163357