Cod sursa(job #1697528)

Utilizator CodrutLemeniCodrut Lemeni CodrutLemeni Data 2 mai 2016 12:57:34
Problema Sate Scor 45
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.29 kb
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#define N 30000

using namespace std;

struct muchie{
    int m,d;
};

struct muchie temp;

std::vector<muchie> muc[N];
int dist,viz[N],grad[N],x,y;

void parg(int c){
    int mc,val,i;

    if(c==y){
        printf("%d",dist);
        exit(0);
    }

    for(i=0;i<grad[c];i++){
        temp=muc[c][i];
        mc=temp.m;
        val=temp.d;

        if(viz[ mc ]==0 ){
            if(c>mc){
                viz[mc]=1;

                dist-=val;
                parg(mc);
                dist+=val;
            }else{
                viz[mc]=1;

                dist+=val;
                parg(mc);
                dist-=val;
            }
        }

    }


}

int main(){
    int i,n,m,n1,n2,d,t;

    freopen("sate.in","r",stdin);
    freopen("sate.out","w",stdout);

    scanf("%d%d",&n,&m);

    scanf("%d%d",&x,&y);

    for(i=0;i<m;i++){
        scanf("%d%d%d",&n1,&n2,&d);
        temp.d=d;
        grad[n1]++;
        grad[n2]++;

        temp.m=n2;
        muc[n1].push_back(temp);

        temp.m=n1;
        muc[n2].push_back(temp);

    }

    if(x>y){
        t=x;
        x=y;
        y=t;
    }

    viz[x]=1;
    parg(x);

    return 0;
}