Cod sursa(job #2129446)

Utilizator tudortarniceruTudor Tarniceru tudortarniceru Data 12 februarie 2018 20:38:58
Problema Sate Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.73 kb
#include <fstream>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;

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

const int MAXN = 30005;
int n, m;
int c1, c2;
vector< pair<int, int> > v[MAXN];
queue<int> q;
int s[MAXN];

void push(int a, int b, int c) {
    v[a].push_back(make_pair(b, c));
    v[b].push_back(make_pair(a, c));
}

int main() {

    fin >> n >> m;
    fin >> c1 >> c2;
    for (int i = 1; i <= m; ++i) {
        int a, b, c;
        fin >> a >> b >> c;
        push(a, b, c);

        for (int j = 0; j < v[a].size(); ++j) {
            pair<int, int> k = v[a][j];
            if (k.first > a) {
                if (k.first < b) {
                    push(k.first, b, c - k.second);
                }
                else if (k.first > b) {
                    push(k.first, b, k.second - c);
                }
            }
        }

        for (int j = 0; j < v[b].size(); ++j) {
            pair<int, int> k = v[b][j];
            if (k.first < b) {
                if (k.first > a) {
                    push(k.first, a, c - k.second);
                }
                else if (k.first < a) {
                    push(k.first, a, k.second - c);
                }
            }
        }

    }

    q.push(c1);
    while (!q.empty()) {
        int k = q.front();
        for (int i = 0; i < v[k].size(); ++i) {
            pair<int, int> p = v[k][i];
            if (s[p.first] == 0 || s[p.first] > s[k] + p.second) {
                s[p.first] = s[k] + p.second;
                q.push(p.first);
            }
        }
        q.pop();
    }

    fout << s[c2];


    fout.close();
    return 0;
}