Cod sursa(job #1132445)

Utilizator alexsimi66FMI Simandi Alexandru alexsimi66 Data 3 martie 2014 12:27:59
Problema Sate Scor 100
Compilator cpp Status done
Runda Teme Pregatire ACM Unibuc 2013 Marime 1.82 kb
#include<fstream>
#include<vector>
#include<deque>

using namespace std;

struct drum{
	int destinatie;
	int distanta;
};



vector<vector<drum>>ListaAdiacenta;


int main(){
	int n, m, x, y, a, b, c;
	ifstream fin("sate.in");
	ofstream fout("sate.out");
	fin >> n >> m >> x >> y;
	bool *vizitat;
	int *vectortati;
	vectortati = new int[n + 2];
	vizitat = new bool[n + 2];
	for (int i = 0; i <= n; i++){
		vizitat[i] = false;
		vectortati[i] = 0;
	}
	vector<drum>temporar;
	for (int i = 0; i <= n; i++)
		ListaAdiacenta.push_back(temporar);
	for (int i = 0; i <= m; i++){
		fin >> a >> b >> c;
		ListaAdiacenta[a].push_back(drum());
		ListaAdiacenta[a].back().destinatie = b;
		ListaAdiacenta[a].back().distanta = c;
		ListaAdiacenta[b].push_back(drum());
		ListaAdiacenta[b].back().destinatie = a;
		ListaAdiacenta[b].back().distanta = c;
	}
	deque<int>lista;
	vizitat[x] = 1;
	vectortati[x] = 0;
	lista.push_back(x);
	bool ok = true;
	while (!lista.empty()&&ok){
		int i = -1;
		for (vector<drum>::iterator j = ListaAdiacenta[lista[0]].begin(); j != ListaAdiacenta[lista[0]].end()&&ok; j++){
			if (vizitat[(*j).destinatie] == 0){
				vizitat[(*j).destinatie] = 1;
				lista.push_back((*j).destinatie);
				vectortati[(*j).destinatie] = lista[0];
				if ((*j).destinatie == y)
					ok = false;
			}
		}
//		fout << lista[0] << "  ";
		lista.pop_front();
	}
	//for (int i = 1; i <= n; i++)
	//	fout << vectortati[i] << " ";
	int om=y;
	long long distanta = 0;
	while (om){
		for (vector<drum>::iterator j = ListaAdiacenta[vectortati[om]].begin(); j != ListaAdiacenta[vectortati[om]].end();j++)
		if ((*j).destinatie == om){
			if (om>vectortati[om])
				distanta += (*j).distanta;
			else distanta -= (*j).distanta;
			break;
		}
		om = vectortati[om];
	}
	fout << distanta;
	return 0;
}