Submission #532807

# Submission time Handle Problem Language Result Execution time Memory
532807 2022-03-04T01:09:06 Z wiwiho Fences (JOI18_fences) C++14
100 / 100
268 ms 3516 KB
#include <bits/stdc++.h>
#include <bits/extc++.h>
 
#define StarBurstStream ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define iter(a) a.begin(), a.end()
#define riter(a) a.rbegin(), a.rend()
#define lsort(a) sort(iter(a))
#define gsort(a) sort(riter(a))
#define pb(a) push_back(a)
#define eb(a) emplace_back(a)
#define pf(a) push_front(a)
#define ef(a) emplace_front(a)
#define pob pop_back()
#define pof pop_front()
#define mp(a, b) make_pair(a, b)
#define F first
#define S second
#define mt make_tuple
#define gt(t, i) get<i>(t)
#define tomax(a, b) ((a) = max((a), (b)))
#define tomin(a, b) ((a) = min((a), (b)))
#define topos(a) ((a) = (((a) % MOD + MOD) % MOD))
#define uni(a) a.resize(unique(iter(a)) - a.begin())
#define printv(a, b) {bool pvaspace=false; \
for(auto pva : a){ \
    if(pvaspace) b << " "; pvaspace=true;\
    b << pva;\
}\
b << "\n";}
 
using namespace std;
using namespace __gnu_pbds;
 
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
 
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
using tiii = tuple<int, int, int>;
 
const ll MOD = 1000000007;
const ll MAX = 2147483647;
 
template<typename A, typename B>
ostream& operator<<(ostream& o, pair<A, B> p){
    return o << '(' << p.F << ',' << p.S << ')';
}
 
ll ifloor(ll a, ll b){
    if(b < 0) a *= -1, b *= -1;
    if(a < 0) return (a - b + 1) / b;
    else return a / b;
}
 
ll iceil(ll a, ll b){
    if(b < 0) a *= -1, b *= -1;
    if(a > 0) return (a + b - 1) / b;
    else return a / b;
}
 
void waassert(bool b){
    if(b) return;
    cout << "WA\n";
    exit(0);
}
 
using pdd = pair<ld, ld>;
ld eps = 1e-9;
 
pdd operator+(pdd a, pdd b){
    return mp(a.F + b.F, a.S + b.S);
}
 
pdd operator-(pdd a, pdd b){
    return mp(a.F - b.F, a.S - b.S);
}
 
ld dot(pdd a, pdd b){
    return a.F * b.F + a.S * b.S;
}
 
ld cross(pdd a, pdd b){
    return a.F * b.S - a.S * b.F;
}
 
ld abs2(pdd a){
    return a.F * a.F + a.S * a.S;
}
 
ld abs(pdd a){
    return sqrt(abs2(a));
}
 
pdd operator*(ld i, pdd p){
    return mp(i * p.F, i * p.S);
}
 
int ori(pdd a, pdd b){
    if(abs(cross(a, b)) <= eps) return 0;
    else if(cross(a, b) > 0) return 1;
    else return -1;
}
 
bool intersect(pdd a, pdd b, pdd c, pdd d){
    return ori(b - a, c - a) * ori(b - a, d - a) < 0 && ori(d - c, a - c) * ori(d - c, b - c) < 0;
}

pdd intersection(pdd a, pdd b, pdd c, pdd d){
    ld t1 = abs(cross(c - a, d - a));
    ld t2 = abs(cross(c - b, d - b));
    return 1 / (t1 + t2) * (t2 * a + t1 * b);
}
 
bool canproj(pdd p, pdd a, pdd b){
    return dot(b - a, p - a) >= 0 && dot(a - b, p - b) >= 0;
}
 
pdd getproj(pdd p, pdd a, pdd b){
    if(abs2(a - b) <= eps) return a;
    return a + dot(p - a, b - a) / abs2(b - a) * (b - a);
}
 
pdd v1 = mp(0, 0), v2 = mp(48763, 3234234);
 
struct Line{
    pdd a, b;
};
 
struct edge{
    int to;
    ld w;
    int f;
};
 
ostream& operator<<(ostream& o, edge e){
    return o << '(' << e.to << ',' << e.w << ',' << e.f << ')';
}
 
int n;
ld SS;
vector<Line> border;
vector<Line> e;
vector<vector<edge>> g;
vector<pdd> midp;
vector<bool> hmid;
 
