Pagini recente » Cod sursa (job #2310599) | Cod sursa (job #215308) | Cod sursa (job #377503) | Cod sursa (job #1011097) | Cod sursa (job #2981086)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
int n,m,a,b,x,y,pr,dist[100100];
struct edge
{
int dist;
int price;
};
vector <edge> neighbours[30005];
void bfs (int source)
{
queue <edge> myq;
myq.push({source,0});
dist[source]=0;
while (!myq.empty())
{
edge u=myq.front();
myq.pop();
for (auto it : neighbours[u.dist])
if (dist[it.dist]==-30000000)
{
dist[it.dist]=it.price+dist[u.dist];
myq.push(it);
}
}
}
int main()
{
ifstream f ("sate.in");
ofstream g ("sate.out");
f>>n>>m>>x>>y;
for (int i=1; i<=n; i++)
{
f>>a>>b>>pr;
neighbours[a].push_back({b,pr});
neighbours[b].push_back({a,-pr});
}
for (int i=1; i<=n ; i++)
dist[i]=-30000000;
bfs(x);
g<<dist[y];
}