Cod sursa(job #648784)

Utilizator pauldbPaul-Dan Baltescu pauldb Data 14 decembrie 2011 14:48:26
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.1 kb
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>

#define maxn 30010
#define maxx 200060
#define maxm 100060

int n,m,x,y,l;
int px[maxm],py[maxm],pz[maxm];
int a[maxx],c[maxx];
int g[maxn],cost[maxn],s[maxn];

int main()
{
    freopen("sate.in","r",stdin);
    freopen("sate.out","w",stdout);
    
    int i,j;
    
    scanf("%d %d %d %d ",&n,&m,&x,&y);
    
    for (i=1;i<=m;i++)
    {
        scanf("%d %d %d ",&px[i],&py[i],&pz[i]);
        g[px[i]]++;
        g[py[i]]++;
    }
    
    for (i=2;i<=n+1;i++) g[i]+=g[i-1];
    
    for (i=1;i<=m;i++)
    {
        a[g[px[i]]]=py[i];
        a[g[py[i]]]=px[i];
        c[g[px[i]]--]=pz[i];
        c[g[py[i]]--]=pz[i];
    }
    
    for (i=1;i<=n+1;i++) g[i]++;
    
    memset(cost,-1,sizeof(cost));
    cost[x]=0;
    l=1;
    s[1]=x;
    
    for (i=1;i<=l;++i)
      for (j=g[s[i]];j<g[s[i]+1];++j)
        if (cost[a[j]]==-1)
        {
             s[++l]=a[j];
             if (s[l]<s[i]) cost[s[l]]=cost[s[i]]-c[j];
             else cost[s[l]]=cost[s[i]]+c[j];
        }
        
    printf("%d\n",cost[y]-cost[x]);
    
    return 0;
}