Cod sursa(job #674769)

Utilizator ioalexno1Alexandru Bunget ioalexno1 Data 6 februarie 2012 18:49:03
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.19 kb
#include <cstdio>
using namespace std;
const int nd=30005;
struct nod{int val; int cost; nod *urm; }*p[nd];
int n,m,x,y,sol[nd],c[nd];
void read()
{ int i,a,b,c;
  nod *aux;
freopen("sate.in","r",stdin); scanf("%d %d %d %d\n",&n,&m,&x,&y);
for(i=1;i<=m;++i)
    {
    scanf("%d %d %d\n",&a,&b,&c);
    aux=new nod; aux->val=b; aux->cost=c; aux->urm=p[a]; p[a]=aux;
    aux=new nod; aux->val=a; aux->cost=-c; aux->urm=p[b]; p[b]=aux;
    }
fclose(stdin);
}
int solve()
{ bool viz[nd];
  int pr,u,i;
  nod *aux;
for(i=1;i<=n;++i)
    {
    viz[i]=0;
    sol[i]=0;
    }
pr=u=1; c[1]=x; viz[x]=1;
while(pr<=u)
    {
     aux=p[c[pr]];
     while(aux!=NULL)
            {
            if(viz[aux->val]==0)
                            {
                            viz[aux->val]=1;
                            c[++u]=aux->val;
                            sol[aux->val]=sol[c[pr]]+aux->cost;
                            if(aux->val==y)return sol[y];
                            }
            aux=aux->urm;
            }
    ++pr;
    }
return 0;
}
int main()
{ int res;
read();
res=solve();
freopen("sate.out","w",stdout); printf("%d",res); fclose(stdout);
return 0;
}