void init(){
    cin >> n >> SS;
    e.resize(n);
    for(int i = 0; i < n; i++){
        cin >> e[i].a.F >> e[i].a.S >> e[i].b.F >> e[i].b.S;
    }
    border.eb(Line({mp(SS, SS), mp(SS, -SS)}));
    border.eb(Line({mp(-SS, -SS), mp(SS, -SS)}));
    border.eb(Line({mp(-SS, -SS), mp(-SS, SS)}));
    border.eb(Line({mp(SS, SS), mp(-SS, SS)}));
    border.eb(Line({mp(SS, -SS), mp(-SS, SS)}));
    border.eb(Line({mp(-SS, -SS), mp(SS, SS)}));
    e.eb(Line({mp(SS, SS), mp(SS, SS)}));
    e.eb(Line({mp(SS, -SS), mp(SS, -SS)}));
    e.eb(Line({mp(-SS, SS), mp(-SS, SS)}));
    e.eb(Line({mp(-SS, -SS), mp(-SS, -SS)}));
    n += 4;
    g.resize(2 * n);
    midp.resize(n);
    hmid.resize(n);
    for(int i = 0; i < n; i++){
        if(!intersect(e[i].a, e[i].b, v1, v2)) continue;
        hmid[i] = true;
        midp[i] = intersection(e[i].a, e[i].b, v1, v2);
        //cerr << "mid " << i << " " << e[i].a << " " << e[i].b << " " << midp[i] << "\n";
    }
    for(int i = 0; i < n; i++){
        g[2 * i].eb(edge({2 * i + 1, 0, hmid[i]}));
        g[2 * i + 1].eb(edge({2 * i, 0, hmid[i]}));
    }
}
 
bool check(pdd a, pdd b){
    for(auto i : border){
        if(intersect(a, b, i.a, i.b)) return false;
    }
    return true;
}
 
int district(int x, pdd a){
    if(!hmid[x]) return 0;
    return a < midp[x];
}

bool foxyy(pdd a, pdd b){
    return intersect(a, b, v1, v2);
}

void addedge(int x, pdd a, int y, pdd b){
    if(!check(a, b)){
        //cerr << "qq " << a << " " << b << "\n";
        return;
    }
    int av = 2 * x + district(x, a);
    int bv = 2 * y + district(y, b);

    //cerr << "addedge " << x << " " << av << " " << y << " " << bv << "\n";
    g[av].eb(edge({bv, abs(a - b), foxyy(a, b)}));
    g[bv].eb(edge({av, abs(a - b), foxyy(a, b)}));
    //cerr << "addedge " << a << " " << b << " " << abs(a - b) << " " << foxyy(a, b) << "\n";
}
 
void tryproj(int x, pdd p, int y, pdd a, pdd b){
    if(!canproj(p, a, b)) return;
    //cerr << "canproj " << x << " " << p << " " << y << " " << getproj(p, a, b) << "\n";
    addedge(x, p, y, getproj(p, a, b));
}
 
void buildedges(int x, int y){
    addedge(x, e[x].a, y, e[y].a);
    addedge(x, e[x].b, y, e[y].a);
    addedge(x, e[x].a, y, e[y].b);
    addedge(x, e[x].b, y, e[y].b);
    
    tryproj(x, e[x].a, y, e[y].a, e[y].b);
    tryproj(x, e[x].b, y, e[y].a, e[y].b);
    tryproj(y, e[y].a, x, e[x].a, e[x].b);
    tryproj(y, e[y].b, x, e[x].a, e[x].b);
}
 
struct info{
    ld d;
    int v;
    int f;
 
    bool operator<(info b) const{
        return d > b.d;
    }
};
 
ld calc(int s){
    std::priority_queue<info> pq;
    vector<vector<ld>> dis(2, vector<ld>(2 * n, 1e20));
    dis[0][s] = 0;
    pq.push(info({0, s, 0}));
    vector<vector<bool>> vst(2, vector<bool>(2 * n));
    //int cnt = 0;
    while(!pq.empty()){
        ld d = pq.top().d;
        int v = pq.top().v, f = pq.top().f;
        pq.pop();
        if(vst[f][v] || abs(d - dis[f][v]) > eps) continue;
        vst[f][v] = true;
        for(auto i : g[v]){
            int nf = f ^ i.f;
            //cnt++;
            //assert(cnt <= 1e5);
            if(vst[nf][i.to] || d + i.w >= dis[nf][i.to] - eps) continue;
            dis[nf][i.to] = d + i.w;
            pq.push(info({d + i.w, i.to, nf}));
        }
    }
    //cerr << "calc " << s << " " << dis[1][s] << "\n";
    return dis[1][s];
}
 
