POJ 1556 错误代码

来源:互联网 发布:金汇宝软件 编辑:程序博客网 时间:2024/06/09 16:43

POJ 1556 扔一个写错了代码 而且不知道那里错了

#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>#include <cmath>#include <queue>#include<iomanip>using namespace std;const int maxn = 1005;const double eps = 1e-8;const double INF = 1e9 + 7;int n;struct point {    double x,y1,y2,y3,y4;}p[maxn];struct seg {    double x1,y1,x2,y2;}segment[maxn];bool vis[maxn] = {0};double dd[maxn] = {0};int num = 0;int cnt = 0;struct node {    double x,y;}points[maxn];int s,t;double d[maxn][maxn] = {0};double corss (double x1,double y1,double x2,double y2) {    return x1 * y2 - x2 * y1;}double dist (double x1,double y1,double x2,double y2) {    return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));}void spfa () {    queue <int> q;    q.push (s);    for (int i = 0;i <= cnt; ++ i) {        dd[i] = INF;    }    dd[s] = 0;    while (!q.empty()) {        int u = q.front();        q.pop();        for (int i = 1;i <= cnt; ++ i) {            if (INF - d[u][i] > eps) {                q.push (i);                dd[i] = min(dd[i],dd[u] + d[u][i]);            }        }    }}bool check (seg a,seg b) {    if (!(max (a.x1,a.x2) > min (b.x1,b.x2) && max (b.x1,b.x2) > min (a.x1,a.x2) && max (a.y1,a.y2) > min (b.y1,b.y2) && max (b.y1,b.y2) > min (a.y1,a.y2))) return false;    if (corss(b.x2 - b.x1, b.y2 - b.y1, a.x1 - b.x1, a.y1 - b.y1) * corss(b.x2 - b.x1, b.y2 - b.y1, a.x2 - b.x1, a.y2 - b.y1) < 0) {        if (corss(a.x2 - a.x1, a.y2 - a.y1, b.x1 - a.x1, b.y1 - a.y1) * corss(a.x2 - a.x1, a.y2 - a.y1, b.x2 - a.x1, b.y2 - a.y1) < 0)            return true;    }    return false;}bool judge (int p1,int p2) {    seg temp = {points[p1].x,points[p1].y,points[p2].x,points[p2].y};    int flag = 1;    for (int i = 0;i < num; ++ i) {        if (check (temp,segment[i])) {            flag = 0;            break;        }    }    if (flag) return true;    return false;}int main () {    ios_base :: sync_with_stdio(false);    while (cin >> n) {        if (n == -1) break;        num = 0;        cnt = 0;        points[cnt ++] = {0,5};        for (int i = 0;i <= maxn; ++ i) {            for (int j = 0;j <= maxn; ++ j) {                d[i][j] = INF;            }        }        for (int i = 1;i <= n; ++ i) {            cin >> p[i].x >> p[i].y1 >> p[i].y2 >> p[i].y3 >> p[i].y4;            points[cnt ++] = {p[i].x,p[i].y1};            points[cnt ++] = {p[i].x,p[i].y2};            points[cnt ++] = {p[i].x,p[i].y3};            points[cnt ++] = {p[i].x,p[i].y4};            segment[num ++] = {p[i].x,0,p[i].x,p[i].y1};            segment[num ++] = {p[i].x,p[i].y2,p[i].x,p[i].y3};            segment[num ++] = {p[i].x,p[i].y4,p[i].x,10};        }        points[cnt] = {10,5};        for (int i = 0;i <= cnt; ++ i) {            for (int j = i + 1;j <= cnt; ++ j) {                if (judge (i,j)) {                    d[i][j] = dist(points[i].x, points[i].y, points[j].x, points[j].y);                }            }        }        s = 0,t = cnt;        spfa ();        if (INF - dd[t] < eps) {            cout << -1 << endl;        }        else {            cout<<fixed<<setprecision(2)<<dd[t]<<endl;        }    }    return 0;}
原创粉丝点击