Cod sursa(job #3177305)

Utilizator mdayAyabakti Muhammed Melih mday Data 28 noiembrie 2023 21:36:30
Problema Sate Scor 15
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.08 kb
#include <fstream>
#include <vector>
#include <queue>
#include <bitset>
#include <utility>

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

const int N = 250;

int n, m, x, y;

int cost[N];
std::vector<std::vector<std::pair<int, int>>> graf;
std::queue<int> q;
std::bitset<N> viz;

void read() {
    int u, v, w;
    for(int i = 0; i < m; ++i) {
	fin >> u >> v >> w;
	u--, v--;
	graf[u].push_back(std::make_pair(v, w));
	graf[v].push_back(std::make_pair(u, w));
    }
}

void bfs(int nod) {
    viz[nod] = 1;
    q.push(nod);
    while(!q.empty()) {
	nod = q.front();
	q.pop();
	for(std::pair<int, int> next : graf[nod]) {
	    if(!viz[next.first]) {
		viz[next.first] = 1;
		q.push(next.first);
		if(next.first > nod)
		    cost[next.first] = cost[nod] + next.second;
		else
		    cost[next.first] = cost[nod] - next.second;
	    }
	}
    }
}

int main() {
    fin >> n >> m >> x >> y;
    x--, y--;

    graf.assign(n, std::vector<std::pair<int, int>>());
    
    read();
    bfs(x);

    fout << cost[y];
    
    return 0;
}