-
Notifications
You must be signed in to change notification settings - Fork 0
/
CHEFGAME(prim algo).cpp
113 lines (102 loc) · 2.48 KB
/
CHEFGAME(prim algo).cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* Details - Codechef April'13 based on Prim's algo */
/*
As the graph given in question is complete so Krushkal will time out because It takes ElogE time as E = V*V here so
log factor will be more than prims , another thing here is as we got an edge of 0 len we need to break
*/
#include <bits/stdc++.h>
#define M 66666+5
#define MOD 747474747
#define INF 0x3f3f3f3f
#define s(x) scanf("%d",&x)
#define p(x) printf("%d",x)
#define sl(x) scanf("%lld",&x)
#define pl(x) printf("%lld",x)
#define nl printf("\n");
#define pb push_back
#define mp make_pair
#define mset(x,val) memset(&x,val,sizeof(x))
#define Fr freopen("E://Test//in.txt","r",stdin);
#define Fw freopen("E://Test//out.txt","w",stdout);
#define Frw Fr Fw
using namespace std;
typedef long long ll;
//typedef __uint128_t int128;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef vector<vii> vvii;
int pt[M][5];
bool visit[M];
ll dist[M];
int N , D;
ll getDis(int a , int b)
{
ll dis = 0;
for (int i = 0; i < D; ++i)
{
dis += (ll)(pt[a][i] - pt[b][i])*(pt[a][i] - pt[b][i]);
}
return dis;
}
ll prim()
{
ll maxDis , dis;
int idx = -1 , cur;
vector<ll> edges;
memset(visit , false , sizeof(visit));
visit[0] = true;
cur = 0;
// here prim's algo is used - first from one point distance is given to every point then after we choose maximum
// among them , visit it and update all other vertices according to this vertex and keep this maxDis as taken edge
for(int i = 1; i < N; ++i)
dist[i] = getDis(0 , i);
while(true)
{
maxDis = 0;
for (int i = 0; i < N; ++i) // choosing maximum
{
if(!visit[i])
{
if(dist[i] > maxDis)
{
maxDis = dist[i];
idx = i;
}
}
}
if(maxDis == 0) break;
visit[idx] = true;
for (int i = 0; i < N; ++i) // updating other vertices according to this point
{
if(!visit[i])
dist[i] = max(dist[i] , getDis(idx , i));
}
edges.push_back(maxDis);
}
ll ans = 1;
for (int i = 0; i < edges.size(); ++i)
{
// if(edges[i] != 0)
ans = ((ll)ans * (edges[i] % MOD)) % MOD;
}
return ans;
}
int main()
{
// FILErw
int Cases;
scanf("%d" , &Cases);
while(Cases--)
{
scanf("%d %d" , &N , &D);
for (int i = 0; i < N; ++i)
{
for (int j = 0; j < D; ++j)
{
scanf("%d" , &pt[i][j]);
}
}
printf("%lld\n", prim());
}
}