void path(town start, graph<town>& g, int p, LList<town> &visited){
	if (g.empty())return;
	int count = 0;
	queue<town> q;
	q.push(start);
	visited.toEnd(start);

	elem_link1<town> * f = g.point(start);
	f = f->link;
	while (!q.empty()){
		town t;
		q.pop(t);

		while (f){
			if (p == count)return;
			else if (!member(visited, f->inf)){

				q.push(f->inf);
				visited.toEnd(f->inf);
				count++;
			}

			f = f->link;
		}
	}
}
Exemplo n.º 2
0
void findCities(graph<int>& g, LList<int>& cities, int cityId, int p){
	elem_link1<int>* start = g.point(cityId);
	int dist[100];	// 100???
	bool visited[100];	// 100??? - nikoi ne garantira, che id-tata na cities shte sa posledovatelni chisla pod 100
	memset(dist, -1, 100);
	memset(visited, 0, 100);

	if (start){
		visited[start->inf] = true;
		dist[start->inf] = 0;
		queue<int> q;
		q.push(start->inf);
		while (!q.empty()){
			int current = q.front();
			q.pop();
			elem_link1<int>* p = g.point(current);
			p = p->link;
			while (p){
				if (!visited[p->inf]){
					visited[p->inf] = true;
					dist[p->inf] = dist[current] + 1;
					q.push(p->inf);
				}
				p = p->link;
			}

		}

		for (int i = 0; i < 100; i++){
			if (dist[i] >= 0 && dist[i] <= p){
				cities.toEnd(i);
			}
		}
	}
}
Exemplo n.º 3
0
void graph<T>::addTop(const T& a)
{ // създаване на линеен списък, съдържащ елемента a
	LList<T> l;
	l.toEnd(a);
	// включване на върха a към графа
	g.toEnd(l);
}
Exemplo n.º 4
0
LList<int> townLessThan(int start, graph<int> towns, LList<int> &visited, int p)
{
	LList<int> vert = towns.vertexes();
	int n = vert.length();

	//masiv, v koito shte pazim razstoianiata ot s da vs dr grad
	int dist[100];	// Защо 100?
	//inicializirame s -1 (ako niama put si ostava)
	for (int i = 0; i < n; i++)
	{
		dist[i] = -1;
	}

	//BFS
	queue<int> q;
	visited.toEnd(start);
	dist[start] = 0;
	q.push(start);
	while (!q.empty())
	{
		int x;
		q.pop(x);
		elem_link1<int> * p = towns.point(x);
		p = p->link;
		while (p)
		{
			if (!member(p->inf, visited))
			{
				visited.toEnd(p->inf);
				q.push(p->inf);
				dist[x] += 1;	// vsichki stoinosti shte badat 0!
			}
			p = p->link;
		}
	}

	//v masiva dist imame razstoianiata ot start do vs gradove
	LList<int> result;
	for (int i = 0; i < n; i++)
	{
		if (dist[i] <= p && dist[i] != -1)
			result.toEnd(i);
	}
	return result;

}
Exemplo n.º 5
0
void orderCities(LList<int>& ord, graph<int>& g, LList<City>& all, int cityId, int p){	// OK
	LList<int> cities;
	findCities(g, cities, cityId, p);
	LList<int> ordered;
	while (!cities.empty()){
		int max = getMaxCity(cities, all);
		deleteCity(cities, max);
		ordered.toEnd(max);
	}

	ord = ordered;
}
Exemplo n.º 6
0
LList<T> graph<T>::vertexes()
{
	LList<T> l;
	g.iterStart();
	elem_link1<LList<T> > *p = g.iter();
	while (p)
	{
		p->inf.iterStart();
		elem_link1<T>* q = p->inf.iter();
		l.toEnd(q->inf);
		p = p->link;
	}
	return l;
}
Exemplo n.º 7
0
void LList<T>::reverse()
{
	LList<T> r;
	iterStart();
	elem_link1<T>* p = iter();
	if (p)
	{
		r.toEnd(p->inf);
		p = p->link;
		while (p)
		{
			r.insertBefore(r.start, p->inf);
			p = p->link;
		}
	}
	*this = r;
}
//v
void firstk(LList<town> &visited, LList<town>& firstk, int k){
	visited.iterStart();
	elem_link1<town> * p = visited.iter();
	int count = 0;
	while (p){
		town max(0, 0);
		max.sites = p->inf.sites;
		while (count != k){
			elem_link1<town>*q = p->link;

			while (q){
				if (max.sites < q->inf.sites)
					max.sites = q->inf.sites;
				q = q->link;
			}
			count++;
			firstk.toEnd(p->inf);
			p = p->link;
		}
	}
}
Exemplo n.º 9
0
LList<int> zad(int k, LList<int> towns, LList<town> zabelejitelnosti)
{
	LList<int> result;
	towns.iterStart();
	elem_link1<int> * p = towns.iter();
	elem_link1<int> * mp; //pointer to max
	while (p)
	{
		int max = p->inf;
		mp = p;
		elem_link1<int> * q = p->link;
		while (q)
		{
			//namirame broi zabelejitelnosti na stoinostta na p
			int pzab = findCount(zabelejitelnosti, p->inf);
			//namirame broi zabelejitelnost na stoinostta na q
			int qzab = findCount(zabelejitelnosti, q->inf);
			if (pzab < qzab)
			{
				max = q->inf;
				mp = q;
			}
			q = q->link;
		}
		mp->inf = p->inf;
		p->inf = max;
		p = p->link;
	}

	//v towns imame sortirani gradovete po broi zabelejitelnosti
	towns.iterStart();
	elem_link1<int> * m = towns.iter();
	for (int i = 0; i < k; i++)
	{
		if (!m) break;
		result.toEnd(m->inf);
		m = m->link;
	}
	return result;
}