Cod sursa(job #686151)

Utilizator BlaugranasEnal Gemaledin Blaugranas Data 21 februarie 2012 14:32:25
Problema Sate Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.41 kb
#include<cstdio>
#include<vector>
using namespace std;
#define N 30001
#define pb push_back
vector<int> h[N],z[N];
vector<int>::iterator t,l;
struct P
{int a,b;};
P s[N];
bool c[N];
int i,n,m,x,y,j,p,u,k;
int main()
{FILE *f=fopen("sate.in","r"),*g=fopen("sate.out","w");
fscanf(f,"%d%d%d%d",&n,&m,&x,&y),c[x]=1;
while(m--)
      fscanf(f,"%d%d%d",&i,&j,&k),h[i].pb(j),h[j].pb(i),z[i].pb(k),z[j].pb(-k);
for(t=h[x].begin();t<h[x].end();t++,u++)
if((*t)>x)
      s[u].a=x,s[u].b=(*t);
else
      s[u].a=(*t),s[u].b=x;
while(p<u)
      {if(c[y])
             break;
      i=s[p].a,j=s[p++].b;
      for(t=h[i].begin(),l=z[i].begin(),k=0;t<h[i].end()&&!k;t++,l++)
      if((*t)==j)
             k=(*l);
      if(!c[i])
             {for(t=h[i].begin(),l=z[i].begin(),c[i]=1;t<h[i].end();t++,l++)
             if(!c[*t])
                    if((*t)<j)
                           s[u].a=(*t),s[u++].b=j,h[*t].pb(j),z[*t].pb(k-(*l));
                    else
                           s[u].a=j,s[u++].b=(*t),h[j].pb(*t),z[j].pb((*l)-k);}
      else
             if(!c[j])
                    for(t=h[j].begin(),l=z[j].begin(),c[j]=1;t<h[j].end();t++,l++)
                    if(!c[*t])
                          s[u].a=i,s[u++].b=(*t),h[i].pb(*t),z[i].pb((*l)+k);}
for(t=h[x].begin(),l=z[x].begin();t<h[x].end()&&l<z[x].end();t++,l++)
if((*t)==y)
      {fprintf(g,"%d",(*l));
      break;}
return 0;}