-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy path11228-Transportation system.cpp
50 lines (47 loc) · 1.41 KB
/
11228-Transportation system.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
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,r,a,b,t,tc=1;
scanf("%d",&t);
while(t--){
scanf("%d %d",&n,&r);
vector<pair<int,int>> cities;
for(int i=0;i<n;i++){
scanf("%d %d",&a,&b);
cities.push_back({a,b});
}
int state = 1;
double road=0, rail=0;
// prim algorithm
vector<bool> taken(n);
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;
pq.push({0,0});
while(!pq.empty()){
int cur,dist;
tie(dist,cur) = pq.top(); pq.pop();
if(taken[cur]) continue;
if(sqrt(dist) > r){
rail += sqrt(dist);
state++;
} else {
road += sqrt(dist);
}
taken[cur] = true;
bool allTaken = true;
for(int i=0;i<n;i++){
if(!taken[i]){
allTaken = false;
// compute euclidean dist
int xDiff = cities[i].first - cities[cur].first;
int yDiff = cities[i].second - cities[cur].second;
xDiff*=xDiff;
yDiff*=yDiff;
pq.push({xDiff+yDiff,i});
}
}
if(allTaken) break;
}
printf("Case #%d: %d %.0f %.0f\n",tc++,state,road,rail);
}
}