您的位置:首页 > 其它

poj 1127 Jack Straws 几何 + 弗洛伊德

2015-10-28 20:25 337 查看
//	poj 1127 Jack Straws  几何 + 弗洛伊德
//
//	解题思路:
//		两两之间,如果相连,则连一条边,最后用弗洛伊德求闭包.
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#define For(x,a,b,c) for (int x = a; x <= b; x += c)
#define Ffor(x,a,b,c) for (int x = a; x >= b; x -= c)
#define cls(x,a) memset(x,a,sizeof(x))
using namespace std;
typedef long long ll;

const double PI = acos(-1.0);
const double EPS = 1e-10;
const int MAX_N = 30;

int N,M;

bool g[MAX_N][MAX_N];

double add(double a,double b){
if (abs(a+b) < EPS*(abs(a) + abs(b)))	return 0;
return a + b;
}

struct P{
double x,y;
P() {}

P(double x,double y) : x(x), y(y) {
}
P operator + (P p){
return P(add(x,p.x),add(y,p.y));
}

P operator - (P p){
return P(add(x,-p.x),add(y,-p.y));
}

P operator * (double d){
return P(x * d,y * d);
}

double dot(P p){
return add(x * p.x,y * p.y);
}

double det(P p){
return add(x * p.y, - y * p.x);
}
}p[MAX_N],q[MAX_N];

P a[MAX_N];

bool on_seg(P p1, P p2, P q){
return (p1 - q).det(p2 - q) == 0 && (p1 - q).dot(p2 - q) <= 0;
}

P intersection(P p1, P p2, P q1, P q2){
return p1 + (p2 - p1) * ((q2 - q1).det(q1 - p1) / (q2 - q1).det(p2 - p1));
}

void input(){
cls(g,0);
For(i,1,N,1){
scanf("%lf%lf%lf%lf",&p[i].x,&p[i].y,&q[i].x,&q[i].y);
}
}

void floyd(){
For(k,1,N,1){
For(i,1,N,1){
For(j,1,N,1){
g[i][j] |= g[i][k] && g[k][j];
}
}
}
}

void print(){
For(i,1,N,1){
For(j,1,N,1){
printf("%d ",g[i][j] ? 1 : 0);
}
puts("");
}
}

void solve(){
for (int i = 1 ; i <= N; i ++){
g[i][i] = 1;
for (int j = 1; j < i; j ++){
if ((p[i] - q[i]).det(p[j] - q[j]) == 0){
g[i][j] = g[j][i] = on_seg(p[i],q[i],p[j])
|| on_seg(p[i],q[i],q[j])
|| on_seg(p[j],q[j],p[i])
|| on_seg(p[j],q[j],q[i]);
}else {
P r = intersection(p[i],q[i],p[j],q[j]);
g[i][j] = g[j][i] = on_seg(p[i],q[i],r) && on_seg(p[j],q[j],r);
}
//printf("%d %d %d %d\n",p[i].x,p[i].y,p[j].x,p[j].y);
//printf("%d\n",g[i][j] ? 1 : 0);
}
}
floyd();
while(1){
int a,b;
scanf("%d%d",&a,&b);
if (!a && !b)
break;
if (g[a][b]){
puts("CONNECTED");
}else {
puts("NOT CONNECTED");
}
}

}

int main(){
freopen("1.in","r",stdin);
while(scanf("%d",&N)!=EOF){
if (!N)
break;
input();
solve();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息