結果

問題 No.1065 電柱 / Pole (Easy)
ユーザー Ricky_ponRicky_pon
提出日時 2020-05-29 21:40:33
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 267 ms / 2,000 ms
コード長 15,844 bytes
コンパイル時間 3,134 ms
コンパイル使用メモリ 225,388 KB
実行使用メモリ 22,528 KB
最終ジャッジ日時 2024-04-23 20:41:56
合計ジャッジ時間 8,625 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 114 ms
13,312 KB
testcase_03 AC 137 ms
21,200 KB
testcase_04 AC 135 ms
21,204 KB
testcase_05 AC 106 ms
21,072 KB
testcase_06 AC 106 ms
21,204 KB
testcase_07 AC 35 ms
8,320 KB
testcase_08 AC 126 ms
22,016 KB
testcase_09 AC 13 ms
5,376 KB
testcase_10 AC 57 ms
11,776 KB
testcase_11 AC 39 ms
9,088 KB
testcase_12 AC 23 ms
7,040 KB
testcase_13 AC 113 ms
14,436 KB
testcase_14 AC 131 ms
16,956 KB
testcase_15 AC 163 ms
18,548 KB
testcase_16 AC 71 ms
11,420 KB
testcase_17 AC 170 ms
19,856 KB
testcase_18 AC 47 ms
9,088 KB
testcase_19 AC 156 ms
19,000 KB
testcase_20 AC 36 ms
7,808 KB
testcase_21 AC 57 ms
10,752 KB
testcase_22 AC 144 ms
17,784 KB
testcase_23 AC 3 ms
5,376 KB
testcase_24 AC 3 ms
5,376 KB
testcase_25 AC 22 ms
6,784 KB
testcase_26 AC 83 ms
12,456 KB
testcase_27 AC 81 ms
12,384 KB
testcase_28 AC 147 ms
18,232 KB
testcase_29 AC 15 ms
6,144 KB
testcase_30 AC 161 ms
19,620 KB
testcase_31 AC 110 ms
15,904 KB
testcase_32 AC 66 ms
11,192 KB
testcase_33 AC 158 ms
20,408 KB
testcase_34 AC 54 ms
9,472 KB
testcase_35 AC 166 ms
19,928 KB
testcase_36 AC 2 ms
5,376 KB
testcase_37 AC 3 ms
5,376 KB
testcase_38 AC 3 ms
5,376 KB
testcase_39 AC 3 ms
5,376 KB
testcase_40 AC 2 ms
5,376 KB
testcase_41 AC 267 ms
22,528 KB
testcase_42 AC 62 ms
9,216 KB
testcase_43 AC 121 ms
13,440 KB
testcase_44 AC 36 ms
7,040 KB
testcase_45 AC 118 ms
13,184 KB
testcase_46 AC 2 ms
5,376 KB
testcase_47 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define For(i, a, b) for(int (i)=(int)(a); (i)<(int)(b); ++(i))
#define rFor(i, a, b) for(int (i)=(int)(a)-1; (i)>=(int)(b); --(i))
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef unsigned long long ulint;
typedef pair<int, int> pii;
typedef pair<lint, lint> pll;
template<class T> bool chmax(T &a, const T &b){if(a<b){a=b; return true;} return false;}
template<class T> bool chmin(T &a, const T &b){if(a>b){a=b; return true;} return false;}
template<class T> T div_floor(T a, T b){
    if(b < 0) a *= -1, b *= -1;
    return a>=0 ? a/b : (a+1)/b-1;
}
template<class T> T div_ceil(T a, T b){
    if(b < 0) a *= -1, b *= -1;
    return a>0 ? (a-1)/b+1 : a/b;
}

constexpr lint mod = 1e9+7;
constexpr lint INF = mod * mod;
constexpr int MAX = 500010;

constexpr double eps=1e-9;
constexpr double PI=3.14159265358979323846264338327950;
 
inline int sgn(double x){
    if(x<-eps) return -1;
    if(x>eps) return 1;
    return 0;
}
 
inline bool EQ(double x, double y){
    return sgn(x-y)==0;
}
 
inline bool GE(double x, double y){
    return sgn(x-y)==1;
}
 
inline bool LE(double x, double y){
    return sgn(x-y)==-1;
}
 
inline bool GEQ(double x, double y){
    return sgn(x-y)>=0;
}
 
inline bool LEQ(double x, double y){
    return sgn(x-y)<=0;
}
 
struct Point{
    double x, y;
    Point(double x=0, double y=0): x(x), y(y){}
 
    Point operator+(const Point &p){
        return {x+p.x, y+p.y};
    }
 
