Cod sursa(job #626138)

Utilizator sanda-mariaPatras Sanda sanda-maria Data 26 octombrie 2011 14:41:51
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.1 kb
#include <fstream>
#include <vector>
using namespace std;

const int N=30005,M=100002,inf=1<<30;
struct muchie{int x,c;};
int v[M],dist[N],n;
bool use[N];
vector<muchie> a[N];

ifstream in("sate.in");
ofstream out("sate.out");

inline void push(int x)
{
    v[++v[0]]=x;
}

inline void pop(int& val)
{
    val=v[v[0]--];
}

void bfs(int x)
{
    int i,y,c;
    for (i=1;i<=n;i++)
        dist[i]=inf;
    dist[x]=0;
    push(x);
    while (v[0])
    {
        pop(x);
        if (use[x])
            continue;
        use[x]=true;
        for (vector<muchie>::iterator i=a[x].begin();i!=a[x].end();i++)
        {
            y=(*i).x;
            c=(*i).c;
            if (dist[y]==inf)
            {
                dist[y]=dist[x]+c;
                push(y);
            }
        }
    }
}

int main()
{
    int m,x,y,c,X,Y;
    in>>n>>m>>X>>Y;
    while (m--)
    {
        in>>x>>y>>c;
        if (x>y)
            c=-c;
        a[x].push_back((muchie){y,c});
        a[y].push_back((muchie){x,-c});
    }
    bfs(X);
    out<<dist[Y]<<"\n";
    return 0;
}