Submission #97887

# Submission time Handle Problem Language Result Execution time Memory
97887 2019-02-19T10:22:13 Z Alexa2001 Fences (JOI18_fences) C++17
100 / 100
524 ms 3192 KB
#include <bits/stdc++.h>
#define x real()
#define y imag()

using namespace std;

const int Nmax = 210;
const long double eps = 1e-6;

typedef complex<long double> point;

long double dist[Nmax][2][Nmax][2];
int S, N;
point a[Nmax], O, W;
point S1[10], S2[10];


int Pair(int k)
{
    if(k >= 2*N) return -1;
    return k^1;
}

long double distanta(point A, point B)
{
    return abs(A - B);
}

long double cross(point a, point b)
{
    return (conj(a) * b).y;
}

long double dot(point a, point b)
{
    return (conj(a) * b).x;
}

bool on_segm(point A, point B, point C) /// este A pe segmentul [BC]?
{
    return distanta(A, B) + distanta(A, C) - distanta(B, C) < eps;
}

point reflect(point A, point B, point C)
{
    A -= B; C -= B;
    return B + (conj(A / C)) * C;
}

point perp(point A, point B, point C) /// perpendiculara din A pe dreapta BC
{
    return (A + reflect(A, B, C)) / (long double) 2;
}

point intersect(point a, point b, point c, point d)
{
    b -= a; c -= a; d -= a;
    long double c1 = cross(c, b), c2 = cross(d, b);
    return a + (c1 * d - c2 * c) / (c1 - c2);
}

bool inters_segm(point a, point b, point c, point d)
{
    if(cross(c-a, b-a) == cross(d-a, b-a)) return 0; /// paralele
    point P = intersect(a, b, c, d);
    return on_segm(P, a, b) && on_segm(P, c, d);
}

bool pass(point a, point b)
{
    return inters_segm(a, b, O, W);
}

bool is_ok(point A, point B) /// trece dreapta AB prin interiorul patratului initial?
{
    int i;
    for(i=0; i<4; ++i)
        if(inters_segm(A, B, S1[i], S2[i])) return 0;
    return 1;
}

void add_edge(int s, int t, long double D, bool p)
{
    dist[s][0][t][p] = min(dist[s][0][t][p], D);
    dist[s][1][t][p^1] = min(dist[s][1][t][p^1], D);
    swap(s, t);
    dist[s][0][t][p] = min(dist[s][0][t][p], D);
    dist[s][1][t][p^1] = min(dist[s][1][t][p^1], D);
}

void find_edge(int s, int t)
{
    if(Pair(s) == t) /// puncte de pe acelasi segment
    {
        add_edge(s, t, 0, pass(a[s], a[t]));
        return;
    }

    if(is_ok(a[s], a[t]))
        add_edge(s, t, distanta(a[s], a[t]), pass(a[s], a[t]));

    int z = Pair(s);
    if(z != -1)
    {
        point P = perp(a[t], a[s], a[z]);
        if(on_segm(P, a[s], a[z]) && is_ok(a[t], P))
            add_edge(s, t, distanta(a[t], P), pass(a[t], P) ^ pass(P, a[s]));
    }

    swap(s, t);
    z = Pair(s);
    if(z != -1)
    {
        point P = perp(a[t], a[s], a[z]);
        if(on_segm(P, a[s], a[z]) && is_ok(a[t], P))
            add_edge(s, t, distanta(a[t], P), pass(a[t], P) ^ pass(P, a[s]));
    }
}