int main(){
    StarBurstStream
 
    init();
 
    for(int i = 0; i < n; i++){
        for(int j = i + 1; j < n; j++) buildedges(i, j);
    }
 
    /*cerr << "graph\n";
    for(int i = 0; i < ts; i++){
        cerr << i << " " << pos[i] << "  ";
        printv(g[i], cerr);
    }*/
 
    ld ans = 1e20;
    for(int i = 0; i < 2 * n; i++){
        ans = min(ans, calc(i));
    }
    cout << fixed << setprecision(20) << ans << "\n";
 
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 216 KB Output is correct
2 Correct 1 ms 316 KB Output is correct
3 Correct 1 ms 308 KB Output is correct
4 Correct 1 ms 312 KB Output is correct
5 Correct 1 ms 308 KB Output is correct
6 Correct 1 ms 312 KB Output is correct
7 Correct 1 ms 312 KB Output is correct
8 Correct 1 ms 312 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 216 KB Output is correct
2 Correct 1 ms 316 KB Output is correct
3 Correct 1 ms 308 KB Output is correct
4 Correct 1 ms 312 KB Output is correct
5 Correct 1 ms 308 KB Output is correct
6 Correct 1 ms 312 KB Output is correct
7 Correct 1 ms 312 KB Output is correct
8 Correct 1 ms 312 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 308 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 1 ms 308 KB Output is correct
33 Correct 1 ms 332 KB Output is correct
34 Correct 1 ms 332 KB Output is correct
35 Correct 1 ms 332 KB Output is correct
36 Correct 1 ms 332 KB Output is correct
37 Correct 1 ms 332 KB Output is correct
38 Correct 1 ms 332 KB Output is correct
39 Correct 1 ms 332 KB Output is correct
40 Correct 1 ms 312 KB Output is correct
41 Correct 1 ms 332 KB Output is correct
42 Correct 1 ms 308 KB Output is correct
43 Correct 1 ms 308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 216 KB Output is correct
2 Correct 1 ms 316 KB Output is correct
3 Correct 1 ms 308 KB Output is correct
4 Correct 1 ms 312 KB Output is correct
5 Correct 1 ms 308 KB Output is correct
6 Correct 1 ms 312 KB Output is correct
7 Correct 1 ms 312 KB Output is correct
8 Correct 1 ms 312 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 308 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 1 ms 308 KB Output is correct
33 Correct 1 ms 332 KB Output is correct
34 Correct 1 ms 332 KB Output is correct
35 Correct 1 ms 332 KB Output is correct
36 Correct 1 ms 332 KB Output is correct
37 Correct 1 ms 332 KB Output is correct
38 Correct 1 ms 332 KB Output is correct
39 Correct 1 ms 332 KB Output is correct
40 Correct 1 ms 312 KB Output is correct
41 Correct 1 ms 332 KB Output is correct
42 Correct 1 ms 308 KB Output is correct
43 Correct 1 ms 308 KB Output is correct
44 Correct 268 ms 3452 KB Output is correct
45 Correct 175 ms 3024 KB Output is correct
46 Correct 136 ms 2284 KB Output is correct
47 Correct 137 ms 1872 KB Output is correct
48 Correct 224 ms 3516 KB Output is correct
49 Correct 202 ms 3312 KB Output is correct
50 Correct 182 ms 2536 KB Output is correct
51 Correct 131 ms 2000 KB Output is correct
52 Correct 154 ms 2408 KB Output is correct
53 Correct 165 ms 2372 KB Output is correct
54 Correct 170 ms 2512 KB Output is correct
55 Correct 179 ms 2828 KB Output is correct
56 Correct 197 ms 2640 KB Output is correct
57 Correct 171 ms 2280 KB Output is correct
58 Correct 147 ms 2360 KB Output is correct
59 Correct 161 ms 2516 KB Output is correct
60 Correct 196 ms 2816 KB Output is correct
61 Correct 178 ms 2896 KB Output is correct
62 Correct 1 ms 336 KB Output is correct
63 Correct 2 ms 336 KB Output is correct
64 Correct 212 ms 2832 KB Output is correct
65 Correct 246 ms 2200 KB Output is correct
66 Correct 171 ms 1852 KB Output is correct
67 Correct 203 ms 3372 KB Output is correct
68 Correct 182 ms 3240 KB Output is correct
69 Correct 219 ms 3084 KB Output is correct
70 Correct 175 ms 2768 KB Output is correct
71 Correct 190 ms 2904 KB Output is correct
72 Correct 132 ms 2128 KB Output is correct