-
Notifications
You must be signed in to change notification settings - Fork 34
/
1049-One-Way-Roads.cpp
110 lines (76 loc) · 1.27 KB
/
1049-One-Way-Roads.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
#include <iostream>
#include <stdio.h>
#include <utility>
#include <vector>
#include <string.h>
using namespace std;
int main()
{
int t;
int x;
int w;
int y;
int total;
int sofar;
bool visited[110];
int count;
int n;
scanf("%d", &t);
for(int cs = 1; cs <= t; cs++) {
cin >> n;
vector < vector < pair <int, int > > > a(n+3);
memset(visited, 0, sizeof(visited));
total = 0;
sofar = 0;
for (int i = 0; i < n; i++) {
scanf("%d", &x);
scanf("%d", &y);
scanf("%d", &w);
a[x].push_back(make_pair(y, w));
a[y].push_back(make_pair(x, -w));
total += w;
}
int i;
i = 1;
count = 0;
visited[1] = 1;
while (count < n -1) {
x = a[i][0].first;
if(visited[x] == 0) {
w = a[i][0].second;
visited[x] = 1;
if(w < 0) {
w = w * -1;
sofar += w;
}
i = x;
}
else {
x = a[i][1].first;
visited[x] = 1;
w = a[i][1].second;
if(w < 0) {
w = w * -1;
sofar += w;
}
i = x;
}
count++;
}
if(x = a[i][0].first == 1) {
w = a[i][0].second;
if(w < 0) {
w = w * -1;
sofar += w;
}
}
else {
w = a[i][1].second;
if(w < 0) {
w = w * -1;
sofar += w;
}
}
printf("Case %d: %d\n", cs, min(sofar, total - sofar));
}
}