Cod sursa(job #1994638)

Utilizator Mircea_DonciuDonciu Mircea Mircea_Donciu Data 25 iunie 2017 15:52:41
Problema Sate Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.05 kb
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
const int inf=20000005;
int n,m,c,d,i,x,y,z,v[30005];
struct vec
{
    vector <int> a,b;
};
vec w[30005];
queue <int> q;
int main()
{
    ifstream f("sate.in");
    ofstream g("sate.out");
    f>>n>>m>>c>>d;
    for(i=1; i<=n; i++)
    {
        w[i].a.push_back(0);
        w[i].b.push_back(0);
        v[i]=inf;
    }
    for(i=1; i<=n; i++)
    {
        f>>x>>y>>z;
        if(x>y) swap(x,y);
        w[x].a.push_back(y);
        w[x].a[0]++;
        w[y].a.push_back(x);
        w[y].a[0]++;
        w[x].b.push_back(z);
        w[y].b.push_back(-z);
    }
    v[c]=0;
    q.push(c);
    while(!q.empty())
    {
        x=q.front();
        q.pop();
        for(i=1; i<=w[x].a[0]; i++)
        {
            y=w[x].a[i];
            if(v[y]==inf)
            {
                v[y]=v[x]+w[x].b[i];
                q.push(y);
            }
        }
    }
    //if(v[d]<0) v[d]*=-1;
    g<<v[d]<<'\n';
    f.close(); g.close();
    return 0;
}