#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;

int va[20][2],i,j,iesire,intrare,st[21],as,ev,k=2,timp,mint=10000,d=0,t=0;
float distanta,mind=10000;
char comune[65][25];
struct intr
   {
    int t;
    float d;
   }v[65][65];

 void init(int k,int st[21])
   {
    st[k]=0;
   }

void succesor(int k,int &as,int st[21])
   {
    if(st[k]<65&&k<20)
         {
             st[k]++;
             as=1;
         }
        else
         {
             as=0;
         }
   }

void valid(int k,int &ev,intr v[65][65],int st[21])
   {
    int i;
    ev=1;
    if (v[st[k]][st[k-1]].d==0)
                         ev=0;
                   else
                      for(i=1;i<k;i++)
                           if(st[i]==st[k])
                                    ev=0;

   }

int solutie(int k,int iesire,int st[21])
     {
         return(st[k]==iesire);
     }

void tipar(int k,int timp,float distanta,int &mint,float &mind,int va[20][2],int &t,int &d,int st[21])
     {
         if(mint>timp)
            {
              for(i=1;i<=k;i++)
                   va[i][0]=st[i];
              mint=timp;
              t=k;
            }
         if(mind>distanta)
            {
              for(i=1;i<=k;i++)
                   va[i][1]=st[i];
              mind=distanta;
              d=k;
            }
     }




int main()
{
cout<<"intrare=";cin>>intrare;
cout<<"iesire=";cin>>iesire;
ifstream f("database.c");
for(i=1;i<=65;i++)
    for(j=1;j<=65;j++)
      {
          f>>v[i][j].t;
      }
for(i=1;i<=65;i++)
    for(j=1;j<=65;j++)
      {
          f>>v[i][j].d;
      }
st[1]=intrare;
k=2;
init(k,st);
if (iesire==40||intrare==40)
        cout<<"Nu exista drum prin judetul Galati.";
     else
{
while(k>1)
      {
        do
          {
            succesor(k,as,st);
            if(as!=0)
                valid(k,ev,v,st);

          }while(as!=0&&ev==0);

          if(as!=0)
                 if(solutie(k,iesire,st))
                       {
                          timp=timp+v[st[k]][st[k-1]].t;
                          distanta=distanta+v[st[k]][st[k-1]].d;
                          tipar(k,timp,distanta,mint,mind,va,t,d,st);
                          timp=timp-v[st[k]][st[k-1]].t;
                          distanta=distanta-v[st[k]][st[k-1]].d;
                       }
                    else
                       {
                          timp=timp+v[st[k]][st[k-1]].t;
                          distanta=distanta+v[st[k]][st[k-1]].d;
                          k++;
                          init(k,st);
                       }
              else
                {
                 k--;
                 timp=timp-v[st[k]][st[k-1]].t;
                 distanta=distanta-v[st[k]][st[k-1]].d;
                }

      }
for(i=1;i<=t;i++)
    cout<<va[i][0]<<" ";
cout<<endl<<"timp min="<<(mint*85/100)<<endl;
for(i=1;i<=d;i++)
    cout<<va[i][1]<<" ";
cout<<endl<<"dist min="<<(mind*75/100)<<endl;
return 0;
}

}

Hello everyone! I am trying to calculate the minimum distance between a few cities and also the minimum time to get from point A to point B.

Intrare = point A Iesire = point B database.c is a file that contains 2 matrix with the links between cities and one with the travel time. DATABASE : https://www.scribd.com/doc/260509723/database-cpp

It will display the link cities you need to go through to get to point b and also the distance in kilometers("dist min") and the travel time ("timp min").

It stops working after build and run. it displays everything but a window pops up saying that the file has stopped working. Could you help me?

ERROR: http://i.imgur.com/JCipywS.png

Run it under a debugger.

I would add that the Travelling Salesman Problem (which this could be, depending on you interpret the traversal requirement) is known to be an NP-Hard problem, so even if you succeed in getting it to work, it may have quite a long run time depending on how many nodes the graph has to cover. Of course, you could just try proving P=NP and thus showing that a shorter run time may be possible, but that's rather a tall order to fill...

commented: As in a PhD thesis problem? :-) +13
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.