Pagini recente » Cod sursa (job #41039) | Cod sursa (job #474596) | Cod sursa (job #2890001) | Cod sursa (job #1610977) | Cod sursa (job #891551)
Cod sursa(job #891551)
// Include
#include <fstream>
#include <utility>
#include <vector>
#include <queue>
using namespace std;
// Definitii
#define mp make_pair
#define pb push_back
#define edge pair<int, int>
#define cost first
#define node second
// Constante
const int sz = (int)3e4+1;
// Variabile
ifstream in("sate.in");
ofstream out("sate.out");
int nodes, edges;
int startNode, endNode;
int dist[sz];
vector<edge> graph[sz];
// Main
int main()
{
in >> nodes >> edges;
in >> startNode >> endNode;
int rFrom, rTo, rCost;
while(edges--)
{
in >> rFrom >> rTo >> rCost;
graph[rFrom].pb(mp(rCost, rTo));
graph[rTo].pb(mp(-rCost, rFrom));
}
queue<int> q;
q.push(startNode);
while(!q.empty())
{
int current = q.front();
q.pop();
vector<edge>::iterator it, end=graph[current].end();
for(it=graph[current].begin() ; it!=end ; ++it)
{
if(!dist[it->node])
{
dist[it->node] = dist[current] + it->cost;
q.push(it->node);
}
}
if(dist[endNode])
break;
}
out << dist[endNode];
in.close();
out.close();
return 0;
}