Cod sursa(job #1504615)

Utilizator VladuZ1338Vlad Vlad VladuZ1338 Data 17 octombrie 2015 23:02:09
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.44 kb
#include <iostream>
#include <fstream>
#include <queue>
  
using namespace std;
  
ifstream fin ("sate.in");
ofstream fout ("sate.out");
  
int n, m, si, sf, i, x, y, c, ct, viz[30001], xi, xc, d[30001], j;
queue <int> Q;
char parser[25];
  
struct nod
{
    int info;
    int dist;
    nod *urm;
};
nod *a[30001];
  
void add (nod *&prim, int x, int c)
{
    nod *p = new nod;
    p->info=x;
    p->dist=c;
    p->urm=prim;
    prim=p;
}
  
void BFS (int xi)
{
    nod *p;
    Q.push(xi); viz[xi]=1; d[xi]=0;
    while (!Q.empty())
    {
        xc=Q.front(); Q.pop();
        for (p=a[xc]; p!=0; p=p->urm)
        {
            if (viz[p->info]==0)
            {
                viz[p->info]=1;
                d[p->info]=d[xc]+p->dist;
                Q.push(p->info);
                if (p->info==sf)
                {
                    fout << d[sf];
                    break;
                }
            }
        }
    }
}
  
int main()
{
    fin >> n >> m >> si >> sf;
    fin.get();
    for (i=1; i<=m; i++)
    {
        x=y=c=j=0;
        fin.getline (parser, 25);
        while (parser[j]!=' ')
        {
        	x=x*10+(parser[j]-'0');
        	j++;
        }
        j++;
        while (parser[j]!=' ')
        {
        	y=y*10+(parser[j]-'0');
        	j++;
        }
        j++;
        while (parser[j]!=0)
        {
        	c=c*10+(parser[j]-'0');
        	j++;
        }
        add (a[x], y, c);
        add (a[y], x, -c);
    }
    BFS(si);
}