Cod sursa(job #1731809)

Utilizator cubaLuceafarul cuba Data 19 iulie 2016 23:34:59
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.07 kb
#include <bits/stdc++.h>
using namespace std;
ifstream f("sate.in");
ofstream g("sate.out");
const int NMAX = 30005;
const int INF = (1<<30);
vector < pair <int,int> > Graf[NMAX];
queue < pair <int,int> > Q;
int start, stop, dp[NMAX];
inline void Dijkstra (int start) {

    Q.push(make_pair(start , 0));
    while(!Q.empty()) {
        pair <int, int> info = Q.front();
        Q.pop();
        int nod = info.first;
        for(auto i : Graf[nod]) {
            int next = i.first;
            int cost = i.second;
            if(dp[next] > dp[nod] + cost) {
                dp[next] = dp[nod] + cost;
                Q.push(make_pair(next,dp[nod] + cost));
            }
        }
    }
}
int main()
{
    int n, m, x, y, c;
    f>> n >> m >> start >> stop;
    for(int i = 1; i <= m; i++) {
        f>> x >> y >> c;
        Graf[x].push_back(make_pair(y,c));
        Graf[y].push_back(make_pair(x,-c));
    }
    for(int i = 1; i <= n; i++) {
        dp[i] = INF;
    }
    dp[start] = 0;
    Dijkstra(start);
    g<<  dp[stop] <<"\n";
    return 0;
}