int main()
{
 //   freopen("input", "r", stdin);
    cin.sync_with_stdio(false);

    int i, n;
    cin >> N >> S;
    for(i=0; i<N; ++i)
    {
        long double X, Y;
        cin >> X >> Y;
        a[2*i] = {X, Y};
        cin >> X >> Y;
        a[2*i+1] = {X, Y};
    }

    n = 2*N;
    a[n++] = {-S, S};
    a[n++] = {S, S};
    a[n++] = {S, -S};
    a[n++] = {-S, -S};

    O = {0, 0}; W = {251, 257};

    S1[0] = S2[3] = a[2*N] + point(eps, -eps);
    S1[1] = S2[0] = a[2*N+1] + point(-eps, -eps);
    S1[2] = S2[1] = a[2*N+2] + point(-eps, +eps);
    S1[3] = S2[2] = a[2*N+3] + point(eps, +eps);



    int j, k, i2, j2, k2;

    for(i=0; i<n; ++i)
        for(i2=0; i2<2; ++i2)
        {
            for(j=0; j<n; ++j)
                for(j2=0; j2<2; ++j2)
                    dist[i][i2][j][j2] = 10 * S;
            dist[i][i2][i][i2] = 0;
        }

    for(i=0; i<n; ++i)
        for(j=i+1; j<n; ++j)
            find_edge(i, j);

    for(k=0; k<n; ++k)
        for(k2=0; k2<2; ++k2)
            for(i=0; i<n; ++i)
                for(i2=0; i2<2; ++i2)
                    for(j=0; j<n; ++j)
                        for(j2=0; j2<2; ++j2)
                            dist[i][i2][j][j2] = min(dist[i][i2][j][j2], dist[i][i2][k][k2] + dist[k][k2][j][j2]);

    long double ans = 10 * S;
    for(i=0; i<n; ++i)
    {
        ans = min(ans, dist[i][0][i][1]);
     //   assert(dist[i][0][i][1] == dist[i][1][i][0]);
    }

    cout << setprecision(10) << fixed;
    cout << ans << '\n';
    return 0;
}

Compilation message

fences.cpp: In function 'int main()':
fences.cpp:137:15: warning: narrowing conversion of '- S' from 'int' to 'long double' inside { } [-Wnarrowing]
     a[n++] = {-S, S};
               ^~
fences.cpp:137:20: warning: narrowing conversion of 'S' from 'int' to 'long double' inside { } [-Wnarrowing]
     a[n++] = {-S, S};
                    ^
fences.cpp:138:19: warning: narrowing conversion of 'S' from 'int' to 'long double' inside { } [-Wnarrowing]
     a[n++] = {S, S};
                   ^
fences.cpp:138:19: warning: narrowing conversion of 'S' from 'int' to 'long double' inside { } [-Wnarrowing]
fences.cpp:139:20: warning: narrowing conversion of 'S' from 'int' to 'long double' inside { } [-Wnarrowing]
     a[n++] = {S, -S};
                    ^
fences.cpp:139:18: warning: narrowing conversion of '- S' from 'int' to 'long double' inside { } [-Wnarrowing]
     a[n++] = {S, -S};
                  ^~
fences.cpp:140:15: warning: narrowing conversion of '- S' from 'int' to 'long double' inside { } [-Wnarrowing]
     a[n++] = {-S, -S};
               ^~
fences.cpp:140:19: warning: narrowing conversion of '- S' from 'int' to 'long double' inside { } [-Wnarrowing]
     a[n++] = {-S, -S};
                   ^~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 3 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 3 ms 384 KB Output is correct
