-
Notifications
You must be signed in to change notification settings - Fork 0
/
1029.cpp
134 lines (100 loc) · 2.36 KB
/
1029.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <bits/stdc++.h>
using namespace std;
int g[101][101];
int graph[101][101],n;
int minDistance(int dist[], bool sptSet[])
{
int min = INT_MAX, min_index;
for (int v = 0; v <= n; v++)
if (sptSet[v] == false && dist[v] <= min)
min = dist[v], min_index = v;
return min_index;
}
int maxDistance(int dist[], bool sptSet[])
{
int max = -1, max_index;
for (int v = 0; v <= n; v++)
if (sptSet[v] == false && dist[v] >= max)
max = dist[v], max_index = v;
return max_index;
}
int prims()
{
int dist[n+1];
bool sptSet[n+1];
for (int i = 0; i <=n; i++)
dist[i] = INT_MAX, sptSet[i] = false;
dist[0] = 0;
for (int count = 0; count < n; count++)
{
int u = minDistance(dist, sptSet);
sptSet[u] = true;
for (int v = 0; v <= n; v++)
{
if (!sptSet[v] && graph[u][v]!=0 && graph[u][v] < dist[v])
dist[v] = graph[u][v];
}
}
int sum=0;
for(int i=0;i<=n;i++){
if(dist[i]!=INT_MAX) sum+=dist[i];
}
return(sum);
}
int anti()
{
int dist[n+1];
bool sptSet[n+1];
for (int i = 0; i <=n; i++)
dist[i] = -1, sptSet[i] = false;
dist[0] = 0;
for (int count = 0; count < n; count++)
{
int u = maxDistance(dist, sptSet);
sptSet[u] = true;
for (int v = 0; v <= n; v++)
{
if (!sptSet[v] && g[u][v]!=0 && g[u][v] > dist[v])
dist[v] = g[u][v];
}
}
int sum=0;
for(int i=0;i<=n;i++){
if(dist[i]!=-1) sum+=dist[i];
}
return(sum);
}
int main() {
int i,j,u,v,w,t;
scanf("%d",&t);
for(int l=1;l<=t;l++){
scanf("%d",&n);
for( i=0;i<=n;i++)
for( j=0;j<=n;j++){
graph[i][j]=0;
g[i][j]=0;
}
while(1){scanf("%d %d %d",&u, &v,&w);
if(u==0 && v==0 && w==0) break;
if(graph[u][v]==0){
graph[u][v]=w;
graph[v][u]=w;
g[u][v]=w;
g[v][u]=w;
}
else if(w<graph[u][v]){
graph[u][v]=w;
graph[v][u]=w;
}
else if(w>g[u][v]){
g[u][v]=w;
g[v][u]=w;
}
}
int a=prims();
int b=anti();
if((a+b)%2==0) printf("Case %d: %d\n",l,(a+b)/2);
else printf("Case %d: %d/2\n",l,(a+b));
}
return 0;
}