Algorithm
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <cstdio>
#include <map>
int main(){
long n; scanf("%ld", &n);
for(long p = 0; p < n; p++){long x; scanf("%ld", &x);} //Irrelevant
long m; scanf("%ld", &m);
std::map<long, long> cost;
while(m--){
long x, y, c; scanf("%ld %ld %ld", &x, &y, &c);
if(cost.count(y) > 0){cost[y] = (cost[y] < c) ? cost[y] : c;}
else{cost[y] = c;}
}
if(cost.size() < n - 1){puts("-1"); return 0;}
long long total(0);
for(std::map<long, long>::iterator it = cost.begin(); it != cost.end(); it++){total += it->second;}
printf("%lld\n", total);
return 0;
}
Copy The Code &
Try With Live Editor
Input
4
7 2 3 1
4
1 2 5
2 4 1
3 4 1
1 3 5
7 2 3 1
4
1 2 5
2 4 1
3 4 1
1 3 5
Output
11
Demonstration
Codeforces Solution-B. Hierarchy-Solution in C, C++, Java, Python