-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1041 - Road Construction.cpp
133 lines (114 loc) · 2.77 KB
/
1041 - Road Construction.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
#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;
int sow; //Sum Of Weight of MST
bool taken[110];
vector <Node> adj[110];
map <string,int> s;
int hudai[110][110];
int tnode;
void PMST(int source)
{
tnode=0;
sow = 0;
MS(taken,0);
taken[source] = 1;
tnode++;
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]) continue;
temp.from = u.from;
temp.to = u.to;
temp.cost = u.cost;
temp.from = u.to;
temp.to = u.from;
temp.cost = temp.cost;
taken[u.to] = 1;
sow += u.cost;
tnode++;
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, m;
string x,y;
int z;
scanf("%d",&tc);
while(tc--)
{
s.clear();
node = 0;
for(int i=0; i<110; i++) {adj[i].clear();}
for(int i=0; i<110; i++)
{
for(int j=0; j<110; j++)
{
hudai[i][j] = INF;
}
}
scanf("%d",&m);
while(m--)
{
cin >>x>>y>>z;
if(s[x] == 0) s[x] = ++node;
if(s[y] == 0) s[y] = ++node;
if(hudai[s[x]][s[y]] < z) continue;
Node temp;
temp.from = s[x];
temp.to = s[y];
temp.cost = z;
adj[s[x]].push_back(temp);
temp.from = s[y];
temp.to = s[x];
temp.cost = z;
adj[s[y]].push_back(temp);
}
PMST(1);
if(tnode == node) printf("Case %d: %d\n", ++cn, sow);
else printf("Case %d: Impossible\n", ++cn);
}
return 0;
}