Cod sursa(job #1847606)

Utilizator NicusorTelescu Nicolae Nicusor Data 14 ianuarie 2017 19:47:57
Problema Sate Scor 100
Compilator cpp Status done
Runda Teme Pregatire ACM Unibuc 2013 Marime 1.39 kb
#include <cstdio>
#include <vector>
#include <string.h>
#include <queue>

using namespace std;

char s[60];

struct pereche
{
    int nod,dist;
};

int pass[30001];
vector <pereche> muchii[30001];
queue <int> coada;

int main()
{
    freopen("sate.in","r",stdin);
    freopen("sate.out","w",stdout);
    int n,m,x,y;
    scanf("%d %d %d %d\n",&n,&m,&x,&y);
    for (int i=1;i<=m;++i)
    {
        int a=0,b=0,c=0;
        gets(s);
        char abc=0;
        for (int j=0;s[j]!=0;++j)
        {
            if (s[j]==' ') abc++;
            else
            {
                if (abc==0) a=a*10+s[j]-'0';
                if (abc==1) b=b*10+s[j]-'0';
                if (abc==2) c=c*10+s[j]-'0';
            }
        }
        if (a>b)
        {
            int aux=a;
            a=b;
            b=aux;
        }
        muchii[a].push_back({b,c});
        muchii[b].push_back({a,-c});
    }
    memset(pass,-1,sizeof(pass));
    pass[x]=0;
    coada.push(x);
    while (pass[y]==-1)
    {
        int abc=coada.front();
        int l=muchii[abc].size();
        for (int i=0;i<l;++i)
        {
            if (pass[muchii[abc][i].nod]==-1)
            {
                pass[muchii[abc][i].nod]=pass[abc]+muchii[abc][i].dist;
                coada.push(muchii[abc][i].nod);
            }
        }
        coada.pop();
    }
    printf("%d ",pass[y]);
}