Cod sursa(job #748530)

Utilizator MagnvsDaniel Constantin Anghel Magnvs Data 13 mai 2012 17:52:47
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.52 kb
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>

using namespace std;

struct edge
{
    int dist,pct;
};

char *buffer;
vector <edge> g[30001];
int x,y,vis[30001],pos[30001],l[30001];

void read(int &x){
    while (*buffer<'0'|| *buffer>'9'){
        ++buffer;
    }

    x=0;
    while (*buffer>='0'&& *buffer<='9'){
        x=x*10+*buffer-'0';
        ++buffer;
    }
}

void dfs(int j)
{
    if (j==y)
    {
        printf("%d\n",pos[j]-pos[x]);
        return;
    }
    unsigned int i;
    for (i=0;i<g[j].size();++i)
        if (!vis[g[j][i].pct])
        {
            vis[g[j][i].pct]=1;
            if (g[j][i].pct>j)
                pos[g[j][i].pct]=pos[j]+g[j][i].dist;
            else
                pos[g[j][i].pct]=pos[j]-g[j][i].dist;
            dfs(g[j][i].pct);
            if (vis[y]) return;
        }
}

int main()
{
    int i,a,b,c,n,m, fs;
    edge aux;
    assert(freopen("sate.in","r",stdin));
    fseek(stdin, 0, SEEK_END);
    fs=ftell(stdin);
    buffer=(char*)malloc(fs);
    rewind(stdin);
    assert(fread(buffer, 1, fs, stdin));

    assert(freopen("sate.out","w",stdout));
    memset(pos,0x3f,sizeof(pos));
    read(n);
    read(m);
    read(x);
    read(y);
    if (x>y)
    {
        a=x;
        x=y;
        y=a;
    }
    for (i=1;i<=m;++i)
    {
        read(a);
        read(b);
        read(c);
        aux.dist=c;
        aux.pct=b;
        g[a].push_back(aux);
        aux.pct=a;
        g[b].push_back(aux);
    }
    pos[x]=0;vis[x]=1;
    dfs(x);
    return 0;
}