8 Correct 3 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 3 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 3 ms 384 KB Output is correct
13 Correct 3 ms 384 KB Output is correct
14 Correct 4 ms 384 KB Output is correct
15 Correct 2 ms 384 KB Output is correct
16 Correct 3 ms 384 KB Output is correct
17 Correct 3 ms 384 KB Output is correct
18 Correct 2 ms 384 KB Output is correct
19 Correct 2 ms 384 KB Output is correct
20 Correct 3 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 3 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 3 ms 384 KB Output is correct
8 Correct 3 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 3 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 3 ms 384 KB Output is correct
13 Correct 3 ms 384 KB Output is correct
14 Correct 4 ms 384 KB Output is correct
15 Correct 2 ms 384 KB Output is correct
16 Correct 3 ms 384 KB Output is correct
17 Correct 3 ms 384 KB Output is correct
18 Correct 2 ms 384 KB Output is correct
19 Correct 2 ms 384 KB Output is correct
20 Correct 3 ms 384 KB Output is correct
21 Correct 3 ms 512 KB Output is correct
22 Correct 4 ms 512 KB Output is correct
23 Correct 4 ms 512 KB Output is correct
24 Correct 2 ms 512 KB Output is correct
25 Correct 2 ms 560 KB Output is correct
26 Correct 3 ms 512 KB Output is correct
27 Correct 3 ms 512 KB Output is correct
28 Correct 3 ms 512 KB Output is correct
29 Correct 3 ms 512 KB Output is correct
30 Correct 3 ms 512 KB Output is correct
31 Correct 3 ms 512 KB Output is correct
32 Correct 3 ms 512 KB Output is correct
33 Correct 4 ms 512 KB Output is correct
34 Correct 3 ms 640 KB Output is correct
35 Correct 3 ms 512 KB Output is correct
36 Correct 4 ms 512 KB Output is correct
37 Correct 3 ms 512 KB Output is correct
38 Correct 2 ms 384 KB Output is correct
39 Correct 4 ms 512 KB Output is correct
40 Correct 3 ms 384 KB Output is correct
41 Correct 3 ms 384 KB Output is correct
42 Correct 3 ms 384 KB Output is correct
43 Correct 3 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 3 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 3 ms 384 KB Output is correct
8 Correct 3 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 3 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 3 ms 384 KB Output is correct
13 Correct 3 ms 384 KB Output is correct
14 Correct 4 ms 384 KB Output is correct
15 Correct 2 ms 384 KB Output is correct
16 Correct 3 ms 384 KB Output is correct
17 Correct 3 ms 384 KB Output is correct
18 Correct 2 ms 384 KB Output is correct
19 Correct 2 ms 384 KB Output is correct
20 Correct 3 ms 384 KB Output is correct
21 Correct 3 ms 512 KB Output is correct
22 Correct 4 ms 512 KB Output is correct
23 Correct 4 ms 512 KB Output is correct
24 Correct 2 ms 512 KB Output is correct
25 Correct 2 ms 560 KB Output is correct
26 Correct 3 ms 512 KB Output is correct
27 Correct 3 ms 512 KB Output is correct
28 Correct 3 ms 512 KB Output is correct
29 Correct 3 ms 512 KB Output is correct
30 Correct 3 ms 512 KB Output is correct
31 Correct 3 ms 512 KB Output is correct
32 Correct 3 ms 512 KB Output is correct
33 Correct 4 ms 512 KB Output is correct
34 Correct 3 ms 640 KB Output is correct
35 Correct 3 ms 512 KB Output is correct
36 Correct 4 ms 512 KB Output is correct
37 Correct 3 ms 512 KB Output is correct
38 Correct 2 ms 384 KB Output is correct
39 Correct 4 ms 512 KB Output is correct
40 Correct 3 ms 384 KB Output is correct
41 Correct 3 ms 384 KB Output is correct
42 Correct 3 ms 384 KB Output is correct
43 Correct 3 ms 384 KB Output is correct
44 Correct 524 ms 3088 KB Output is correct
45 Correct 444 ms 3192 KB Output is correct
46 Correct 468 ms 3192 KB Output is correct
47 Correct 386 ms 3084 KB Output is correct
48 Correct 491 ms 3192 KB Output is correct
49 Correct 451 ms 3088 KB Output is correct
50 Correct 433 ms 3072 KB Output is correct
51 Correct 431 ms 3164 KB Output is correct
52 Correct 472 ms 3192 KB Output is correct
53 Correct 414 ms 3072 KB Output is correct
54 Correct 448 ms 3164 KB Output is correct
55 Correct 476 ms 3192 KB Output is correct
56 Correct 441 ms 3088 KB Output is correct
57 Correct 449 ms 3116 KB Output is correct
58 Correct 414 ms 3192 KB Output is correct
59 Correct 471 ms 3072 KB Output is correct
60 Correct 462 ms 3088 KB Output is correct
61 Correct 460 ms 3088 KB Output is correct
62 Correct 4 ms 512 KB Output is correct
63 Correct 4 ms 640 KB Output is correct
64 Correct 380 ms 3064 KB Output is correct
65 Correct 477 ms 3164 KB Output is correct
66 Correct 378 ms 2944 KB Output is correct
67 Correct 450 ms 3064 KB Output is correct
68 Correct 401 ms 2976 KB Output is correct
69 Correct 485 ms 3164 KB Output is correct
70 Correct 339 ms 2944 KB Output is correct
71 Correct 408 ms 3028 KB Output is correct
72 Correct 420 ms 3080 KB Output is correct