Pagini recente » Cod sursa (job #581956) | Cod sursa (job #2725971) | Cod sursa (job #473422) | Cod sursa (job #1991282) | Cod sursa (job #2786032)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
struct dublu
{
int x,dist;
};
vector<dublu>a[30005];
int n, m, s,X,Y;
int dp[30005];
ifstream in("sate.in");
ofstream out("sate.out");
void bfs(dublu x1)
{
queue <dublu> q;
dp[x1.x]=0;
q.push(x1);
while(!q.empty())
{
//cout<<111<<"\n";
dublu x;
x=q.front();
q.pop();
// cout<<x.x<<" ";
for(auto y : a[x.x])
{
// cout<<y.x<<" \n";
if( dp[y.x]>dp[x.x]+y.dist)
{
dp[y.x]=dp[x.x]+y.dist;
q.push(y);
}
}
/*
for(int i=1; i<=n; i++)
{
out<<dp[i]<<" ";
}
out<<"\n";
*/
}
}
int main()
{
dublu start;
in>>n>>m>>X>>Y;
for(int i=0; i<m; i++)
{
int x,y,d;
in>>x>>y>>d;
dublu aux;
aux.x=y;
aux.dist=d;
a[x].push_back(aux);
if(x==X)
{
aux.x=x;
start=aux;
}
aux.x=x;
aux.dist=-d;
a[y].push_back(aux);
}
for(int i=1; i<=n; i++)
{
dp[i]=10000000000;
}
bfs(start);
out<<dp[Y];
return 0;
}