-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1059 - Air Ports.cpp
116 lines (101 loc) · 2.45 KB
/
1059 - Air Ports.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
114
115
116
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define INF INT_MAX
#define EPS 0.00000001
#define PI 2*acos(0.0)
#define MOD 1000000007
#define ck(XX) cout<<XX<<endl
#define set(XX,POS) XX|(1<<POS)
#define reset(XX,POS) XX&(~(1<<POS))
#define check(XX,POS) (bool)(XX&(1<<POS))
#define toggle(XX,POS) (XX^(1<<POS))
#define Fin freopen("input.txt","r",stdin)
#define Fout freopen("output.txt","w",stdout)
#define valid(X,Y,R,C) X>=0 && X<R && Y>=0 && Y<C
#define MS(ARRAY,VALUE) memset(ARRAY,VALUE,sizeof(ARRAY))
#define RT printf("Run Time : %0.3lf seconds\n", clock()/(CLOCKS_PER_SEC*1.0))
struct Node{
int from;
int to;
int cost;
};
bool operator < (Node A, Node B) {return A.cost > B.cost;}
int node, edge;
int sow;
int Acost;
bool taken[10010];
vector <Node> adj[10010];
void PMST(int source)
{
sow = 0;
taken[source] = 1;
priority_queue <Node> Q;
Node temp;
for(int i=0; i<adj[source].size();i++)
{
temp.from = source;
temp.to = adj[source][i].to;
temp.cost = adj[source][i].cost;
Q.push(temp);
}
while(!Q.empty())
{
Node u = Q.top();
Q.pop();
if(taken[u.to] || u.cost >= Acost) continue;
taken[u.to] = 1;
sow += u.cost;
for(int i=0; i<adj[u.to].size();i++)
{
if(taken[adj[u.to][i].to]) continue;
temp.from = adj[u.to][i].from;
temp.to = adj[u.to][i].to;
temp.cost = adj[u.to][i].cost;
Q.push(temp);
}
}
return;
}
int main()
{
//Fin;
//Fout;
int tc, cn=0;
int x,y,z;
int Nairport;
int ans;
scanf("%d",&tc);
while(tc--)
{
Nairport = 0;
ans = 0;
MS(taken,0);
for(int i=0; i<10010; i++) {adj[i].clear();}
scanf("%d%d%d",&node, &edge, &Acost);
for(int i=0; i<edge; i++)
{
scanf("%d%d%d",&x,&y,&z);
Node temp;
temp.from = x;
temp.to = y;
temp.cost = z;
adj[x].push_back(temp);
temp.from = y;
temp.to = x;
temp.cost = z;
adj[y].push_back(temp);
}
for(int i=1; i<=node; i++)
{
if(taken[i] == 0){
PMST(i);
Nairport++;
ans += sow;
}
}
ans = ans + (Nairport*Acost);
printf("Case %d: %d %d\n",++cn,ans,Nairport);
}
return 0;
}