1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
#include <iostream>
#include <fstream>
#include <string>
#include <queue>
#include <vector>
#include <iterator>
using namespace std;
struct Edge
{
int end; //this is the node that the edge points to.
int weight; //this is the weight of the node, very important!
int origin; //finally, this is the node which we are pointing to, just in case it's needed.
Edge(int start, int dest, int lbs): origin(start), end(dest), weight(lbs){ };
bool operator < (const Edge& a) const
{
return weight < a.weight;
}
};
const int INFINITY = 76543210;
int filelength(string filename)
{
ifstream file;
string str;
int lines=0;
file.open(filename);
while (getline(file, str))
{
lines++;
}
file.close();
return lines;
}
void dij(int x, const vector<vector<Edge>> &list, vector<int> &path, vector<int> &visited)
{
priority_queue<Edge> Q;
int a = list.size();
Edge origin = Edge(list[x][0].origin, list[x][0].end, list[x][0].weight);
//origin.origin = x;
path.clear(); //clean out any pre-existing junk
path.resize(a, INFINITY);
visited.resize(a, -1);
Q.push(origin);
path.clear();
path.assign(a, INFINITY);
//cout<<path[0];
//path.assign(a, 0);
while (!Q.empty())
{
Edge temp = Edge(Q.top().origin, Q.top().end, Q.top().weight);//we need to preserve the top value so we can use it later
Q.pop();//removing the top
//cout<<"eee";
int begin = temp.origin;
for(std::size_t i=0;i < list[begin].size()-1;++i )
{
//cout<<"fff";
//cout<<list[begin].size();
if(list[begin][i].weight != 0)
{
//cout<<path[begin];
//cout<<"ggg";
//cout<<i<<" "<<begin;
//cout<<path[i];
if(path[i] < (path[begin] + list[begin][i].weight))
{
// cout<<"hhh";
path[i] = path[begin] + list[begin][i].weight;
}
// cout<<"iii";
if(!visited[i])
{
Edge newEdge(list[begin][i].origin, list[begin][i].end, path[i]);
Q.push(newEdge);
visited[i] = true;
}
//cout<<"hhh";
}
}
}//closure of the while loop
cout << "The shortest distance from " << x << " to all the nodes is" << endl;
for(int i=0;i < list.size()-1;i++ )
{
cout << i << " : " << path[i] << endl;
}
cout << endl << endl;
}
int main()
{
//Edge original;
//original.weight = 0;
vector<vector<Edge>> list;
vector<int> path;
vector<int> visited;
std::vector<int>::size_type sz = path.size();
int e = 0; //This is the total size of the vector. It gets determined once
string filename;
printf("Enter the name of the file: ");
cin>>filename;
ifstream infile;
infile.open(filename);
int v = 0;
int x;
//cout<<filename;
if(!infile.is_open())
{
cout << "Error opening file";
//cin.get();
exit(EXIT_FAILURE);
}
infile>>x;
Edge original(x, x, 0);
original.origin = original.end;
e = filelength(filename);
list.resize(e);
int a,b/*,c*/;
//c=0;
while(infile>>v>>a>>b)
{
list[v].push_back(Edge(v,a,b));
}
//the first is the origin point, the second is the n-1th term. for example 00 is the first listed value of the node 0
//cout<<list[0][0].weight;
for (unsigned i=0; i<sz; i++)
{
path[i]=i;
}
infile.close();
dij(x, list, path, visited);
cin>>x;
//quit();
return 0;
}
|