Chinaunix首页 | 论坛 | 博客
  • 博客访问: 71295
  • 博文数量: 41
  • 博客积分: 1475
  • 博客等级: 上尉
  • 技术积分: 440
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-27 22:49
文章分类
文章存档

2012年(8)

2011年(1)

2009年(32)

我的朋友
最近访客

分类: C/C++

2012-03-29 17:10:39

题目: 

思路: (BFS and DP)
  • BFS
  • DP 
     When do BFS, there may be many nodes which have the same _sumConvValue and _sumInfoTechValue, so we can ignore those nodes whose _numCoin is not the smallest.
代码:

点击(此处)折叠或打开

  1. //
  2. // UVA: 10306
  3. // Author: xfye
  4. // Status: AC
  5. //
  6. // Brief:
  7. // BFS and DP
  8. // When do BFS, there may be many nodes which have the same
  9. // _sumConvValue and _sumInfoTechValue, so we can ignore those
  10. // nodes whose _numCoin is not the smallest.
  11. //

  12. #include <iostream>
  13. #include <list>

  14. using namespace std;

  15. #define INF 0x7fffffff

  16. struct Node
  17. {
  18.     Node()
  19.     {
  20.         _sumConvValue = 0;
  21.         _sumInfoTechValue = 0;
  22.         _numCoin = 0;
  23.     }
  24.     int _sumConvValue;
  25.     int _sumInfoTechValue;
  26.     int _numCoin;
  27. };

  28. struct CoinType
  29. {
  30.     int _convValue;
  31.     int _infoTechValue;
  32. };

  33. //
  34. // g_dp[x][y]
  35. //
  36. // x is the sum of conventinal value
  37. // y is the sum of InfoTech value
  38. // g_dp[x][y] is the number of coins
  39. //
  40. int g_dp[310][310];
  41. CoinType g_coinTypes[40];
  42. int g_numCoinTypes;
  43. int g_eModulusResult;

  44. void search()
  45. {
  46.     for (int i = 0; i < 310; i++) {
  47.         for (int j = 0; j < 310; j++) {
  48.             g_dp[i][j] = INF;
  49.         }
  50.     }
  51.     list<Node> nodeList;

  52.     Node node;
  53.     int minNumCoin = INF;
  54.     nodeList.push_back(node);

  55.     //
  56.     // do BFS
  57.     //
  58.     while (nodeList.size() > 0) {
  59.         node = nodeList.front();
  60.         nodeList.pop_front();
  61.         int eModulus = node._sumConvValue * node._sumConvValue + node._sumInfoTechValue * node._sumInfoTechValue;
  62.         if (eModulus == g_eModulusResult * g_eModulusResult) {
  63.             if (minNumCoin > node._numCoin) {
  64.                 minNumCoin = node._numCoin;
  65.             }
  66.         } else if (eModulus > g_eModulusResult * g_eModulusResult) {
  67.             continue;
  68.         }
  69.         if (node._numCoin >= minNumCoin) {
  70.             continue;
  71.         }

  72.         for (int i = 0; i < g_numCoinTypes; i++) {
  73.             Node newNode = node;
  74.             newNode._sumConvValue += g_coinTypes[i]._convValue;
  75.             newNode._sumInfoTechValue += g_coinTypes[i]._infoTechValue;
  76.             if (newNode._sumConvValue > g_eModulusResult || newNode._sumInfoTechValue > g_eModulusResult) {
  77.                 continue;
  78.             }
  79.             newNode._numCoin ++;
  80.             if (g_dp[newNode._sumConvValue][newNode._sumInfoTechValue] > newNode._numCoin) {
  81.                 g_dp[newNode._sumConvValue][newNode._sumInfoTechValue] = newNode._numCoin;
  82.                 nodeList.push_back(newNode);
  83.             }
  84.         }
  85.     }

  86.     if (minNumCoin != INF) {
  87.         cout << minNumCoin << endl;
  88.     } else {
  89.         cout << "not possible" << endl;
  90.     }
  91. }

  92. int main()
  93. {
  94.     int numCases = 0;

  95.     cin >> numCases;
  96.     for (int i = 0; i < numCases; i++) {
  97.         cin >> g_numCoinTypes >> g_eModulusResult;
  98.         for (int j = 0; j < g_numCoinTypes; j++) {
  99.             cin >> g_coinTypes[j]._convValue >> g_coinTypes[j]._infoTechValue;
  100.         }
  101.         search();
  102.     }

  103.     return 0;
  104. }
阅读(1001) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~