When I run the code with the first input file the code works fine, but when i use the second input file Dev-C++ 4.9.9.2 compiler, shows me the warning: "An Access Violation (Segmentation Fault) raised in your program" pointing to the line:
__test = __grouping_tmp[__i] == __grouping[__min];
of the file locale_facets.tcc of the compiler. I've searched to find possible solutions but nothing came up, can anybody assist me?
Here's My Code:
#include <algorithm>
#include <stdio.h>
#include <vector>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
vector< vector<int> > adj;
vector <string> towns;
vector <int> w;//weights
vector <int> v;//visited
string town1,town2;
int n,i,j,k,l;
void dfs(int start){
int c,size;
v[start]=1;
size=adj[start].size();
for (c=0;c<size;c++)
//if ((adj[start][c]==1) && (v[c]==0))
//if (v[c]==0)
dfs(adj[start][c]);
}
int findi(string name){ //Finds the I of the town
for(i=0;i<towns.size();i++){
if(name==towns[i]){
return i;
}
}
return 101;//Dead END
}
int main(){
ifstream fin ("B.in");
ofstream fout ("B.out");
towns.push_back("Start");
w.push_back(0);
bool found;
while(!fin.eof()){
fin>>town1>>k>>town2>>l;
found=false;
for(i=0;i<towns.size();i++){//search an ksanabike i poli
if(towns[i]==town1){
found=true;
}
}
if(!found){
towns.push_back(town1);
w.push_back(k);
}
}
towns.push_back("Finish");
w.push_back(10001);
for(i=1;i<towns.size()-1;i++){//sorting by weight
for(j=i+1;j<towns.size();j++){
if(w[i]>w[j]){
k=w[i];
w[i]=w[j];
w[j]=k;
town1=towns[i];
towns[i]=towns[j];
towns[j]=town1;
}
}
}
int c;
string prev;
c=0;
prev=towns[0];
for(i=0;i<towns.size();i++){//sort towns with same weight
if(towns[i]==prev){
c++;
}
if(towns[i]!=prev&&c!=0){
sort(towns.begin()+i-c-1,towns.begin()+i-1);
}
//Arxikopiisi visited je grafou
v.push_back(0);
adj.push_back(vector<int> ());
}
fin.clear();//reset file read!!
fin.seekg(0, std::ios::beg);
while(!fin.eof()){//Dimiourgia grafou
fin>>town1>>k>>town2>>l;
//cout<<town1<<" "<<town2<<endl;
adj[findi(town1)].push_back(findi(town2));
}
dfs(0);
//cout<<"v size:"<<v.size();
for(i=1;i<towns.size()-1;i++){
if(v[i]==1){
fout<<towns[i]<<endl;
}
}
fin.close();
fout.close();
return 0;
}
Input file that works-->
Start 0 Larnaca 40
Paphos 70 Finish 10001
Nicosia 90 Finish 10001
Larnaca 40 Paphos 70
Limasol 50 Paphos 70
Limasol 50 Larnaca 40
Nicosia 90 Limasol 50
Input File that gives the error -->
Paphos 40 Finish 10001
Nicosia 90 Finish 10001
Start 0 Larnaca 40
Larnaca 40 Paphos 40
Limasol 40 Larnaca 40
Larnaca 40 Limasol 40
Nicosia 90 Limasol 40