    Point operator-(const Point &p){
        return {x-p.x, y-p.y};
    }
 
    Point operator*(const double k){
        return {k*x, k*y};
    }
 
    Point operator/(const double k){
        return {x/k, y/k};
    }
 
    double operator*(const Point &p){
        return x*p.x+y*p.y;
    }
 
    double operator^(const Point &p){
        return x*p.y-y*p.x;
    }
 
    bool operator==(const Point &p){
        return EQ(x, p.x) && EQ(y, p.y);
    }
 
    bool operator<(const Point &p) const{
        if(EQ(x, p.x)) return LE(y, p.y);
        return LE(x, p.x);
    }
};
 
using Vec=Point;
using Polygon=vector<Point>;
 
double norm(Point p){
    return p.x*p.x+p.y*p.y;
}
 
double abs(Point p){
    return sqrt(norm(p));
}
 
double arg(Point p){
    return atan2(p.y, p.x);
}
 
Point rot(Point p, double t){
    return {p.x*cos(t)-p.y*sin(t), p.x*sin(t)+p.y*cos(t)};
}
 
Point proj(Point a, Vec v, Point p){
    double t=v*(p-a)/norm(v);
    return a+v*t;
}
 
Point refl(Point a, Vec v, Point p){
    return proj(a, v, p)*2-p;
}
 
constexpr int CCW_COUNTER_CLOCKWISE=1; //反時計回り
constexpr int CCW_CLOCKWISE=-1; //時計回り
constexpr int CCW_ONLINE_BACK=-2; //一直線, C->A->B
constexpr int CCW_ONLINE_FRONT=2; //一直線, A->B->C
constexpr int CCW_ON_SEGMENT=0; //一直線, A->C->B
 
inline int ccw(Point a, Point b, Point c){
    Vec v=b-a, w=c-a;
    if(GE(v^w, 0)) return CCW_COUNTER_CLOCKWISE;
    if(LE(v^w, 0)) return CCW_CLOCKWISE;
    if(LE(v*w, 0)) return CCW_ONLINE_BACK;
    if(LE((a-b)*(c-b), 0)) return CCW_ONLINE_FRONT;
    return CCW_ON_SEGMENT;
}
 
bool isParallel(Vec v, Vec w){
    return EQ(v^w, 0);
}
 
bool isOrthogonal(Vec v, Vec w){
    return EQ(v*w, 0);
}
 
bool intersectSS(Point a, Point b, Point c, Point d){
    return ccw(a, b, c)*ccw(a, b, d)<=0 && ccw(c, d, a)*ccw(c, d, b)<=0;
}
 
Point getCrossPointLL(Point a, Vec v, Point b, Vec w){
    double t=((b-a)^w)/(v^w);
    return a+v*t;
}
 
double getDistanceLP(Point a, Vec v, Point p){
    return abs(v^(p-a)/abs(v));
}
 
double getDistanceSP(Point a, Point b, Point p){
    if(LE((b-a)*(p-a), 0)) return abs(p-a);
    if(LE((a-b)*(p-b), 0)) return abs(p-b);
    return getDistanceLP(a, b-a, p);
}
 
double getDistanceLL(Point a, Vec v, Point b, Vec w){
    if(isParallel(v, w)) return getDistanceLP(a, v, b);
    return 0;
}
 
double getDistanceLS(Point a, Vec v, Point c, Point d){
    Point b=a+v;
    if(ccw(a, b, c)*ccw(a, b, d)<=0) return 0;
    return min(getDistanceLP(a, v, c), getDistanceLP(a, v, d));
}
 
double getDistanceSS(Point a, Point b, Point c, Point d){
    if(intersectSS(a, b, c, d)) return 0;
    return min({getDistanceSP(a, b, c), getDistanceSP(a, b, d),
                getDistanceSP(c, d, a), getDistanceSP(c, d, b)});
}
 
double getAreaP(Polygon &p){
    double ret=0;
    rep(i, (int)p.size()) ret+=p[i]^p[(i+1)%p.size()]/2;
    return abs(ret);
}
 
bool isConvex(Polygon &p){
    int n=p.size();
    bool flag1=false, flag2=false;
    rep(i, n){
        int tmp=ccw(p[(i+n-1)%n], p[i], p[(i+1)%n]);
        if(tmp==CCW_COUNTER_CLOCKWISE){
            if(flag2) return false;
            flag1=true;
        }
        else if(tmp==CCW_CLOCKWISE){
            if(flag1) return false;
            flag2=true;
        }
    }
    return true;
}
 
