Cod sursa(job #682557)

Utilizator lucian666Vasilut Lucian lucian666 Data 19 februarie 2012 09:52:11
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.92 kb
#include<fstream>
#include<cstdlib>
#include<vector>
#define NN 30001
#define pb push_back
using namespace std;
ofstream out("sate.out");
struct QQ{
	int vf,cost;
	QQ(int v,int c)
	{
		vf=v;
		cost=c;
	}
};

vector<QQ>G[NN];
int x,y,n,m,v[NN];
void citire();
void dfs(int ,int );
int main()
{
	citire();
	dfs(x,0);
	return 0;
}
void citire()
{
	ifstream in("sate.in");
	in>>n>>m>>x>>y;
	int i,j,c;
	for(;m;m--)
	{
		in>>i>>j>>c;
		G[i].pb(QQ(j,c));
		G[j].pb(QQ(i,c));
	}
}
void dfs(int start,int d)
{
	int i,j;
	if(start==y)
	{
		out<<d<<" ";
		exit(0);
	}
	v[start]=1;
	for(i=0;i<G[start].size();i++)
		if(v[G[start][i].vf]==0)
			if(G[start][i].vf<start)
			{
				d-=G[start][i].cost;
				dfs(G[start][i].vf,d);
				d+=G[start][i].cost;
			}
			else
				if(G[start][i].vf>start)
				{
					d+=G[start][i].cost;
					dfs(G[start][i].vf,d);
					d-=G[start][i].cost;
				}
}