Cod sursa(job #1758847)

Utilizator fanache99Constantin-Buliga Stefan fanache99 Data 17 septembrie 2016 23:04:47
Problema Reconst Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.85 kb
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

ifstream cin("reconst.in");
ofstream cout("reconst.out");

const int MAXN = 2000;

vector<pair<int, int> > g[1 + MAXN];
int sum[1 + MAXN];
bool seen[1 + MAXN];

void DFS(int node) {
    seen[node] = true;
    for (auto &it : g[node])
        if (!seen[it.first]) {
            sum[it.first] = sum[node] + it.second;
            DFS(it.first);
        }
}

int main() {
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= m; i++) {
        int a, b, c;
        cin >> a >> b >> c;
        g[a - 1].push_back(make_pair(b, c));
        g[b].push_back(make_pair(a - 1, -c));
    }
    for (int i = 1; i <= n; i++)
        if (!seen[i])
            DFS(i);
    for (int i = 1; i <= n; i++)
        cout << sum[i] - sum[i - 1] << " ";
    return 0;
}