Cod sursa(job #2380907)

Utilizator gheorghita.pavelGheorghita Pavel gheorghita.pavel Data 15 martie 2019 17:20:04
Problema Sate Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.61 kb

#include <bits/stdc++.h>
 #include<fstream>  
   
 using namespace std;  
   
 ifstream fin("sate2.in");  
 ofstream fout("sate2.out");  
   
 struct nod { int v,dist;  
              nod *next;  
            } *v[30001];  
 int n,m,e,xx,yy,s[30001],ww[30001],d[30001];   
   
 void citire()    
 {   int i,x,y,z;    
     nod *nou;    
     fin>>n>>m>>xx>>yy;   
     for(i=1;i<=m;i++)    
     {   fin>>x>>y>>z;   
         if(x==xx) d[y]=z;  
           nou=new nod;    
           nou->v=y;    
           nou->dist=z;  
           nou->next=v[x];    
           v[x]=nou;    
           nou=new nod;    
           nou->v=x;    
           nou->dist=z;  
           nou->next=v[y];    
           v[y]=nou;    
     }    
 }    
   
 void distanta(int nd)  
 {    int st=1,dr=1,i;  
      s[dr]=nd;  
      ww[nd]=1;  
      nod *p;  
      while(st<=dr)  
      {            p=v[s[st]];  
                   while(p!=NULL)  
                   {             if(ww[p->v]==0)  
                                 {               ww[p->v]=1;  
                                                 s[++dr]=p->v;   
                                                 if(s[dr]>s[st]) d[s[dr]]=d[s[st]]+p->dist;  
                                                   else       d[s[dr]]=d[s[st]]-p->dist;
                                 }  
                                 p=p->next;  
                   }  
                   st++;  
      }  
 }  
    
 int main()  
 {   citire();  
     distanta(xx);  
     fout<<d[yy];  
     fin.close();  
     fout.close();  
     return 0;  
}