Polygon convex_hull(Polygon p){
    int n=p.size();
    sort(p.begin(), p.end());
    Polygon ch(2*n);
    int k=0;
    rep(i, n){
        while(k>1 && LE((ch[k-1]-ch[k-2])^(p[i]-ch[k-1]), 0)) --k;
        ch[k++]=p[i];
    }
    for(int i=n-2, t=k; i>=0; --i){
        while(k>t && LE((ch[k-1]-ch[k-2])^(p[i]-ch[k-1]), 0)) --k;
        ch[k++]=p[i];
    }
    ch.resize(k-1);
    return ch;
}
 
int intersectCC(Point c1, double r1, Point c2, double r2){
    if(r1<r2){
        swap(c1, c2);
        swap(r1, r2);
    }
    double d=abs(c1-c2), r=r1+r2;
    if(GE(d, r)) return 4;
    if(EQ(d, r)) return 3;
    if(EQ(d+r2, r1)) return 1;
    if(LE(d+r2, r1)) return 0;
    return 2;
}
 
bool intersectCL(Point c, double r, Point a, Vec v){
    return LEQ(getDistanceLP(a, v, c), r);
}
 
bool intersectCS(Point c, double r, Point a, Point b){
    return LEQ(getDistanceSP(a, b, c), r) && GEQ(max(abs(a-c), abs(b-c)), r);
}
 
Polygon getCrossPointCL(Point c, double r, Point a, Vec v){
    Polygon ps;
    if(!intersectCL(c, r, a, v)) return ps;
    Point p=proj(a, v, c);
    double t=sqrt(max((double)0.0, (r*r-norm(p-c))/norm(v)));
    ps.push_back(p+v*t);
    if(!EQ(t, 0)) ps.push_back(p-v*t);
    return ps;
}
 
Polygon getCrossPointCC(Point c1, double r1, Point c2, double r2){
    Polygon ps;
    Vec v=c2-c1, w(v.y*-1, v.x);
    double d=abs(v);
    double x=(d*d+r1*r1-r2*r2)/(2*d);
    double y=sqrt(max(r1*r1-x*x, (double)0.0));
    ps.push_back(c1+v*x/d+w*y/d);
    if(intersectCC(c1, r1, c2, r2)!=2) return ps;
    ps.push_back(c1+v*x/d-w*y/d);
    return ps;
}
 
double getAreaCC(Point c1, double r1, Point c2, double r2){
    int flag=intersectCC(c1, r1, c2, r2);
    if(flag>=3) return 0;
    if(flag<=1){
        double r=min(r1, r2);
        return PI*r*r;
    }
    double d=abs(c1-c2);
    double ret=0;
    rep(i, 2) {
        double x=(d*d+r1*r1-r2*r2)/(2*d);
        double t=acos(x/r1)*2;
        ret+=(t-sin(t))*r1*r1/2;
        swap(c1, c2);
        swap(r1, r2);
    }
    return ret;
}
 
Polygon Tangent(Point c, double r, Point p){
    Polygon ps;
    double d=abs(p-c);
    double t=acos(r/d);
    ps.push_back(c+rot(p-c, t)*r/d);
    ps.push_back(c+rot(p-c, -t)*r/d);
    return ps;
}
 
Polygon getCommonTangent(Point c1, double r1, Point c2, double r2){
    Polygon ps;
    int flag=intersectCC(c1, r1,c2, r2);
    if(flag>=2){
        double d=abs(c2-c1);
        double t=acos(abs(r1-r2)/d);
        if(LE(r1, r2)) t=PI-t;
        ps.push_back(c1+rot(c2-c1, t)*r1/d);
        ps.push_back(c1+rot(c2-c1, -t)*r1/d);
    }
    if(flag==4){
        double d=abs(c2-c1);
        double L=d*r1/(r1+r2);
        double t=acos(r1/L);
        ps.push_back(c1+rot(c2-c1, t)*r1/d);
        ps.push_back(c1+rot(c2-c1, -t)*r1/d);
    }
    if(flag==3 || flag==1){
        Polygon tg=getCrossPointCC(c1, r1, c2, r2);
        ps.push_back(tg[0]);
    }
    return ps;
}
 
Point getO(Point a, Point b, Point c){
    Point M=(a+b)/2, N=(a+c)/2;
    Vec v={-(b-a).y, (b-a).x}, w={-(c-a).y, (c-a).x};
    return getCrossPointLL(M, v, N, w);
}
 
Point getI(Point a, Point b, Point c){
    double A=abs(b-c), B=abs(c-a), C=abs(a-b);
    return (a*A+b*B+c*C)/(A+B+C);
}
 
Point getH(Point a, Point b, Point c){
    Vec v={-(c-b).y, (c-b).x}, w={-(c-a).y, (c-a).x};
    return getCrossPointLL(a, v, b, w);
}
 
