Cod sursa(job #3219492)

Utilizator paull122Paul Ion paull122 Data 31 martie 2024 15:24:37
Problema Sate Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.81 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 main()
{
	fin >> n >> m >> x >> y;
  while(m--)
	{
		int from,to,d;
		fin >> from >> to >> d;
    graph[from].push_back({to,d});
    graph[to].push_back({from,-d});
	}
  queue<int> Q;
  Q.push(x);
  vis[x]=1;
  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;
					res += c;
          if(to==y)
					{
							fout << res;
							return 0;
					}
					Q.push(to);
			}
		}
	}

}