题意:在一个三维空间内有n个小行星(n<50), 每个小行星的初始状态由(x,y,z,vx,vy,vz) 表示, 其中(x,y,z)是初始位置,(vx,vy,vz)是小行星恒定的速度。你要在行星之间建立联系,连接两个行星的费用为两个行星间的距离。要求你用最少的connection数量,建立行星之间的联系,使得每两个行星都可直接或间接通过connection联系彼此。其次要求花费最少。不过因为行星在运动,时不时需要更改所建立的体系。请你求出最初建立体系加上后面更改体系的总次数。保证初始时建立联系的方式时唯一的,而且保证如果一个系统在t时刻成为最优的系统,那么在t+1e-6之前,该系统都是最优的。
方法:MST, Brute Force
首先,因为要用最少的connection建立网络系统,所以要建立一个生成树Spanning Tree;其次要求花费最少,那么就要建立一棵Minimum Spanning Tree (MST)。这里我用的是Kruskal's algorithm 和并查集DSU,复杂度大约是O(E*log(E)), E为边数,本题也就等于n*(n-1)/2。
下面我们想,什么时候可能要更改原有的MST呢?那就是边之间的大小关系发生变化时,进一步说,当两条边e1, e2之间的大小关系在时刻t前后发生变化(在t时刻e1和e2的长度相等),t之前有e1 < e2, t之后有 e1 > e2, 而且e1在t之前的MST, e2不再t之前的MST之内,那么我们在建立MST的时候,就会先考虑e2,再考虑e1,因此建立的生成树有可能会改变。一共有n*(n-1)/2 条边,任意两条边之间大小发生变化的时刻至多有2个,那么最多可能的时刻的数量大约是 (n*(n-1)/2)*(n*(n-1)/2-1)/2 * 2, O(n^4)。当然并不是所有的时刻都满足我们所说的可能会产生新的MST的条件(大约O(n^3)),所以总的时间复杂度大约在O(n^5) (这个复杂度不是特别准确,希望大家可以纠正我的错误)。
code:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <bitset>
#include <cstdlib>
#include <cmath>
#include <set>
#include <list>
#include <deque>
#include <map>
#include <queue>
#include <fstream>
#include <cassert>
#include <cmath>
#include <sstream>
#include <time.h>
#include <complex>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define FOR(a,b,c) for (int (a)=(b);(a)<(c);++(a))
#define FORN(a,b,c) for (int (a)=(b);(a)<=(c);++(a))
#define DFOR(a,b,c) for (int (a)=(b);(a)>=(c);--(a))
#define FORSQ(a,b,c) for (int (a)=(b);(a)*(a)<=(c);++(a))
#define FORC(a,b,c) for (char (a)=(b);(a)<=(c);++(a))
#define FOREACH(a,b) for (auto &(a) : (b))
#define rep(i,n) FOR(i,0,n)
#define repn(i,n) FORN(i,1,n)
#define drep(i,n) DFOR(i,n-1,0)
#define drepn(i,n) DFOR(i,n,1)
#define MAX(a,b) a = Max(a,b)
#define MIN(a,b) a = Min(a,b)
#define SQR(x) ((LL)(x) * (x))
#define Reset(a,b) memset(a,b,sizeof(a))
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(v) v.begin(),v.end()
#define ALLA(arr,sz) arr,arr+sz
#define SIZE(v) (int)v.size()
#define SORT(v) sort(all(v))
#define REVERSE(v) reverse(ALL(v))
#define SORTA(arr,sz) sort(ALLA(arr,sz))
#define REVERSEA(arr,sz) reverse(ALLA(arr,sz))
#define PERMUTE next_permutation
#define TC(t) while(t--)
#define forever for(;;)
#define PINF 1000000000000
#define newline '\n'
#define test if(1)if(0)cerr
using namespace std;
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> ii;
typedef pair<double,double> dd;
typedef pair<char,char> cc;
typedef vector<ii> vii;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> l4;
const double pi = acos(-1.0);
int n;
const int maxn = 100;
double a[2][maxn][3]; //input data
double dis[maxn][maxn][3]; //store the coefficients of distance funcition between two points at time t;
bool mst[2][maxn][maxn]; // store the MST information. mst[flag][i][j] == true if edge(i,j) is in MST
bool flag = false; //flag is used to toggle between new MST and old MST
//calculate the square of distance between point i and point j at time t
inline double distance(int i, int j, double t)
{
return dis[i][j][0] + dis[i][j][1]*t + dis[i][j][2]*t*t;
}
//state is the record of two edges changing into the same length
//d is timestamp, u and v are the 2 edges
//first == true if u is becoming smaller after the timestamp
struct state
{
double d;
int u, v;
bool first;
bool operator<(const state &r) const
{
if (abs(d - r.d) < 1e-6) return false;
return d < r.d;
}
};
//the following is DSU structure for constructing mst
int pa[maxn];
int total_cnt;
int findpa(int id)
{
return id==pa[id]?id:pa[id]=findpa(pa[id]);
}
bool merge(int x, int y)
{
x = findpa(x), y = findpa(y);
if (x != y)
{
pa[x] = y; return true;
}
return false;
}
void init(int n)
{
total_cnt = n;
rep(i, n) pa[i] = i;
}
//dsu end
void build_mst(double t)
{
//cerr << "build tree at t = " << t << newline;
flag = !flag;
init(n);
vector<pair<double, int>> edges;
//Kruskal
rep(i, n) for (int j = i+1; j < n; ++j)
{
edges.pb(mp(distance(i, j, t), i*n+j));
}
sort(edges.begin(), edges.end());
Reset(mst[flag], false);
rep(i, edges.size())
{
if (total_cnt == 1) break;
int u = edges[i].second/n, v = edges[i].second%n;
if (merge(u, v))
{
mst[flag][u][v] = mst[flag][v][u] = true;
}
}
}
// intree(e) == true if edge e is in MST
inline bool intree(int e)
{
return mst[flag][e/n][e%n];
}
//different() return true if new MST and old MST are different
inline bool different()
{
rep(i, n)
rep(j, n)
if (mst[0][i][j] != mst[1][i][j]) return true;
return false;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int kase = 0;
while (cin >> n)
{
++kase;
rep(i, n) rep(k, 2) rep(j, 3)
cin >> a[k][i][j];
rep(i, n) for (int j = i+1; j < n; ++j)
{
double temp = 0;
rep(k, 3) temp += pow(a[0][i][k]-a[0][j][k], 2);
dis[i][j][0] = dis[j][i][0] = temp;
temp = 0;
rep(k, 3) temp +=(a[0][i][k]-a[0][j][k])*(a[1][i][k]-a[1][j][k]);
temp *= 2;
dis[i][j][1] = dis[j][i][1] = temp;
temp = 0;
rep(k, 3) temp += pow(a[1][i][k]-a[1][j][k], 2);
dis[i][j][2] = dis[j][i][2] = temp;
}
vector<state> tm;
rep(i, n*n) if (i%n > i/n) for (int j = i+1; j < n*n; ++j) if (j%n > j/n)
{
double c[3];
rep(k, 3) c[k] = dis[i%n][i/n][k]-dis[j%n][j/n][k];
if (c[2] == 0)
{
if (c[1] == 0) continue;
double temp = -c[0]/c[1];
if (temp > 0) tm.pb((state){temp, i, j, c[1] < 0});
continue;
}
double delta = c[1]*c[1] - 4*c[0]*c[2];
if (delta <= 0) continue;
delta = sqrt(delta);
double x1 = (-c[1]+delta)/2/c[2], x2 = (-c[1]-delta)/2/c[2];
bool first = c[2] > 0;
if (x1 > x2) swap(x1, x2);
if (x1 > 0) tm.pb((state){x1, i, j, first});
if (x2 > 0) tm.pb((state){x2, i, j, !first});
}
sort(tm.begin(), tm.end());
int ans = 1;
build_mst(0);
int i = 0;
//cerr << "tm.size() = " << tm.size() << newline;
while (i < tm.size())
{
double start_t = tm[i].d;
bool change = false;
while (i < tm.size() && start_t+1e-6>tm[i].d)
{
if (intree(tm[i].u)^intree(tm[i].v))
{
if (tm[i].first && intree(tm[i].v))
change = true;
if (!tm[i].first && intree(tm[i].u))
change = true;
}
++i;
}
if (change)
{
build_mst(start_t+1e-6);
ans += different();
}
}
cout << "Case " << kase << ": " << ans << newline;
}
}
/*
3
0 0 0 0 0 0
5 0 0 0 0 0
10 1 0 -1 0 0
4
0 0 0 1 0 0
0 1 0 0 -1 0
1 1 1 3 1 1
-1 -1 2 1 -1 -1
*/