Pagini recente » Cod sursa (job #365094) | Cod sursa (job #1534050) | Cod sursa (job #870545) | Cod sursa (job #1256891) | Cod sursa (job #2455439)
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
ifstream f("sate.in");
ofstream g("sate.out");
vector<pair<int,int> >graph[30005];
queue<int>q;
int n, x, from, to, a, b, c, m;
int distante[30005];
void citire()
{
f >> n >> m >> from >> to;
for (int i=1; i<=m; ++i)
{
f >> a >> b >> c;
graph[a].push_back({b,c});
graph[b].push_back({a,c});
}
for (int i=1; i<=n; ++i)
distante[i]=9999999;
}
int modul(int x, int y)
{
if (x>y)
return x-y;
return y-x;
}
void rezolvare()
{
distante[from]=0;
q.push(from);
while(!q.empty())
{
int sus=q.front();
q.pop();
for (auto &v:graph[sus])
{
int dest=v.first;
int cost=v.second;
if (sus==from)
{
if (distante[dest]>cost)
distante[dest]=cost, q.push(dest);
}
else if (dest > from && sus > from)
{
if (dest>sus)
{
if (distante[dest]>distante[sus]+cost)
{
distante[dest]=distante[sus]+cost;
q.push(dest);
}
}
else
{
if (distante[dest]>distante[sus]-cost)
{
distante[dest]=distante[sus]-cost;
q.push(dest);
}
}
}
else if (dest<from && sus<from)
{
if (dest<sus)
{
if (distante[dest]>distante[sus]+cost)
{
distante[dest]=distante[sus]+cost;
q.push(dest);
}
}
else
{
if (distante[dest]>distante[sus]-cost)
{
distante[dest]=distante[sus]-cost;
q.push(dest);
}
}
}
else if (dest<from && sus>from || sus<from && dest>from)
{
if (distante[dest]>cost-distante[sus])
{
distante[dest]=cost-distante[sus];
q.push(dest);
}
}
}
}
if (distante[to]!=9999999)
{
g<< distante[to];
}
}
int main()
{
citire();
rezolvare();
return 0;
}