Mai intai trebuie sa te autentifici.

Cod sursa(job #1511214)

Utilizator tudormaximTudor Maxim tudormaxim Data 26 octombrie 2015 10:44:08
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.28 kb
#include <iostream>
#include <vector>
#include <queue>
#include <cstdio>
using namespace std;
const int nmax = 30005;
const int oo = 1<<29;
vector <pair<int,int> > g[nmax];
int dist[nmax], n;
bool viz[nmax];

void bfs(int start, int finish)
{
    queue <int> coada;
    int dad, son, cost, i;
    for(i=1; i<=n; i++)
        dist[i]=oo;
    coada.push(start);
    viz[start] = true;
    dist[start] = 0;
    while (!coada.empty())
    {
        dad=coada.front();
        coada.pop();
        for (i=0; i<g[dad].size(); i++)
        {
            son=g[dad][i].first;
            cost=g[dad][i].second;
            if (viz[son]==false && (dist[son]==oo || dist[son] > dist[dad]+cost))
            {
                dist[son]=dist[dad]+cost;
                viz[son]=true;
                coada.push(son);
            }
        }
    }
}

int main()
{
    freopen("sate.in", "r", stdin);
    freopen("sate.out", "w", stdout);
    int m, x, y, c, a, b, i;
    scanf("%d %d %d %d", &n, &m, &x, &y);
    for(i=1; i<=m; i++)
    {
        scanf("%d %d %d", &a, &b, &c);
        g[a].push_back(make_pair(b, c));
        g[b].push_back(make_pair(a, -c));
    }
    bfs(x, y);
    printf("%d", dist[y]);
    fclose(stdin);
    fclose(stdout);
    return 0;
}