思路:
每给出一个横坐标x都会对应三条线,首先我们将所有的线都输入并储存,每次取出两点,观察两点之间的连线是否与其他直线相交,若不相交,就将其之间的距离求出并赋值,这里不知道怎么判断的可以取我的前几篇博客里看一下。
可能是叉乘的模板用错了,wa了好几发,换了一个模板就过了。
数据不大,跑一遍Floyd即可。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<map>
#include<cstdio>
#include<cmath>
#include<iomanip>
#include<sstream>
#include<queue>
#define pi 3.1415926535
#define me(a,b,c) memset(a,b,sizeof c)
#define eps 0.00000001
//#define x first
//#define y second
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
typedef pair<int, int> pii;
const int N = 1e6 + 10;
int n;
double g[210][210];
struct point {
double x, y;
point() {}
point(double _x, double _y) {
x = _x; y = _y;
}
}a[210];
double cross(double x1, double y1, double x2, double y2) {
return x1 * y2 - x2 * y1;
}
int dcmp(double x) {
if (fabs(x) < eps) return 0;
if (x < 0) return -1;
else return 1;
}
struct Line {
double x1, y1, x2, y2;
Line() {}
Line(double _x1, double _y1, double _x2, double _y2) {
x1 = _x1; x2 = _x2; y1 = _y1; y2 = _y2;
}
}le[210];
double dis(point a, point b) {
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
bool check(point a, point b) {
for (int i = 1; i <= n * 3; i++) {
if (cross(le[i].x1 - a.x, le[i].y1 - a.y, b.x - a.x, b.y - a.y) *
cross(b.x - a.x, b.y - a.y, le[i].x2 - a.x, le[i].y2 - a.y) > eps
&& cross(a.x - le[i].x2, a.y - le[i].y2, le[i].x1 - le[i].x2, le[i].y1 - le[i].y2) *
cross(le[i].x1 - le[i].x2, le[i].y1 - le[i].y2, b.x - le[i].x2, b.y - le[i].y2) > eps)
return false;
}
return true;
}
void inti() {
for (int i = 0; i <= 110; i++)
for (int j = 0; j <= 110; j++) {
g[i][j] = (double)0x3f3f3f3f;
}
}
int main() {
while (cin >> n)
{
inti();
if (n == -1)return 0;
for (int i = 1; i <= n; i++) {
double x, y1, y2, y3, y4;
scanf_s("%lf%lf%lf%lf%lf", &x, &y1, &y2, &y3, &y4);
a[i * 4 - 3] = point(x, y1);
a[i * 4 - 2] = point(x, y2);
a[i * 4 - 1] = point(x, y3);
a[i * 4 ] = point(x, y4);
le[i * 3 - 2] = Line(x, 0,x, y1);
le[i * 3 - 1] = Line(x, y2, x, y3);
le[i * 3 - 0] = Line(x, y4,x, 10);
}
a[n * 4 + 1] = point(0.0, 5.0), a[n * 4 + 2] = point(10.0, 5.0);
for (int i = 1; i <= n * 4 + 2; i++)
for (int j = i+1; j <= n * 4 + 2; j++) {
if (fabs(a[i].x - a[j].x) < eps)continue;
if (check(a[i], a[j])) {
g[i][j] = g[j][i] = dis(a[i], a[j]);
}
}
for (int k = 1; k <= 4 * n + 2; k++)
for (int i = 1; i <= 4 * n + 2; i++)
for (int j = 1; j <= 4 * n + 2; j++)
g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
printf("%.2f\n", g[n * 4 + 1][n * 4 + 2]);
}
}
//
//Buddha blesses the code with no bugs
// _ooOoo_
// o8888888o
// 88" . "88
// (| -_- |)
// O\ = /O
// ____/`---'\____
// .' \| |// `.
// / \||| : |||// \
// / _||||| -:- |||||- \
// | | \\ - /// | |
// | \_| ''\---/'' | |
// \ .-\__ `-` ___/-. /
// ___`. .' /--.--\ `. . __
// ."" '< `.___\_<|>_/___.' >'"".
// | | : `- \`.;`\ _ /`;.`/ - ` : | |
// \ \ `-. \_ __\ /__ _/ .-` / /
// ======`-.____`-.___\_____/___.-`____.-'======
// `=---='
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//