URI Online Judge Solution 1152 Dark Roads - Solution in C, C++, Java, Python and C# - Online Judge Solution

Latest

It is a free Online judges problems solution list. Here you can find UVA online Judge Solution, URI Online Judge Solution, Code Marshal Online Judge Solution, Spoz Online Judge Problems Solution

Tuesday, September 19, 2017

URI Online Judge Solution 1152 Dark Roads - Solution in C, C++, Java, Python and C#

URI Online Judge Solution 1152 Dark Roads - Solution in C, C++, Java, Python and C#

URI Online Judge Solution  1152 Dark Roads  | Beginner
URI Problem Link - https://www.urionlinejudge.com.br/judge/en/problems/view/1152

Problem Name:  1152 Dark Roads solution
Problem Number : URI -  1152 Dark Roads solution
Online Judge : URI Online Judge Solution
Category: Graph
Solution Language : C,C plus plus, java, python, c#(c sharp)


Sample Input Output URI 1152 problem

Input SimpleOutput Simple
7 11
0 1 7
0 3 5
1 2 8
1 3 9
1 4 7
2 4 5
3 4 15
3 5 6
4 5 8
4 6 9
5 6 11
0 0
51

URI Solution  1152 Dark Roads  Code in C++ / URI 1152 in cpp:


#include <cstdio>
#include <cstring>
#include <cstdlib>

using namespace std;

#define sc2(a, b) scanf("%d%d", &a, &b)
#define sc3(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define MAX 200000

struct Grafo { int x, y, v; };

int c;
int fat[MAX];
Grafo grafo[MAX];

int cmp(const void *a, const void *b) {
    return (*(struct Grafo *)a).v - (*(struct Grafo *)b).v;
}

void makeset(int n) {
    for (int i = 0; i < n; i++)
        fat[i] = i;
}

int find(int n) {
    if(fat[n] != n)
        fat[n] = find(fat[n]);
    return fat[n];
}

int unionp(int x, int y, int p) {
    int i = find(x), j = find(y);

    if(i != j) {
        c -= grafo[p].v;
        if(i > j) fat[i] = j;
        else fat[j] = i;
        return 1;
    }

    return 0;
}

int main(int argc, char const *argv[]) {
    int m, n, i;

    while(sc2(m, n) && (m || n)) {
        c = 0;
        memset(&grafo, 0, sizeof(grafo));

        for (i = 0; i < n; i++) {
            sc3(grafo[i].x, grafo[i].y, grafo[i].v);
            c += grafo[i].v;
        }

        makeset(m);
        qsort(grafo, n, sizeof(grafo[0]), cmp);
        for (i = 0; i < n; ++i)
            unionp(grafo[i].x, grafo[i].y, i);
        printf("%d\n", c);
    }

    return 0;
}

URI Solution  1152 Dark Roads  Code / URI 1152 solution in Java:



URI Solution  1152 Dark Roads  Code / URI 1152 solution in  Python:

URI Solution  1152 Dark Roads  Code / URI 1152 solution in  C# (C Sharp):


Demonstration:

Just implement this in coding. Since having any problem just put a comment below. Thanks



Tags: URI Online Judge Solution, URI OJ Solution list, URI Problems Solution, URI solver, URI all problem solution list, URI  1152 Dark Roads  code in C, URI  1152 Dark Roads  code in C++, URI  1152 Dark Roads  solution in C, URI solution, URI  1152 Dark Roads  solution in C,URI 1002 solution in C++-CPP,URI  1152 Dark Roads  solution in C# (C sharp),URI  1152 Dark Roads  solution in Java,URI  1152 Dark Roads  solution in Python,

No comments:

Post a Comment