Cod sursa(job #593438)

Utilizator Magnuscont cu nume gresit sau fals Magnus Data 2 iunie 2011 19:58:55
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.16 kb
#include <cstdio>
#include <cstring>
#include <vector>

using namespace std;

struct edge
{
    int dist,pct;
};

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

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;
    edge aux;
    freopen("sate.in","r",stdin);
    freopen("sate.out","w",stdout);
    memset(pos,0x3f,sizeof(pos));
    scanf("%d %d %d %d\n",&n,&m,&x,&y);
    if (x>y)
    {
        a=x;
        x=y;
        y=a;
    }
    for (i=1;i<=m;++i)
    {
        scanf("%d %d %d\n",&a,&b,&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;
}