Algorithm
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <cstdio>
#include <set>
int main(){
int n; scanf("%d\n", &n);
std::set<int> first, second;
long totalCost(0), current(0);
while(n--){
int a, b, c; scanf("%d %d %d\n", &a, &b, &c);
totalCost += c;
if(first.count(a) || second.count(b)){current += c; first.insert(b); second.insert(a);}
else{first.insert(a); second.insert(b);}
}
if(totalCost - current < current){current = totalCost - current;}
printf("%ld\n", current);
return 0;
}
Copy The Code &
Try With Live Editor
Input
3
1 3 1
1 2 1
3 2 1
1 3 1
1 2 1
3 2 1
Output
1
Demonstration
Codeforces Solution-A. Ring road-Solution in C, C++, Java, Python