HDU4305:Lightning(生成树计数+判断点是否在线段上)
Posted heyuhhh
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU4305:Lightning(生成树计数+判断点是否在线段上)相关的知识,希望对你有一定的参考价值。
Lightning
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2465 Accepted Submission(s): 912
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4305
Description:
There are N robots standing on the ground (Don‘t know why. Don‘t know how).
Suddenly the sky turns into gray, and lightning storm comes! Unfortunately, one of the robots is stuck by the lightning!
So it becomes overladen. Once a robot becomes overladen, it will spread lightning to the near one.
The spreading happens when:
Robot A is overladen but robot B not.
The Distance between robot A and robot B is no longer than R.
No other robots stand in a line between them.
In this condition, robot B becomes overladen.
We assume that no two spreading happens at a same time and no two robots stand at a same position.
The problem is: How many kind of lightning shape if all robots is overladen? The answer can be very large so we output the answer modulo 10007. If some of the robots cannot be overladen, just output -1.
Input:
There are several cases.
The first line is an integer T (T < = 20), indicate the test cases.
For each case, the first line contains integer N ( 1 < = N < = 300 ) and R ( 0 < = R < = 20000 ), indicate there stand N robots; following N lines, each contains two integers ( x, y ) ( -10000 < = x, y < = 10000 ), indicate the position of the robot.
Output:
One line for each case contains the answer.
Sample Input:
3 3 2 -1 0 0 1 1 0 3 2 -1 0 0 0 1 0 3 1 -1 0 0 1 1 0
Sample Output:
3 1 -1
题意:
在一个二维平面中给出每个人的坐标,问有多少种方式能让所有人都被雷p。
被雷p还有条件,假如第i个人被p了,离他不超过R距离的人也都会被P,并且他们之中没有其它人。
题解:
为什么这么丧心病狂要算有多少种方式能让所有人都被p。。
做法就是按照条件建边,然后就是生成树计数裸题了。。
代码如下:
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <cmath> using namespace std; typedef long long ll; const int N = 305,MOD = 10007; int t; int n,r; struct Point{ int x,y; }p[N]; double dis(int x,int y){ return sqrt((double)(p[x].x-p[y].x)*(p[x].x-p[y].x)+(double)(p[x].y-p[y].y)*(p[x].y-p[y].y)); } int check(int p1,int p2){ int x1=min(p[p1].x,p[p2].x),x2=max(p[p1].x,p[p2].x); if(x1==x2){ for(int i=1;i<=n;i++){ if(i==p1||i==p2) continue ; if(p[i].x==x1){ if(p[i].y>=min(p[p1].y,p[p2].y) && p[i].y<=max(p[p1].y,p[p2].y)) return 0; } } return 1; } for(int i=1;i<=n;i++){ if(i==p1||i==p2||p[i].x<x1||p[i].x>x2) continue ; double K = (double)(p[p2].y-p[p1].y)/(p[p2].x-p[p1].x); if(K==(double)(p[p2].y-p[i].y)/(p[p2].x-p[i].x)) return 0; } return 1; } ll b[N][N]; int g[N][N]; ll Det(int n){ int i,j,k; ll ret = 1; for(i=2;i<=n;i++){ for(j = i+1;j <= n;j++){ while(b[j][i]){ ll tmp=b[i][i]/b[j][i];//不存在除不尽的情况 for(k = i;k <= n;k++){ b[i][k] = (b[i][k] - tmp*b[j][k])%MOD; if(b[i][k]<0) b[i][k]+=MOD; } swap(b[i],b[j]); ret = -ret; } } if(!b[i][i]) return -1; ret = ret * b[i][i]%MOD; } if(ret < 0) ret += MOD; return ret; } int main(){ scanf("%d",&t); while(t--){ scanf("%d%d",&n,&r); memset(g,0,sizeof(g)); memset(b,0,sizeof(b)); for(int i=1;i<=n;i++) scanf("%d%d",&p[i].x,&p[i].y); for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ if(i==j) continue ; if(check(i,j)&&dis(i,j)<=r) g[i][j]=g[j][i]=1; } } for(int i=1;i<=n;i++){ for(int j=i+1;j<=n;j++){ if(g[i][j]){ b[i][i]++;b[j][j]++; b[i][j]=b[j][i]=-1; } } } cout<<Det(n)<<endl; } return 0; }
以上是关于HDU4305:Lightning(生成树计数+判断点是否在线段上)的主要内容,如果未能解决你的问题,请参考以下文章