pair<Point, double> MinimumBoundingCircle(Polygon &p){
    Point C;
    double r;
    if(p.size()==1) C=p[0], r=0;
    else if(p.size()==2) C=(p[0]+p[1])/2, r=abs(p[0]-C);
    else{
        r=INF;
        Polygon ch=convex_hull(p);
        int K=ch.size();
        auto check=[&](Point tc, double tr){
            rep(i, K){
                if(GE(abs(ch[i]-tc), tr)) return false;
            }
            return true;
        };
        rep(i, K)For(j, i+1, K){
            Point tc=(ch[i]+ch[j])/2;
            double tr=abs(ch[i]-tc);
            if(check(tc, tr) && chmin(r, tr)) C=tc;
            For(k, j+1, K){
                int ccw_flag=ccw(ch[i], ch[j], ch[k]);
                if(ccw_flag!=CCW_COUNTER_CLOCKWISE && ccw_flag!=CCW_CLOCKWISE) continue;
                tc=getO(ch[i], ch[j], ch[k]);
                tr=abs(ch[i]-tc);
                if(check(tc, tr) && chmin(r, tr)) C=tc;
            }
        }
    }
    return {C, r};
}

typedef struct UnionFindTree{
    vector<int> par;
 
    UnionFindTree(int n): par(n, -1){}
 
    int find(int x){
        if(par[x] < 0) return x;
        return par[x] = find(par[x]);
    }
 
    int size(int x){
        return -par[find(x)];
    }
 
    bool unite(int x, int y){
        x = find(x);
        y = find(y);
        if(x == y) return false;
        if(size(x) < size(y)) swap(x, y);
        par[x] += par[y];
        par[y] = x;
        return true;
    }
 
    bool same(int x, int y){
        return find(x) == find(y);
    }
}UF;
 
template<typename T> struct edge{
    int from, to; T cost;
    edge(int f, int t, T c): from(f), to(t), cost(c){}
};
 
