Cod sursa(job #1253154)

Utilizator nicolaegutaNicolae Guta nicolaeguta Data 31 octombrie 2014 20:43:09
Problema Sate Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.31 kb
#include <cstdio>
#include <queue>
#define Nmax 30005
#define INF 2000000000

using namespace std;

int N,X,Y,dp[Nmax];

struct Edge
{
    int nod,cost;
};
vector <Edge> L[Nmax];

struct el
{
    int nod,cost;
    bool operator <(const el &A) const
    {
        return cost>A.cost;
    }
};
priority_queue <el> Q;

inline void Dijkstra()
{
    el w,w1;
    vector <Edge> :: iterator it;
    dp[X]=0;
    w.nod=X; w.cost=0;
    Q.push(w);
    while(!Q.empty())
    {
        w=Q.top(); Q.pop();
        if(w.nod==Y) continue;
        for(it=L[w.nod].begin();it!=L[w.nod].end();++it)
        {
            w1.nod=it->nod; w1.cost=w.cost+it->cost;
            if(dp[w1.nod]>w1.cost)
            {
                dp[w1.nod]=w1.cost;
                Q.push(w1);
            }
        }
    }
}

int main()
{
    int x,y,z,M,aux,i;
    Edge w;
    freopen ("sate.in","r",stdin);
    freopen ("sate.out","w",stdout);
    scanf("%d%d%d%d", &N,&M,&X,&Y);
    while(M--)
    {
        scanf("%d%d%d", &x,&y,&z);
        w.nod=y; w.cost=z;
        L[x].push_back(w);
        w.nod=x; w.cost=-z;
        L[y].push_back(w);
    }
    for(i=1;i<=N;++i)
        dp[i]=INF;
    if(X>Y)
    {
        aux=X; X=Y; Y=aux;
    }
    Dijkstra();
    printf("%d\n", dp[Y]);
    return 0;
}