Cod sursa(job #996350)

Utilizator sleepaholicNeculaescu Theodor sleepaholic Data 11 septembrie 2013 17:49:21
Problema Sate Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.96 kb
#include <iostream>
#include <vector>
#include <fstream>

using namespace std;
vector <int> v[30001];
int c[30001], cost[30001], deg[30001];
int n,m,x,y;

void bfs()
{
    int i,j=1,k;
    c[0]=x;
    for(i=0; i<j; i++)
       for(k=0; k<deg[c[i]]; k+=2)
       {
            if(cost[v[c[i]][k]]==0)
            {
                c[j]=v[c[i]][k];
                cost[c[j]]=cost[c[i]]+v[c[i]][k+1];
                j++;
            }
            if(cost[y]!=0)
                break;
       }

}

int main()
{
    int i,a,b,dist;
    ifstream in("sate.in");
    ofstream out("sate.out");
    in>>n>>m>>x>>y;
    for(i=0; i<m; i++)
    {
        in>>a>>b>>dist;
        v[a].push_back(b);
        v[a].push_back(dist);
        v[b].push_back(a);
        v[b].push_back(dist*(-1));
    }
    in.close();
    for(i=1; i<=n; i++)
    {
        deg[i]=v[i].size();
    }
    bfs();
    out<<cost[y];
    out.close();
    return 0;
}