Triangles
1010: Triangles
Time Limit: 2 Sec Memory Limit: 128 MB
Submit: 18
Solved: 8
Description
You are given a figure consisting of n points in a 2D-plane and m segments connecting some of them. We guarantee that any two segments don’t share points except their ends and there’s no more than one segment between the same pair of points. Please count the total number of triangles in the given figure.
Input
There’re multiple test cases. In each case:
The first line contains two positive integers n and m. (n ≤ 200, m ≤ 20000)
Each of the following n lines contains two real numbers xi and yi indicating the coordinates of the i-th point. (−100000 < xi, yi < 100000)
Each of the following m lines contains four real numbers xi, yi, xj, yj . It means (xi,yi) and (xj,yj) are connected by a segment. We guarantee that these points are part of the given n points.
Output
For each test case, print a single line contains the total number of triangles in the given figure.
Sample Input
Sample Output
#include<iostream>
#include<stdio.h>
#include<cmath>
#include<map>
#include<cstring>
using namespace std;
const unsigned int MAX=200;
#define ERR 0.000001
struct Point
{
double x,y;
}point[MAX+10];
int edge[MAX+10][MAX+10];
map <double,int> mymap;
bool in_line(Point a1,Point a2,Point a3)//判断是否共线
{
if(fabs((a2.x-a1.x)*(a3.y-a2.y)-(a2.y-a1.y)*(a3.x-a2.x))<=ERR)
return true;
return false;
}
int main()
{
//freopen("Triangles.in","r",stdin);
int m,n,i,j,k,ans;
int u,v;
while(scanf("%d%d",&n,&m)!=EOF)
{
mymap.clear();
memset(edge,0,sizeof(edge));
ans=0;
for(i=0;i<n;i++)
{
scanf("%lf%lf",&point[i].x,&point[i].y);
mymap[point[i].x*20000+point[i].y]=i;
}
for(i=0;i<m;i++)
{
double p1,q1,p2,q2;
scanf("%lf%lf%lf%lf",&p1,&q1,&p2,&q2);
u=mymap[p1*20000+q1];
v=mymap[p2*20000+q2];
//printf("u==%d v==%d\n",u,v);
edge[u][v]=edge[v][u]=1;//点与线之间联系起来
}
/*for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
printf("edge[%d][%d]=%d ",i,j,edge[i][j]);
}
printf("\n");
}*/
for(i=0;i<n;i++)
for(j=0;j<n;j++)
for(k=0;k<n;k++)
if(i!=j&&j!=k&&i!=k&&edge[j][i]&&edge[i][k]&&!edge[j][k]&&in_line(point[i],point[j],point[k]))
edge[j][k]=edge[k][j]=1;//三个点中,有两条连线,并且三点共线,加一条连线
/*for(i=0;i<n;i++)//这种方法貌似可以,并且复杂度较低,但就是通不过,不知为啥
for(j=i+1;j<n;j++)
for(k=j+1;k<n;k++)
{
//printf("point[%d] x=%lf y=%lf ",i,point[i].x,point[i].y);
//printf("point[%d] x=%lf y=%lf ",j,point[j].x,point[j].y);
//printf("point[%d] x=%lf y=%lf \n",k,point[k].x,point[k].y);
if(in_line(point[i],point[j],point[k]))
{
if((edge[i][j]&&(edge[j][k]||edge[i][k]))||(edge[j][k]&&edge[i][k]))
edge[i][j]=edge[i][k]=edge[j][k]=edge[j][i]=edge[k][i]=edge[k][j]=1;
}
}*/
for(i=0;i<n;i++)//扫描所有点,三点两两之间有连线,且不共线,则组成三角形
for(j=i+1;j<n;j++)
for(k=j+1;k<n;k++)
{
if(edge[i][j]&&edge[i][k]&&edge[j][k]&&!in_line(point[i],point[j],point[k]))
ans++;
}
printf("%d\n",ans);
}
return 0;
}
FROM:暑假训练第二场
Triangles的更多相关文章
- Count the number of possible triangles
From: http://www.geeksforgeeks.org/find-number-of-triangles-possible/ Given an unsorted array of pos ...
- [ACM_搜索] Triangles(POJ1471,简单搜索,注意细节)
Description It is always very nice to have little brothers or sisters. You can tease them, lock them ...
- acdream.Triangles(数学推导)
Triangles Time Limit:1000MS Memory Limit:64000KB 64bit IO Format:%lld & %llu Submit Stat ...
- UVA 12651 Triangles
You will be given N points on a circle. You must write a program to determine how many distinctequil ...
- Codeforces Gym 100015F Fighting for Triangles 状压DP
Fighting for Triangles 题目连接: http://codeforces.com/gym/100015/attachments Description Andy and Ralph ...
- Codeforces Round #309 (Div. 1) C. Love Triangles dfs
C. Love Triangles Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/553/pro ...
- Codeforces Round #308 (Div. 2) D. Vanya and Triangles 水题
D. Vanya and Triangles Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/55 ...
- Project Euler 94:Almost equilateral triangles 几乎等边的三角形
Almost equilateral triangles It is easily proved that no equilateral triangle exists with integral l ...
- Project Euler 91:Right triangles with integer coordinates 格点直角三角形
Right triangles with integer coordinates The points P (x1, y1) and Q (x2, y2) are plotted at integer ...
- Project Euler 75:Singular integer right triangles
题目链接 原题: It turns out that 12 cm is the smallest length of wire that can be bent to form an integer ...
随机推荐
- Compiler options do not specify -mv64+, but configuration is for C64x+
2013-06-20 10:02:47 错误报告: "pin_connect_cfg.s62", ERROR! at line 365: [ ***** USER ERROR ...
- 主机头部分 www有和无是有区别的
关于主机部分www的问题: case 1: frontend web_service bind *:80 bind *:443 ssl crt /etc/haproxy/cert.pem acl ww ...
- linux中应用程序main函数中没有开辟进程的,它应该在那个进程中运行呢?
1.main函数是一个进程还是一个线程? 不知道你是用c创建的,还是用java创建的. 因为它们都是以main()做为入口开始运行的. 是一个线程,同时还是一个进程. 在现在的操作系统中,都是多线程的 ...
- 【HDOJ】4579 Random Walk
1. 题目描述一个人沿着一条长度为n个链行走,给出了每秒钟由i到j的概率($i,j \in [1,n]$).求从1开始走到n个时间的期望. 2. 基本思路显然是个DP.公式推导也相当容易.不妨设$dp ...
- poj 2240 Arbitrage (最短路 bellman_ford)
题目:http://poj.org/problem?id=2240 题意:给定n个货币名称,给m个货币之间的汇率,求会不会增加 和1860差不多,求有没有正环 刚开始没对,不知道为什么用 double ...
- linux 上下文切换带来的影响
1.保存CPU寄存器中的内容 2.CPU高速缓存中的内容失效 3.重新装载页表,用于给线程程安装一个新的虚拟地址空间,页表缓存失效
- jquery放大镜
效果体验:http://runjs.cn/detail/dvygyp5t demo下载 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tra ...
- HDU 4276-The Ghost Blows Light(树状背包)
题意: n个房间,每个有一定的钱,一个房间到另一个房间花费一定的时间,给你房间连接树,求在t时间内到达房间m能得到的最大钱数(从房间1(根)出发) 分析: 该题关键是最后要到达m,没有这个条件,就是基 ...
- 移动端混合型App(hybrid app)自动化测试选型与实践
背景 公司产品的业务已经发展到了移动端,开发选型已经结束,决定使用phonegap做移动端的web应用开发平台.考虑到业务的复杂与多样,移动端的测试同样需要自动化.在网上看了很多,最终锁定了3个移动端 ...
- 同样的JS写法,为啥只有IE9模式正常?
使用 IE F12 开发者工具,选择不同的“文档模式” 从IE7 - IE9,只有IE9正常! <!DOCTYPE html> <html> <script type=& ...