仍然是多边形与线段求交(不规范),然后加最短路。
边权:如果相交则inf,如果不相交则其直线距离。
不知道为什么错,想出了几个脑残图形,都对了。
对应的测试数据:
1 0 1 4 6 0 0 1 1 2 0 2 4 1 3 0 4 0 0 5 4 7 1 0 5 2 5 4 3 4 1 4 -2 2 -1 0 0 0 2 6 8 2 0 4 2 4 4 2 6 0 4 2 4 0 2 2 2 0 0 5 0 4 1 0 2 0 3 0 4 0 0 0 2 2 9 2 0 3 0 5 0 5 1 4 1 3 2 1 1 2 1 3 1
#include <iostream> #include <math.h> #include <stdio.h> #include <stdlib.h> #define MAXN 1000 #define offset 10000 #define eps 1e-8 #define zero(x) (((x)>0?(x):-(x))<eps) #define _sign(x) ((x)>eps?1:((x)<-eps?2:0)) #define MAXMAT 200 #define inf 1<<30 #define elem_t double using namespace std; struct point { double x, y; }; struct line { point a, b; }; double xmult(point p1, point p2, point p0) { return (p1.x - p0.x)*(p2.y - p0.y) - (p2.x - p0.x)*(p1.y - p0.y); } double distance_(point p1, point p2) { return sqrt((p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y)); } bool opposite_side(point p1, point p2, line l) { return xmult(l.a, p1, l.b)*xmult(l.a, p2, l.b) < -eps; } bool intersect_ex(line u, line v) { return opposite_side(u.a, u.b, v) && opposite_side(v.a, v.b, u); } int insidepoly(int n, point *p, point a, point b) { for (int i = 0; i <= n; i++) { for (int j = 0; j <= n; j++) { line temp, temp2; temp.a = p[i], temp.b = p[j]; temp2.a = a, temp2.b = b; if (intersect_ex(temp, temp2)) { return 1; } } } return 0; } int bellman_ford(int n, elem_t mat [][MAXMAT], int s, elem_t* min, int* pre){ int v[MAXMAT], i, j, k, tag; for (i = 0; i < n; i++) min[i] = inf, v[i] = 0, pre[i] = -1; for (min[s] = 0, j = 0; j < n; j++){ for (k = -1, i = 0; i < n; i++) if (!v[i] && (k == -1 || min[i] < min[k])) k = i; for (v[k] = 1, i = 0; i < n; i++) if (!v[i] && mat[k][i] >= 0 && min[k] + mat[k][i] < min[i]) min[i] = min[k] + mat[pre[i] = k][i]; } for (tag = 1, j = 0; tag && j <= n; j++) for (tag = i = 0; i < n; i++) for (k = 0; k < n; k++) if (min[k] + mat[k][i] < min[i]) min[i] = min[k] + mat[pre[i] = k][i], tag = 1; return j <= n; } int main() { point a, b; cin >> a.x >> a.y >> b.x >> b.y; int n; cin >> n; point p[110]; p[0] = a; for (int i = 1; i <= n; i++) { cin >> p[i].x >> p[i].y; } p[++n] = b; //for (int i = 0; i <= n; i++) { //cout << "No " << i << " point is " << p[i].x << ' ' << p[i].y << endl; } elem_t mat[MAXMAT][MAXMAT]; for (int i = 0; i <= n; i++) { for (int j = 0; j <= n; j++) { if (insidepoly(n, p, p[i], p[j])) { mat[i][j] = inf; //cout << i << ' ' << j << ' ' << mat[i][j] << endl; } else { mat[i][j] = distance_(p[i], p[j]); //cout << i << ' ' << j << ' ' << mat[i][j] << endl; } } } elem_t min[MAXMAT]; int pre[MAXMAT]; bellman_ford(n+1, mat, 0, min, pre); /*for (int i = 0; i <= n; i++) { cout << i << ' ' << min[i] << endl; }*/ printf("%.4lf\n", min[n]); //system("pause"); }
凸包版本
#include <iostream> #include <stdio.h> #include <math.h> #include <stdlib.h> #define MAXN 1000 #define offset 10000 #define eps 1e-8 #define zero(x) (((x)>0?(x):-(x))<eps) #define _sign(x) ((x)>eps?1:((x)<-eps?2:0)) #define MAXMAT 200 #define inf 1000000000 typedef double elem_t; struct point{ double x, y; }; struct line{ point a, b; }; double xmult(point p1, point p2, point p0){ return (p1.x - p0.x)*(p2.y - p0.y) - (p2.x - p0.x)*(p1.y - p0.y); } double distance_(point p1, point p2) { return sqrt((p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y)); } bool opposite_side(point p1, point p2, line l) { return xmult(l.a, p1, l.b)*xmult(l.a, p2, l.b) < -eps; } bool intersect_ex(line u, line v) { return opposite_side(u.a, u.b, v) && opposite_side(v.a, v.b, u); } int insidepoly(int n, point *p, point a, point b) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { line temp, temp2; temp.a = p[i], temp.b = p[j]; temp2.a = a, temp2.b = b; if (intersect_ex(temp, temp2)) { return 1; } } } return 0; } //graham算法顺时针构造包含所有共线点的凸包,O(nlogn) point p1, p2; int graham_cp(const void* a, const void* b){ double ret = xmult(*((point*) a), *((point*) b), p1); return zero(ret) ? (xmult(*((point*) a), *((point*) b), p2) > 0 ? 1 : -1) : (ret > 0 ? 1 : -1); } void _graham(int n, point* p, int& s, point* ch){ int i, k = 0; for (p1 = p2 = p[0], i = 1; i<n; p2.x += p[i].x, p2.y += p[i].y, i++) if (p1.y - p[i].y>eps || (zero(p1.y - p[i].y) && p1.x > p[i].x)) p1 = p[k = i]; p2.x /= n, p2.y /= n; p[k] = p[0], p[0] = p1; qsort(p + 1, n - 1, sizeof(point), graham_cp); for (ch[0] = p[0], ch[1] = p[1], ch[2] = p[2], s = i = 3; i < n; ch[s++] = p[i++]) for (; s>2 && xmult(ch[s - 2], p[i], ch[s - 1]) < -eps; s--); } int wipesame_cp(const void *a, const void *b) { if ((*(point *) a).y < (*(point *) b).y - eps) return -1; else if ((*(point *) a).y > (*(point *) b).y + eps) return 1; else if ((*(point *) a).x < (*(point *) b).x - eps) return -1; else if ((*(point *) a).x > (*(point *) b).x + eps) return 1; else return 0; } int _wipesame(point * p, int n) { int i, k; qsort(p, n, sizeof(point), wipesame_cp); for (k = i = 1; i < n; i++) if (wipesame_cp(p + i, p + i - 1) != 0) p[k++] = p[i]; return k; } //构造凸包接口函数,传入原始点集大小n,点集p(p原有顺序被打乱!) //返回凸包大小,凸包的点在convex中 //参数maxsize为1包含共线点,为0不包含共线点,缺省为1 //参数clockwise为1顺时针构造,为0逆时针构造,缺省为1 //在输入仅有若干共线点时算法不稳定,可能有此类情况请另行处理! int graham(int n, point* p, point* convex, int maxsize = 1, int dir = 1) { point* temp = new point[n]; int s, i; n = _wipesame(p, n); _graham(n, p, s, temp); for (convex[0] = temp[0], n = 1, i = (dir ? 1 : (s - 1)); dir ? (i < s) : i; i += (dir ? 1 : -1)) if (maxsize || !zero(xmult(temp[i - 1], temp[i], temp[(i + 1)%s]))) convex[n++] = temp[i]; delete []temp; return n; } int bellman_ford(int n, elem_t mat [][MAXMAT], int s, elem_t* min, int* pre){ int v[MAXMAT], i, j, k, tag; for (i = 0; i < n; i++) min[i] = inf, v[i] = 0, pre[i] = -1; for (min[s] = 0, j = 0; j < n; j++){ for (k = -1, i = 0; i < n; i++) if (!v[i] && (k == -1 || min[i] < min[k])) k = i; for (v[k] = 1, i = 0; i < n; i++) if (!v[i] && mat[k][i] >= 0 && min[k] + mat[k][i] < min[i]) min[i] = min[k] + mat[pre[i] = k][i]; } for (tag = 1, j = 0; tag && j <= n; j++) for (tag = i = 0; i < n; i++) for (k = 0; k < n; k++) if (min[k] + mat[k][i] < min[i]) min[i] = min[k] + mat[pre[i] = k][i], tag = 1; return j <= n; } int main() { point a, b; std::cin >> a.x >> a.y >> b.x >> b.y; int n; std::cin >> n; point p[MAXMAT]; p[0] = a; for (int i = 1; i <= n; i++) { std::cin >> p[i].x >> p[i].y; } p[++n] = b; point convex[MAXMAT]; int convexsize = graham(n+1, p, convex, 1, 0); convex[convexsize++] = b; /*for (int i = 0; i < convexsize; i++) { std::cout <<"no." <<i << " point is "<<convex[i].x << ' ' << convex[i].y << std::endl; } for (int i = 0; i <= n; i++) { std::cout << "no." << i << " point is " << p[i].x << ' ' << p[i].y << std::endl; } std::cout << convexsize << std::endl; std::cout << n << std::endl;*/ elem_t mat[MAXMAT][MAXMAT]; for (int i = 0; i < convexsize; i++) { for (int j = 0; j < convexsize; j++) { if (insidepoly(convexsize, convex, convex[i], convex[j])) { mat[i][j] = inf; ///std::cout << i << ' ' << j << ' ' << mat[i][j] << std::endl; } else if (i == j) { mat[i][j] = 0.0; ///std::cout << i << ' ' << j << ' ' << mat[i][j] << std::endl; } else { mat[i][j] = distance_(convex[i], convex[j]); ///std::cout << i << ' ' << j << ' ' << mat[i][j] << std::endl; } } } /* for (int i = 0; i < convexsize; i++) { for (int j = 0; j < convexsize; j++) { std::cout << i << ' ' << j << ' ' << mat[i][j] << std::endl; } }*/ elem_t min[MAXN]; int pre[MAXN]; int iii = bellman_ford(convexsize, mat, 0, min, pre); /*for (int i = 0; i < convexsize; i++) { std::cout << min[i] << std::endl; } for (int i = 1; i < 10; i++) { std::cout << pre[i] << '\t'; } printf("\n");*/ printf("%.4f\n", min[convexsize - 1]); //system("pause"); }