Cod sursa(job #2289717)

Utilizator MoldooooooooMoldoveanu Stefan Moldoooooooo Data 25 noiembrie 2018 08:41:10
Problema Algoritmul lui Dijkstra Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.17 kb
#include <cstdio>
#include <cmath>
#include <queue>
#include <vector>
#define MaxN 102
#define min(a, b) (a<b?a:b)
using namespace std;
struct Coords{
    float x1;
    float y1;
    float x2;
    float y2;
    float x3;
    float y3;
};
struct Pair{
    int a;
    int b;
};
Coords List[MaxN];
Pair Work[MaxN];
int N, i, j, K, V, T, T1, T2, Head[MaxN], Count[MaxN], P;
double D[MaxN][MaxN], Sum;
bool Use[MaxN];
struct comp{
    bool operator()(Pair x, Pair y){
        return (D[x.a][x.b]>D[y.a][y.b]);
    }
};
priority_queue<Pair, vector<Pair>, comp> Q;
double Dist(double a, double b, double c, double d);
int FindHead(int i);
void DFS(int i);
int main()
{
    freopen("elicoptere.in", "r", stdin);
    freopen("elicoptere.out", "w", stdout);
    scanf("%d", &T);
    scanf("%d%d", &N, &K);
    for(i=1; i<=N; ++i) {scanf("%f%f%f%f%f%f", &List[i].x1, &List[i].y1, &List[i].x2, &List[i].y2, &List[i].x3, &List[i].y3); D[i][i]=K+1; Head[i]=-1; Count[i]=1;}
    for(i=1; i<N; ++i)
    for(j=i+1; j<=N; ++j){
        D[i][j]=min(Dist(List[i].x1, List[i].y1, List[j].x1, List[j].y1), min(Dist(List[i].x1, List[i].y1, List[j].x2, List[j].y2), min(Dist(List[i].x1, List[i].y1, List[j].x3, List[j].y3), min(Dist(List[i].x2, List[i].y2, List[j].x1, List[j].y1), min(Dist(List[i].x2, List[i].y2, List[j].x2, List[j].y2), min(Dist(List[i].x2, List[i].y2, List[j].x3, List[j].y3), min(Dist(List[i].x3, List[i].y3, List[j].x1, List[j].y1), min(Dist(List[i].x3, List[i].y3, List[j].x2, List[j].y2), Dist(List[i].x3, List[i].y3, List[j].x3, List[j].y3)))))))));
        D[j][i]=D[i][j];
}
    for(i=1; i<=N; ++i){
        if(Use[i]==false){
            V=0; P=1;
            DFS(i);
            if(T==2) T2+=V*(V-1)/2;
            else if(T==1 && V>1) T1+=(V-1);
            else{
                j=0; int p=0;
                while(!Q.empty()){
                    Work[++j]=Q.top();
                    Q.pop();
                }
                for(j=1; p<P-1; ++j){
                    int x=FindHead(Work[j].a), y=FindHead(Work[j].b);
                    if(x!=y){
                        ++p;
                        Sum+=D[Work[j].a][Work[j].b];
                        if(Count[x]>Count[y]){
                            Count[x]+=Count[y];
                            Head[y]=x;
                        }
                        else{
                            Count[y]+=Count[x];
                            Head[x]=y;
                        }
                    }
                }
            }
        }
    }
    if(T==1)printf("%d", T1);
    else if(T==2)printf("%d", T2);
    else printf("%.4f", Sum);
}
double Dist(double a, double b, double c, double d){
    if(a==c) return abs(d-b);
    if(b==d) return abs(a-c);
    return 1000005;
}
void DFS(int i){
    int j; ++V;
    Use[i]=true;
    for(j=1; j<=N; ++j){
        if(D[i][j]<=K && Use[j]==false){
            Pair a; a.a=i; a.b=j;
            Q.push(a); ++P;
            DFS(j);
        }
        else if(D[i][j]<=K){
            Pair a; a.a=i; a.b=j;
            Q.push(a);
        }
    }
    return;
}
int FindHead(int i){
    while(Head[i]!=-1) i=Head[i];
    return i;
}