One of graph theory training

noip water Series title

https://www.luogu.org/problem/P1027

Title clearly a plurality of sets of data (also <= 10) the shortest single-source ,

Here you can not floyed

But you will find that it is the title track Blue is a grain of truth

You will be very annoyed its construction side:

Use the Pythagorean theorem plus a series of special judge, and also float

Quiet and later,

The idea is simple, trouble codes, put the problem solution amend the move coming

code:

#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
using namespace std;
struct data {
    int x,y;
    int city; 
};
const int maxn=100;
int s,t,A,B;
int T[maxn+1];
double dis[maxn<<2|1];
data a[maxn<<2|1];
int pingfang(int x) { return x*x; }
double juli(int x1, int y1, int x2, int y2) { return sqrt(pingfang(x1-y1)+pingfang(x2-y2)); }
void get_4th(int x1, int y1, int x2, int y2, int x3, int y3, int i) {
    int ab=pingfang(x1-x2)+pingfang(y1-y2),
        ac=pingfang(x1-x3)+pingfang(y1-y3),
        bc=pingfang(x2-x3)+pingfang(y2-y3);
    int x4,y4;
    if (ab+ac==bc) x4=x2+x3-x1, y4=y2+y3-y1;
    if (ab+bc==ac) x4=x1+x3-x2, y4=y1+y3-y2;
    if (ac+bc==ab) x4=x1+x2-x3, y4=y1+y2-y3;
    a[i+3].x=x4;
    a[i+3].y=y4;
}
void init() {
    memset(a,0,sizeof(a));
    scanf("%d%d%d%d",&s,&t,&A,&B);
    for (int i=1; i<=4*s; i+=4) {
        scanf("%d%d%d%d%d%d%d",&a[i].x,&a[i].y,&a[i+1].x,&a[i+1].y,&a[i+2].x,&a[i+2].y,&T[i/4+1]);
        a[i].city=a[i+1].city=a[i+2].city=a[i+3].city=i/4+1;
        get_4th(a[i].x,a[i].y,a[i+1].x,a[i+1].y,a[i+2].x,a[i+2].y,i);
    }
}
void spfa() { 
    bool mark[maxn<<2|1];
    queue <int> q;
    for (int i=1; i<=4*s; i++) dis[i]=99999999.99999;
    for (int i=A*4-3;i<=A*4;i++)
        dis[i]=0, q.push(i), mark[i]=true;
    while (!q.empty()) {
        int x=q.front(); q.pop(); mark[x]=false;
        for (int i=1; i<=4*s; i++) {
            if (i==x) continue;
            double cost=juli(a[x].x,a[i].x,a[x].y,a[i].y);
            if (a[i].city==a[x].city) cost*=T[a[i].city];
            else cost*=t;
            if (dis[x]+cost<dis[i]) {
                dis[i]=dis[x]+cost;
                if (!mark[i])
                    mark[i]=true, q.push(i);
            }
        }
    }
}

int main() {
    int n;
    scanf("%d",&n);
    while (n--) {
        init();
        spfa();
        double ans=dis[B*4];
        for (int i=B*4-3; i<B*4; i++)
            if (dis[i]<ans) ans=dis[i];
        printf("%.1lf",ans);
    }
}

Guess you like

Origin www.cnblogs.com/wzxbeliever/p/11621688.html