Problem Summary:

Problem Number: 1148
Problem Name: Countries at War
Author’s Name: by Alessandro Luna Brazil
Timelimit: 2
Problem Category: AD-HOC
Problem Source:

Some Talks about Contest Programming:

An incredible method to enhance your abilities when figuring out how to code is by solving coding problems. Solving different kinds of challenges and riddles can enable you to improve as a problem solver, take in the complexities of a programming dialect, get ready for prospective job interviews, learn new algorithms and more.

An online judge is an online platform to test programs in focused programming challenges. They are likewise used to practice for such challenges. A considerable amount of these platforms also arrange their own programming contests.

10 Steps to Solve Any Problems:

  1. Read the problem completely at least two or three times (or however many makes you feel comfortable)
  2. Identify the subject, the problem belongs to. Is it a sorting or pattern matching problem? Can I use graph theory? Is it related to number theory? etc.
  3. Try to solve the problem manually by considering 3 or 4 sample data sets.
  4. After concentrate on optimizing the manual steps. Try to make it as simple as possible.
  5. Write to write pseudo-code and comments besides the code from the manual steps. One thing you can do is to check after every function is written. Use a good IDE with a debugger, if possible. Don’t need to think much about the syntax. Just focus on the logic and steps.
  6. Replace the comments or pseudo-code with real code. Always check if the values and code are behaving as expected before moving to the new line of pseudo-code.
  7. Then optimize the real code.
  8. Take care of boundary conditions as well.
  9. Get feedback from your teammates, professors, and other developers and also ask your question on Stack Overflow if possible. Try to learn from others’ guidelines and what they are handling those problems. A problem may be solved in several ways. So, don’t get disappointed if you can’t think like an expert. You need to stick to the problem and you will gradually become better and quicker in solving problems like others.
  10. Practice, Practice, and Practice.

N.B: Try to follow the above steps always. If you still can’t get the problem solved, take a look at the solution below. Don’t just copy paste the code. It will kill your creativity. Try to enjoy contest programming and develop your skills.


#include <string.h>
#include <stdio.h>
#include <iostream>
#define Vertices int

struct Grafo {
       int v;
       int a;
    int rota[501][501];

Grafo grafo;

void dijkstra (int origem, int destino)
    int w, w0;
    int fr[501], parnt[501];
    int cst[501];
    memset (parnt, -1, sizeof(parnt));
    memset (cst, 999999, sizeof(cst));
    fr[origem] = origem;
    cst[origem] = 0;
             int mincst = 999999;
             for(w = 0; w < grafo.v; w++){
                   if(parnt[w] == -1 && mincst > cst[w]){
                               mincst = cst[w0=w];
             if(mincst == 999999) break;
             parnt[w0] = fr[w0];     
             for(w = 0; w < grafo.v; w++){
                   if(cst[w] > cst[w0] + grafo.rota[w0][w]){
                               cst[w] = cst[w0] + grafo.rota[w0][w];
                               fr[w]= w0;
             if(cst[destino] <  999999) printf("%dn", cst[destino]);
             else printf("Nao e possivel entregar a cartan");

int main()
	memset(&grafo, 0, sizeof(grafo));
	int n,e,k,o,d;
	int x,y,z;
    int i, j;
             scanf("%d %d", &n, &e);
             if(n == 0 && e == 0) break;
             for(i = 0; i <= n; i++)
                   for(j = 0; j <= n; j++)
                         grafo.rota[i][j] = 999999;
             for (i = 0; i < e; ++i){
                     scanf("%d %d %d",&x, &y, &z);
                     if(grafo.rota[y][x] != 999999){
                                    grafo.rota[x][y] = 0;
                                    grafo.rota[y][x] = 0;
                           grafo.rota[x][y] = z;
             grafo.v = n+1;
             grafo.a = e;
             scanf("%d", &k);
             for (i = 0; i < k; ++i){
                     scanf("%d %d",&o, &d);
                               dijkstra(o, d);
	return 0;

N.B.: Code is Collected from Different Sources

life2coding_icon [] URI ONLINE JUDGE SOLUTION : 1148 - Countries at War (INTERMEDIATE PROBLEM)

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.