Pagini recente » Cod sursa (job #527860) | Cod sursa (job #923870) | Cod sursa (job #3200784) | Cod sursa (job #2918603) | Cod sursa (job #813299)
Cod sursa(job #813299)
#include<iostream>
#include<fstream>
#include<vector>
#define f first
#define s second
using namespace std;
int n,m,i,j,bot,top,viz[30002],coada[30002],cost[30002],x,q,a,b,c,y;
vector< pair <int,int> > A[30002];
void bfs()
{
viz[x]=1;
bot=0;
top=1;
coada[0]=x;
while(bot<n)
{
q=coada[bot];
for(i=0;i<A[q].size();++i)
if (!viz[A[q][i].f])
{
viz[A[q][i].f] = 1;
coada[top] = A[q][i].f;
cost[A[q][i].f] = cost[q] + A[q][i].s;
top++;
}
bot++;
}
}
int main()
{
ifstream f("sate.in");
ofstream g("sate.out");
f>>n>>m>>x>>y;
for(i=0;i<m;++i)
{
f>>a>>b>>c;
A[a].push_back(make_pair(b,c));
A[b].push_back(make_pair(a,-c));
}
bfs();
g<<cost[y];
}