Uva 1050 Ars Longa
Description
You have been struck with inspiration, and are designing a beautiful new art sculpture for the foyer of your local museum. For highly important artistic reasons, you are designing it using very specific materials. However, you are not sure if physics is on your side. Will your sculpture actually stand up?
The sculpture will be composed of various ball joints weighing 1 kilogram each, and various rods (of negligible weight) connecting the joints. Rods cannot be stretched or compressed, and they can never detach from a joint. However, they are free to rotate around the joints in any direction. The joints that lie on the ground are glued in place; all others are free to move. For simplicity, you may ignore the effects of intersections of rods; each rod exerts force only on the 2 joints connected to it. Also, any joint that is in the air will have at least one rod coming out that is not parallel to the ground. This prevents the degenerate case where a ball is supported only horizontally by a rigid structure. In real life, it would sag just a little.
Write a program to determine whether your structure is static (that is, will not immediately move from the effects of gravity). Note that each rod can transmit an arbitrarily large tensional force along its length, and that “being static” means that the tensional forces at each joint balance the weight of the joint.
If the structure is static, you must also determine whether it is stable (that is, will not move if perturbed slightly by pulling its joints).
Input
The input contains several sculpture descriptions. Every description begins with a line containing integers \(J\) and \(R\), the number of joints and rods in the structure, respectively. Joints are numbered from \(1\) to \(J\). The description continues with \(J\) lines, one per joint, each containing \(3\) floating point numbers giving the \(x,y,z\) coordinates of that joint. Following are \(R\) lines, one per rod, with \(2\) distinct integers indicating the joints connected by that rod.
Each rod is exactly the right length to connect its joints. The \(z\) coordinates will always be nonnegative; a \(z\) coordinate of \(0\) indicates that the joint is on the ground and fixed in place. There are at most \(100\) joints and \(100\) rods.
The last description is followed by a line containing two zeroes.
Output
For each sculpture description, output ‘NON-STATIC’, ‘UNSTABLE’, or ‘STABLE’, as shown in the sample output.
Sample Input
4 3
0 0 0
-1.0 -0.5 1.0
1.0 -0.5 1.0
0 1.0 1.0
1 2
1 3
1 4
0 0
Sample Output
Sculpture 1: NON-STATIC
将每根杆子当做变量。
对于不在地面上的每个小球,我们可以列出一个方程。即每根与他相连的杆子给球的力的矢量和为\((0,0,1)\)(注意:相互作用力等大反向),由于是个三元组,我们需要把他拆成三个方程,对\(x,y,z\)分别列即可。最后是否静止只需判断方程有没有解即可。
对于是否稳定,即给予一个外力,方程是否还有解。但是在原方程上调整值复杂度太高,不可行。我们考虑问题的本质,即我们只改变常量,未知量的系数什么的都不变。因此,若消元后方程出现了\(0=0\)的情况,改变右边常量,方程明显无解了,反之肯定有解(可用增广矩阵来理解)。
#include<cstring>
#include<cmath>
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
#define eps (1e-8)
#define maxn (310)
int J,R,tot,con[maxn][maxn],T; double val[maxn][maxn];
struct Node
{
double x,y,z;
inline void read() { scanf("%lf %lf %lf",&x,&y,&z); }
friend inline Node operator -(const Node &a,const Node &b) { return (Node){ a.x-b.x,a.y-b.y,a.z-b.z }; }
}joints[maxn];
inline int guass()
{
int now = 0;
for (int i = 1,j;i <= R;++i)
{
for (j = now+1;j <= tot;++j) if (fabs(val[j][i]) > eps) break;
if (j == tot+1) continue; ++now;
for (int k = 1;k <= R+1;++k) swap(val[now][k],val[j][k]);
for (j = 1;j <= tot;++j)
{
if (j == now) continue; double t = val[j][i]/val[now][i];
for (int k = 1;k <= R+1;++k) val[j][k] -= t*val[now][k];
}
}
for (int i = now+1;i <= tot;++i) if (fabs(val[i][R+1]) > eps) return 0;
if (now < tot) return 1; return 2;
}
int main()
{
freopen("1050.in","r",stdin);
freopen("1050.out","w",stdout);
while (++T)
{
tot = 0; memset(val,0,sizeof(val)); memset(con,0,sizeof(con));
scanf("%d %d\n",&J,&R); if (!J) break;
for (int i = 1;i <= J;++i) joints[i].read();
for (int i = 1,a,b;i <= R;++i) scanf("%d %d",&a,&b),con[a][b] = con[b][a] = i;
for (int i = 1;i <= J;++i)
{
if (fabs(joints[i].z) <= eps) continue; tot += 3;
for (int j = 1;j <= J;++j)
{
if (!con[i][j]) continue; int id = con[i][j];
Node vec = joints[i]-joints[j];
val[tot-2][id] = vec.x,val[tot-1][id] = vec.y,val[tot][id] = vec.z;
}
val[tot][R+1] = 1;
}
int res = guass();
if (!res) printf("Sculpture %d: NON-STATIC\n",T);
else if (res == 1) printf("Sculpture %d: UNSTABLE\n",T);
else printf("Sculpture %d: STABLE\n",T);
}
fclose(stdin); fclose(stdout);
return 0;
}