HDU4305 Lightning

本文探讨了一种特殊场景下,即多个机器人遭受闪电袭击时,如何通过计算生成树的数量来确定可能的闪电传播形态。利用图论、矩阵树定理和模方程,通过O(n^3)算法直接判断机器人间的连接条件,最终使用高斯消元法计算矩阵行列式,输出形态数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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. 

InputThere 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.
OutputOne 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

 

题目配图戳中笑点2333

大意:给出n个点,求生成树数量,两个点之间有无向边需要满足:两点距离小于给定的R,且两点所连线段上没有其他点

 

 

图论 矩阵树定理 模方程

n只有300,于是直接O(n^3)暴力判断两点间是否有边(判距离+判斜率)

建出Matrix-Tree定理的矩阵后,高斯消元计算矩阵行列式的值,输出答案,无解输出-1

注意到矩阵是处在模意义下的,又去学了一下高斯消元解线性模方程组

↑全程循环着シカコ的《胃が痛いんだなぁ》,灵感泉涌,一次AC,带感

  ↑这歌真的很棒呢(跑题)

 

  1 /*by SilverN*/
  2 #include<iostream>
  3 #include<algorithm>
  4 #include<cstring>
  5 #include<cstdio>
  6 #include<cmath>
  7 using namespace std;
  8 const int mod=10007;
  9 const double eps=1e-7;
 10 const int mxn=330;
 11 int read(){
 12     int x=0,f=1;char ch=getchar();
 13     while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();}
 14     while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
 15     return x*f;
 16 }
 17 struct block{
 18     int x,y;
 19 }a[mxn];
 20 int n,R;
 21 int D;
 22 int G[mxn][mxn];
 23 bool vis[mxn];
 24 void exgcd(int a,int b,int &x,int &y){//求逆元用 
 25     if(!b){x=1;y=0;return;}
 26     exgcd(b,a%b,x,y);
 27     int t=x;x=y;y=t-a/b*x;
 28     return;
 29 }
 30 int Gauss(){//Matrix-tree定理 高斯消元 
 31     int i,j,x,y;
 32     int ans=1;
 33     for(i=1;i<n;i++){
 34         int p=i;
 35         if(!G[i][i]){
 36             for(j=i+1;j<n;j++)if(G[j][i]>G[p][i])p=j;
 37             if(p==i){return -1;}//无解 
 38             for(j=1;j<n;j++)swap(G[p][j],G[i][j]);
 39             ans=-ans;
 40         }
 41         exgcd(G[i][i],mod,x,y);
 42         x=((x%mod)+mod)%mod;
 43         for(int k=i+1;k<n;k++)(G[i][k]*=x)%=mod;
 44         for(j=i+1;j<n;j++){
 45             if(G[j][i]){
 46                 for(int k=i+1;k<n;k++){
 47                     G[j][k]=((G[j][k]-G[i][k]*G[j][i])%mod+mod)%mod;
 48                 }
 49             }
 50         }
 51         ans=ans*G[i][i]%mod;
 52     }
 53     return ans;
 54 }
 55 inline int dist(int i,int j){//距离 
 56     return (a[i].x-a[j].x)*(a[i].x-a[j].x)+(a[i].y-a[j].y)*(a[i].y-a[j].y);
 57 }
 58 inline double ck(int i,int j){//斜率 
 59     return ((double)a[i].y-a[j].y)/(double)(a[i].x-a[j].x);
 60 }
 61 bool check(int x,int y){
 62     int dis=dist(x,y);
 63     if(dis>D)return 0;//距离大于R
 64     double k=ck(x,y);
 65     for(int i=1;i<=n;i++){
 66         if(i==y || i==x)continue;
 67         if(dist(x,i)<dis && fabs(ck(x,i)-k)<eps)return 0;//连线上有其他机器人 
 68     }
 69     return 1;
 70 }
 71 void solve(){
 72     int i,j,k;
 73     for(i=1;i<=n;i++)
 74         for(j=i+1;j<=n;j++){
 75              if(check(i,j)){
 76                  ++G[i][i];    ++G[j][j];
 77                  --G[i][j];     --G[j][i];
 78                  vis[i]=vis[j]=1;
 79              }
 80         }
 81     for(i=1;i<=n;i++)
 82         if(!vis[i]){printf("-1\n");return;}
 83     for(i=1;i<=n;i++)
 84         for(j=1;j<=n;j++){
 85             G[i][j]=((G[i][j]%mod)+mod)%mod;
 86         }
 87     printf("%d\n",Gauss());
 88     return;
 89 }
 90 void init(){
 91     memset(G,0,sizeof G);
 92     memset(vis,0,sizeof vis);
 93     return;
 94 }
 95 int main(){
 96     int i,j;
 97     int T=read();
 98     while(T--){
 99         init();
100         n=read();R=read();D=R*R;
101         for(i=1;i<=n;i++){a[i].x=read();a[i].y=read();}
102         solve();
103     }
104     return 0;
105 }

 

转载于:https://www.cnblogs.com/SilverNebula/p/6408106.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值