我们可以通过将任意点相连跑spfa就可以了。我这里没有直接建图,我每次直接将spfa中现在所要探索的点遍历,因为是要和所有点相连。另外,这道题比较坑的地方是,给定的三个点,不一定是平行的正着的。也就是说,我们没办法通过枚举3个点的位置求出来第四个点的坐标(血的教训)。所有我们只能通过数学方法求出第四个点。
Code:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <queue>
using namespace std;
pair<int, int> f[5001];
int ended[10001], n, m, start;
int es;
double dis[10001];
double jl(double x1, double y1, double x2, double y2)
{
return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
int getcity(int x)
{
return x / 4 + 1 - !(x % 4);
}
double spfa(int s, int e)
{
queue<int> q;
q.push(s);
for(int i = 1; i <= 4 * n; i ++ )
{
dis[i] = 9999.99;
}
dis[s] = 0;
while(!q.empty())
{
int t = q.front();
q.pop();
for(int i = 1; i <= 4 * n; i ++)
{
int f1 = getcity(t);
int f2 = getcity(i);
double v = 0;
if(f1 == f2)
{
v = ended[f1] * jl(f[t].first, f[t].second, f[i].first, f[i].second);
}
else
{
v = m * jl(f[t].first, f[t].second, f[i].first, f[i].second);
}
if(dis[t] + v < dis[i])
{
dis[i] = dis[t] + v;
q.push(i);
}
}
}
double res = 214444.44;
for(int i = (e - 1) * 4 + 1; i <= e * 4; i ++ )
{
res = min(res , dis[i]);
}
return res;
}
pair<int, int> get4(int x1, int y1, int x2, int y2, int x3, int y3)
{
int ab = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
int ac = (x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3);
int bc = (x2 - x3) * (x2 - x3) + (y2 - y3) * (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;
return make_pair(x4, y4);
}
int we(int x, int y)
{
return (x - 1) * 4 + y;
}
int main()
{
int t;
scanf("%d", &t);
while( t -- )
{
scanf("%d%d%d%d", &n, &m, &start, &es);
for(int i = 1; i <= n; i ++ )
{
for(int j = 1; j <= 3; j ++ )
{
scanf("%d%d", &f[we(i, j)].first, &f[we(i, j)].second);
}
f[we(i, 4)] = get4(f[we(i, 1)].first, f[we(i, 1)].second, f[we(i, 2)].first, f[we(i, 2)].second, f[we(i, 3)].first, f[we(i, 3)].second);
scanf("%d", &ended[i]);
}
double res = 99999.99;
for(int i = (start - 1) * 4 + 1; i <= (start - 1) * 4 + 4; i ++ )
{
res = min(res, spfa(i, es));
}
printf("%.1f \n", res);
}
}