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; }
相关文章推荐
- poj 1127 Jack Straws(两线相交,并查集)
- Java工具类之Apache的Commons Lang和BeanUtils
- 分布式消息队列XXL-MQ
- 博客已迁移至gzxultra.github.io
- 登陆器列表
- Android数据访问(一)——Asserts
- 七个对我最好的职业建议(精简版)
- java强制类型转换
- STL树
- CodeForces 261C Maxim and Matrix [规律题]
- ios开发系列之页面跳转
- JS中通过建立类来防止方法重载
- 匈牙利算法详解
- poj 1458 Common Subsequence (最长公共子序列)
- android power--wakelock framework
- Hadoop教程(三): MR重要运行参数
- javascript入门
- Ubuntu 下载 & 编译 Android5.1 源码
- 最短路-Dijkstra算法
- 《大道至简》第五章读后感