Codeforces Beta Round #24 24A - Ring road Solution



Problem Link:  https://codeforces.com/problemset/problem/24/A

 Solution in C++:

///La ilaha illellahu muhammadur rasulullah
///******Bismillahir-Rahmanir-Rahim******///
///Abul Hasnat  Tonmoy
///Department of CSE,23rd batch
///Islamic University,Bangladesh
///**********ALLAH IS ALMIGHTY************///
#include <bits/stdc++.h>
using namespace std;
const int N = 109;
int cost[N][N];
int cost_v;
vector<int> adj[N];
int ans = 1e9;
void dfs(int node, int parent) {
  if (node == 1 && parent != -1) {
    ans = min(ans, cost_v);
    cost_v = 0;
    return;
  }
  for (int i = 0; i < adj[node].size(); i++) {
    int child = adj[node][i];
    if (child != parent) {
      cost_v += cost[node][child];
      dfs(child, node);
    }
  }
}
int main() {
  int n;
  cin >> n;
  while (n--) {
    int u, v, c;
    cin >> u >> v >> c;
    adj[u].push_back(v);
    adj[v].push_back(u);
    cost[u][v] = 0;
    cost[v][u] = c;
  }
  dfs(1, -1);
  cout << ans << endl;
}


No comments

Most View Post

Recent post

Codeforces Round 971 (Div. 4) 2009C. The Legend of Freya the Frog Solution

  Problem Link    https://codeforces.com/contest/2009/problem/C S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. h &g...

Powered by Blogger.