Cod sursa(job #996352)

Utilizator sleepaholicNeculaescu Theodor sleepaholic Data 11 septembrie 2013 17:50:05
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.4 kb
#include<stdio.h>
#include<vector>
#include<queue>
#define pb push_back
#define mp make_pair
#define maxn 30005
#define maxb 8192
using namespace std;

int n,m,x,y;
int v[maxn],d[maxn];
vector < pair<int,int> > l[maxn];
char buff[maxb];
int pos=0;

int get_int()
{
    int nr=0;
    while(buff[pos]<'0' || buff[pos]>'9')
     if(++pos==maxb) fread(buff,1,maxb,stdin),pos=0;

    while(buff[pos]>='0' && buff[pos]<='9')
    {
        nr=nr*10+buff[pos]-'0';
        if(++pos==maxb) fread(buff,1,maxb,stdin),pos=0;
    }
    return nr;
}

void read()
{
    int a,b,c;
    n=get_int(); m=get_int();
    x=get_int(); y=get_int();
    for(int i=1;i<=m;i++)
    {
        a=get_int(); b=get_int();
        c=get_int();
        l[a].pb(mp(b,c));
        l[b].pb(mp(a,-c));
    }
}

void bfs()
{
    queue <int> q;
    int k;

    for(q.push(x),d[x]=0,v[x]=1;!q.empty();q.pop())
    {
        k=q.front();
        for(unsigned int i=0;i<l[k].size();i++)
         if(!v[l[k][i].first])
         {
             d[l[k][i].first]=d[k]+l[k][i].second;
             v[l[k][i].first]=1;
             q.push(l[k][i].first);
             if(l[k][i].first==y) return;
         }
    }
}

int main()
{
    freopen("sate.in","r",stdin);
    freopen("sate.out","w",stdout);

    read();
    bfs();
    printf("%d",d[y]);

    fclose(stdin);
    fclose(stdout);
    return 0;
}