Cod sursa(job #623915)

Utilizator otilia_sOtilia Stretcu otilia_s Data 20 octombrie 2011 22:54:01
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.03 kb
#include <fstream>
#include <vector>
#include <deque>
#define NMAX 30004
#define oo 20000005
using namespace std;
int d[NMAX];
vector < pair<int,int> >  A[NMAX];
int X,Y;

void citire()
{
	int m,n,i,j,ct;
	ifstream fin("sate.in");
	fin>>n>>m>>X>>Y;
	if (X>Y) { int aux=X; X=Y; Y=aux;}
	while (m--)
	{
		fin>>i>>j>>ct;
		if (i<j)
			{
				A[i].push_back( make_pair(j,ct));
				A[j].push_back( make_pair(i,-ct));
			}
		else
			{
				A[i].push_back( make_pair(j,-ct));
				A[j].push_back( make_pair(i,ct));
			}		
	}
	
	for (i=1;i<=n;++i)
		d[i]=oo;
	d[X]=0;
}

void afisare()
{
	ofstream fout("sate.out");
	fout<<d[Y]<<endl;
	fout.close();
}

int main()
{
	citire();

	int i,x,y;
	
	deque <int> Q;
	Q.push_back(X);
	
	while (!Q.empty())
	{
		x=Q.front(); Q.pop_front();
		for (i=0;i<A[x].size(); ++i)
		{
			y=A[x][i].first;
			if (d[y]==oo)
				{
					d[y]=d[x]+A[x][i].second;
					if (y==Y) { Q.clear(); break;}
					Q.push_back(y);					
				}
		}
	}
	
	afisare();
	return 0;
}