Pagini recente » Cod sursa (job #2947016) | Cod sursa (job #1232928) | Cod sursa (job #2050345) | Cod sursa (job #183336) | Cod sursa (job #2257176)
#include <bits/stdc++.h>
#define pii pair <int, int>
#define INF 0x3f3f3f3f
using namespace std;
ifstream fin("sate.in");
ofstream fout("sate.out");
struct str{
int nod;
int cost;
bool operator <(const str&other)const
{
return cost>other.cost;
}
};
priority_queue <str> pq;
vector <pii> v[30005];
int n,m,xx,yy,drum[30005];
void dijkstra()
{
while(!pq.empty())
{
int nod=pq.top().nod;
int cost=pq.top().cost;
pq.pop();
if(cost!=drum[nod])
continue;
for(auto it=v[nod].begin();it<v[nod].end();it++)
{
int nxt=(*it).first;
int cst=(*it).second;
if(drum[nod]+cst<drum[nxt])
{
drum[nxt]=drum[nod]+cst;
pq.push({nxt,drum[nxt]});
}
}
}
}
int main()
{
fin>>n>>m>>xx>>yy;
memset(drum,INF,sizeof(drum));
for(int i=1;i<=m;i++)
{
int x,y,c;
fin>>x>>y>>c;
v[x].push_back({y,c});
v[y].push_back({x,-c});
}
pq.push({xx,0});
drum[xx]=0;
dijkstra();
fout<<drum[yy];
return 0;
}