[uva] 216 - Getting in Line

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=152

简单回溯, 这代码被我写的, 各种vector, map, set, 离了STL就活不下去的样子...

代码有如下几点需要注意:

1.回溯时判重用set, 自定义类型用set的时候需要重载"<"号运算符, 另外用set判重显然不如hash高效.

2.记录解的路径使用了father数组, 这是个常用技巧

  1 #include <cstdio>
  2 #include <vector>
  3 #include <algorithm>
  4 #include <cmath>
  5 #include <climits>
  6 #include <cstring>
  7 #include <set>
  8 #include <map>
  9 using namespace std;
 10 
 11 #define MAX_NUM 10
 12 
 13 typedef struct _Point{
 14     int x;
 15     int y;
 16 }Point;
 17 
 18 struct cmp {
 19     bool operator() (Point a, Point b) {
 20         return memcmp(&a, &b, sizeof(Point)) < 0;
 21     }
 22 };
 23 
 24 int n = 0;
 25 vector<Point> vec;
 26 vector<Point> re;
 27 set<Point, cmp> used;
 28 int father[MAX_NUM] = {0};
 29 map<int, vector<Point> > ma;
 30 
 31 float minDinstance = INT_MAX;
 32 
 33 float dis(const Point& a, const Point& b) {
 34     return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)) + 16;
 35 }
 36 
 37 void solve(int cur, float curdis) {
 38     if (cur == n) {
 39         if (curdis < minDinstance) {
 40             minDinstance = curdis;
 41             vector<Point> v;
 42             for (int i = 0; i < n; ++i) {
 43                 Point point;
 44                 point.x = vec[father[i]].x;
 45                 point.y = vec[father[i]].y;
 46                 v.push_back(point);
 47             }
 48             ma[minDinstance] = v;
 49         }
 50         return;
 51     }
 52     for (unsigned int i = 0; i < n; ++i) if (used.find(vec[i]) == used.end()) {
 53         re[cur] = vec[i];
 54         float d = 0.0f;
 55         if (cur > 0) {
 56             d = dis(re[cur - 1], re[cur]);
 57         }
 58         if (d >= minDinstance) {
 59             continue;
 60         }
 61         father[cur] = i;
 62         used.insert(vec[i]);
 63         solve(cur + 1, curdis + d);
 64         used.erase(vec[i]);
 65     }
 66 }
 67 
 68 void printRe(int index) {
 69     printf("**********************************************************\n");
 70     printf("Network #%d\n", index);
 71     vector<Point>& v = ma[minDinstance];
 72     for (unsigned int i = 1; i < v.size(); ++i) {
 73         printf("Cable requirement to connect (%d,%d) to (%d,%d) is %.2f feet.\n",
 74                v[i - 1].x, v[i - 1].y, v[i].x, v[i].y, dis(v[i - 1], v[i]));
 75     }
 76     printf("Number of feet of cable required is %.2f.\n",minDinstance);
 77 }
 78 
 79 int main(int argc, const char * argv[])
 80 {
 81     int index = 1;
 82     while (true) {
 83         scanf("%d", &n);
 84         if (n == 0) {
 85             break;
 86         }
 87         vec.clear();
 88         for (int i = 0; i < n; ++i) {
 89             Point point;
 90             scanf("%d%d", &point.x, &point.y);
 91             vec.push_back(point);
 92         }
 93         re.clear();
 94         re.resize(n);
 95         minDinstance = INT_MAX;
 96         used.clear();
 97         ma.clear();
 98         solve(0, 0.0f);
 99         printRe(index++);
100     }
101     return 0;
102 }

 

posted @ 2013-12-15 23:59  NextLife  阅读(283)  评论(0)    收藏  举报