-
Notifications
You must be signed in to change notification settings - Fork 7
/
LightOj 1049.cpp
109 lines (102 loc) · 1.95 KB
/
LightOj 1049.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
#include<cstdio>
#include<vector>
#include<queue>
#include<utility>
#include<map>
#define FOR(n) for(i=1;i<=n;i++)
using namespace std;
class data
{
public:
int v,left,right;
};
vector<int>aj_list[105];
map<pair<int,int>,int>p;
int i,j,n,tcase,t;
int a,b,w,l_cost,r_cost;
void clr()
{
p.clear();
FOR(104)
{
aj_list[i].clear();
}
}
void input()
{
scanf("%d",&n);
FOR(n)
{
scanf("%d %d %d",&a,&b,&w);
aj_list[a].push_back(b);
aj_list[b].push_back(a);
p[make_pair(a,b)]=0;
p[make_pair(b,a)]=w;
}
}
void right_cost()
{
vector<int>take(n+5);
queue<int>q;
int now;
q.push(aj_list[1][0]);
take[1]=1;
r_cost+=p[make_pair(1,aj_list[1][0])];
while(!q.empty())
{
now=q.front();
take[now]=1;
q.pop();
for(i=0;i<aj_list[now].size();i++)
{
if(take[aj_list[now][i]]==0)
{
q.push(aj_list[now][i]);
r_cost+=p[make_pair(now,aj_list[now][i])];
}
}
}
r_cost+=p[make_pair(now,1)];
}
void left_cost()
{
vector<int>take(n+5);
queue<int>q;
int now;
q.push(aj_list[1][1]);
l_cost+=p[make_pair(1,aj_list[1][1])];
take[1]=1;
while(!q.empty())
{
now=q.front();
take[now]=1;
q.pop();
for(i=0;i<aj_list[now].size();i++)
{
if(take[aj_list[now][i]]==0)
{
q.push(aj_list[now][i]);
l_cost+=p[make_pair(now,aj_list[now][i])];
}
}
}
l_cost+=p[make_pair(now,1)];
}
int main()
{
while(scanf("%d",&t)==1)
{
tcase=1;
while(t--)
{
clr();
input();
l_cost=r_cost=0;
left_cost();
right_cost();
printf("Case %d: ",tcase++);
l_cost<=r_cost?printf("%d\n",l_cost):printf("%d\n",r_cost);
}
}
return 0;
}