Cod sursa(job #1721352)

Utilizator PopoviciRobertPopovici Robert PopoviciRobert Data 25 iunie 2016 13:35:46
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.64 kb
#include <cstdio>
#include <vector>
#define MAXN 30000
#define MAXBUF (1<<20)
FILE*fi,*fout;
using namespace std;
class Muchie{
    public :
     int cost;
     int nod;
};
vector <Muchie> G[MAXN+1];
int dist[MAXN+1],viz[MAXN+1],cod[MAXN+1];
inline void BFS(int nod){
    int b,e,i;
    b=0;
    e=1;
    cod[0]=nod;
    viz[nod]=1;
    dist[nod]=0;
    do{
        for(i=0;i<G[cod[b]].size();i++){
          nod=G[cod[b]][i].nod;
          if(viz[nod]==0){
             viz[nod]=1;
             cod[e++]=nod;
             if(cod[b]>nod)
               dist[nod]=dist[cod[b]]-G[cod[b]][i].cost;
             else
               dist[nod]=dist[cod[b]]+G[cod[b]][i].cost;
          }
        }
        b++;
    }while(b<e);
}
char buf[MAXBUF];
int pos=MAXBUF;
inline char nextch(){
    if(pos==MAXBUF){
       fread(buf,1,MAXBUF,fi);
       pos=0;
    }
    return buf[pos++];
}
inline int getnr(){
   char a=nextch();
   while(a<'0'||a>'9')
      a=nextch();
   int nr=0;
   while(a>='0'&&a<='9'){
      nr=nr*10+a-'0';
      a=nextch();
   }
   return nr;
}
int main(){
    int i,n,m,x,y,a,b,d;
    fi=fopen("sate.in" ,"r");
    fout=fopen("sate.out" ,"w");
    //fscanf(fi,"%d%d%d%d" ,&n,&m,&x,&y);
    n=getnr();
    m=getnr();
    x=getnr();
    y=getnr();
    for(i=0;i<m;i++){
      // fscanf(fi,"%d%d%d" ,&a,&b,&d);
       a=getnr();
       b=getnr();
       d=getnr();
       Muchie x;
       x.nod=b;
       x.cost=d;
       G[a].push_back(x);
       x.nod=a;
       G[b].push_back(x);
    }
    BFS(x);
    fprintf(fout,"%d" ,dist[y]);
    fclose(fi);
    fclose(fout);
    return 0;
}