Cod sursa(job #3219496)

Utilizator paull122Paul Ion paull122 Data 31 martie 2024 15:28:22
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.94 kb
#include <iostream>
#include <fstream>
#include <queue>
#define INF 1000000
#define NMAX 30000
#define MMAX 100024

using namespace std;
ifstream fin("sate.in");
ofstream fout("sate.out");

vector<pair<int,int>> graph[NMAX+1];
int n,m,x,y;
bool vis[NMAX+1];
int dist[NMAX+1];

int main()
{
	fin >> n >> m >> x >> y;
	if(x > y)
	{
		swap(x,y);
	}
  while(m--)
	{
		int from,to,d;
		fin >> from >> to >> d;
		if(from > to)
		{
			swap(from,to);
		}
    graph[from].push_back({to,d});
    graph[to].push_back({from,-d});
	}
  queue<int> Q;
  Q.push(x);
  vis[x]=1;
  dist[x]=0;

  int res=0;
  while(!Q.empty())
	{
		int from = Q.front();
    Q.pop();
    for(auto i : graph[from])
		{
      int to = i.first, c = i.second;
      if(!vis[to])
			{
					vis[to]=1;
					dist[to] = dist[from] + c;
          if(to==y)
					{
							fout << dist[y];
							return 0;
					}
					Q.push(to);
			}
		}
	}

}