URI Online Judge Solution 1148 Countries at War – Solution in C, C++, Java, Python and C#

URI Online Judge Solution URI Solution - Graph

URI Online Judge Solution 1148 Countries at War – Solution in C, C++, Java, Python and C#

URI Online Judge Solution 1148 Countries at War  | Beginner
URI Problem Link – https://www.urionlinejudge.com.br/judge/en/problems/view/1148

Problem Name: 1148 Countries at War Solution
Problem Number : URI – 1148 Countries at War Solution
Online Judge : URI Online Judge Solution
Category: Graph
Solution Language : C,C plus plus, java, python, c#(c sharp)

URI Solution 1148 Countries at War  Code in C/ URI 1148 in C++:

#include <cstdio>
#include <stdlib.h>
#include <limits.h>

using namespace std;

#define sc(a) scanf("%d", &a)
#define sc2(a, b) scanf("%d%d", &a, &b)
#define sc3(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define INF 1 << 25

int matrix[501][501];

int dijkstra(int src, int des, int n) {
int short_dis, nextv, i, j;
int dist[n];
bool corr[n];

for (i = 1; i <= n; i++) {
dist[i] = INF;
corr[i] = false;
}

dist[src] = 0;

for (i = 1; i <= n; i++) {
short_dis = INF;

if (corr[des]) break;

for (j = 1; j <= n; j++) {
if (dist[j] < short_dis && !corr[j]) {
short_dis = dist[j];
nextv = j;
}
}

if (short_dis == INF) break;

corr[nextv] = true;

for (j = 1; j <= n; j++)
if (short_dis + matrix[nextv][j] < dist[j])
dist[j] = short_dis + matrix[nextv][j];
}

return dist[des];
}

int main(int argc, char const *argv[]) {
int i, j, n, e, x, y, h, k, o, d, r;

while(sc2(n, e) && (n || e)) {
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
matrix[i][j] = INF;

while(e--) {
sc3(x, y, h);
matrix[x][y] = h;
if(matrix[y][x] != INF)
matrix[x][y] = matrix[y][x] = 0;
}

sc(k);

for (i = 0; i < k; i++) {
sc2(o, d);
r = dijkstra(o, d, n);
if(r == INF) printf("Nao e possivel entregar a cartan");
else printf("%dn", r);
}

printf("n");
}

return 0;
}

URI Solution 1148 Countries at War  Code / URI 1148 solution in CPP:

URI Solution 1148 Countries at War  Code / URI 1148 solution in Java:

URI Solution 1148 Countries at War  Code / URI 1148 solution in  Python:

URI Solution 1148 Countries at War  Code / URI 1148 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 area of a circle code in C, URI 1148 Countries at War  code in C++, URI 1148 Countries at War  solution in C, URI solution, URI 1002 solution in C,URI 1148 Countries at War  solution in C++-CPP,URI 1002 solution in C# (C sharp),URI 1148 Countries at War  solution in Java,URI 1148 Countries at War  solution in Python,

Leave a Reply

Your email address will not be published. Required fields are marked *