template<typename T> struct Graph{
    vector<vector<edge<T>>> G;
    int n;
 
    Graph(int n_): n(n_){
        G.resize(n);
    }
 
    void add_edge(int f, int t, T c){
        G[f].emplace_back(f, t, c);
    }
 
    pair<bool, vector<T>> bellman_ford(int s){
        T d_INF = numeric_limits<T>::max();
        vector<T> d(n, d_INF);
        vector<edge<T>> E;
        rep(i, n)for(edge<T> &e: G[i]) E.push_back(e);
        d[s] = 0;
        rep(i, n)for(edge<T> &e: E){
            if(d[e.from] != d_INF && d[e.from] + e.cost < d[e.to]){
                d[e.to] = d[e.from] + e.cost;
                if(i == n-1) return make_pair(true, d);
            }
        }
        return make_pair(false, d);
    }
 
    vector<T> dijkstra(int s){
        using P = pair<T, int>;
        priority_queue<P, vector<P>, greater<P>> que;
        vector<T> d(n, numeric_limits<T>::max());
        d[s] = 0;
        que.push(P((T)0, s));
        while(!que.empty()){
            P p = que.top(); que.pop();
            int v = p.second;
            if(d[v] < p.first) continue;
            for(edge<T> &e : G[v]){
                if(d[e.to] > d[v] + e.cost){
                    d[e.to] = d[v] + e.cost;
                    que.push(P(d[e.to], e.to));
                }
            }
        }
        return d;
    }
 
    pair<bool, vector<vector<T>>> warshall_floyd(){
        T d_INF = numeric_limits<T>::max();
        vector<vector<T>> d = vector<vector<T>>(n, vector<T>(n, d_INF));
        rep(i, n){
            for(edge<T> &e: G[i]) d[i][e.to] = e.cost;
            d[i][i] = 0;
        }
        rep(k, n)rep(i, n)rep(j, n)if(d[i][k] < d_INF && d[k][j] < d_INF){
            d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
        }
        rep(i, n)if(d[i][i] < 0) return make_pair(true, d);
        return make_pair(false, d);
    }
 
    pair<T, Graph<T>> kruskal(){
        vector<edge<T>> E;
        rep(i, n)for(edge<T> &e: G[i]) E.push_back(e);
        sort(E.begin(), E.end(), [](const edge<T> &e1, const edge<T> &e2){return e1.cost < e2.cost;});
        UF uf(n);
        T ret = 0;
        Graph<T> MST(n);
        for(edge<T> &e: E){
            if(!uf.same(e.from, e.to)){
                uf.unite(e.from, e.to);
                ret += e.cost;
                MST.add_edge(e.from, e.to, e.cost);
                MST.add_edge(e.to, e.from, e.cost);
            }
        }
        return {ret, MST};
    }
 
    pair<bool, vector<int>> toposo(){
        vector<int> ret(n, -1), in(n, 0);
        rep(i, n)for(edge<T> &e: G[i]) ++in[e.to];
        int cur = 0;
        stack<int> st;
        rep(i, n)if(!in[i]) st.push(i);
        if(st.empty()) return make_pair(false, ret);
        while(!st.empty()){
            int v = st.top(); st.pop();
            ret[cur++] = v;
            for(edge<T> &e: G[v]){
                if(!in[e.to]) return make_pair(false, ret);
                --in[e.to];
                if(!in[e.to]) st.push(e.to);
            }
        }
        return make_pair(cur==n, ret);
    }
 
    bool has_cycle(){
        return !toposo().fi;
    }
 
    void scc_dfs(int v, vector<bool> &used, vector<int> &vs){
        used[v] = true;
        for(edge<T> &e: G[v])if(!used[e.to]) scc_dfs(e.to, used, vs);
        vs.push_back(v);
    }
 
    void scc_rdfs(int v, int k, vector<int> &cmp, vector<bool> &used, vector<vector<int>> &rG){
        used[v] = true;
        cmp[v] = k;
        for(int nv: rG[v])if(!used[nv]) scc_rdfs(nv, k, cmp, used, rG);
    }
 
    tuple<int, vector<int>, vector<vector<int>>> scc(){
            vector<vector<int>> rG(n);
            rep(i, n)for(edge<T> &e: G[i]) rG[e.to].push_back(i);
            vector<bool> used(n, false);
            vector<int> vs;
            vector<int> vtoc(n);
            rep(i, n)if(!used[i]) scc_dfs(i, used, vs);
            fill(used.begin(), used.end(), false);
            int k = 0;
            vector<vector<int>> ctov=vector<vector<int>>(n, vector<int>());
            rrep(i, n)if(!used[vs[i]]) scc_rdfs(vs[i], k++, vtoc, used, rG, ctov);
            return make_tuple(k, vtoc, ctov);
    }
 
    int bridge_dfs(int v, int pv, int &idx, vector<int> &ord, vector<int> &low, vector<pii> &bridge){
        ord[v]=low[v]=idx++;
        for(auto &e: G[v])if(e.to!=pv){
            int nv=e.to;
            if(ord[nv]<0){
                chmin(low[v], bridge_dfs(nv, v, idx, ord, low, bridge));
                if(low[nv]>ord[v]) bridge.emplace_back(min(v, nv), max(v, nv));
            }
            else chmin(low[v], ord[nv]);
        }
        return low[v];
    }
 
    vector<pii> get_bridge(){
        vector<int> ord(n, -1), low(n, -1);
        vector<pii> bridge;
        int idx=0;
        bridge_dfs(0, -1, idx, ord, low, bridge);
        sort(bridge.begin(), bridge.end());
        bridge.erase(unique(bridge.begin(), bridge.end()), bridge.end());
        return bridge;
    }
 
    int art_dfs(int v, int prev, int &idx, vector<int> &ord, vector<int> &low, vector<int> &art){
        ord[v]=low[v]=idx++;
        for(auto &e: G[v])if(e.to!=prev){
            int nv=e.to;
            if(ord[nv]<0){
                chmin(low[v], art_dfs(nv, v, idx, ord, low, art));
                if((prev<0 && ord[nv]!=1) || (prev>=0 && low[nv]>=ord[v])){
                    art.push_back(v);
                }
            }
            else chmin(low[v], ord[nv]);
        }
        return low[v];
    }
 
    vector<int> get_art(){
        vector<int> ord(n, -1), low(n, -1), art;
        int idx=0;
        art_dfs(0, -1, idx, ord, low, art);
        sort(art.begin(), art.end());
        art.erase(unique(art.begin(), art.end()), art.end());
        return art;
    }
};

int main(){
    int n, m, X, Y;
    scanf("%d%d%d%d", &n, &m, &X, &Y);
    --X; --Y;
    Graph<double> gr(n);
    Polygon p(n);
    rep(i, n) scanf("%lf%lf", &p[i].x, &p[i].y);
    rep(i, m){
        int a, b;
        scanf("%d%d", &a, &b);
        --a; --b;
        gr.add_edge(a, b, abs(p[a]-p[b]));
        gr.add_edge(b, a, abs(p[a]-p[b]));
    }
    auto d = gr.dijkstra(X);
    printf("%.10lf\n", d[Y]);
}
0