Cod sursa(job #271925)

Utilizator nusmaibunkeleviprofesor cicalescu nusmaibunkelevi Data 6 martie 2009 08:10:30
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.35 kb
#include<stdio.h>
#include<stdlib.h>

#define NM 30001
#define MM 100025
struct nod{
	int b,d;
	nod*next;
	};
struct lista{nod *vf,*sf;};
lista v[MM+1],w[MM+1];
int n,m,x,y,dxy;
int c[NM+1],li=1,ls,viz[NM+1],d[NM+1];

void addend(lista& l,int x,int y){
	nod*n=new nod;
	n->b=x,n->d=y;
	n->next=NULL;
	if(!l.vf) l.vf=n;
	else l.sf->next=n;
	l.sf=n;
}

int cvida(){return li>ls;}
void pune(int x){c[++ls]=x,viz[x]=1;}
int scoate(){return c[li++];}

int main(){
freopen("sate.in","r",stdin);
freopen("sate.out","w",stdout);
int i,p,gata=0,z,u,b,l,_a,_b,_d;
char sir[41],*ps;
scanf("%d%d%d%d\n",&n,&m,&x,&y);
for(i=1;i<=m;++i){
	fgets(sir,40,stdin);
	ps=sir;
	_a=atoi(ps);
	while(*ps!=32) ps++;ps++;
	_b=atoi(ps);
	while(*ps!=32) ps++;ps++;
	_d=atoi(ps);
	addend(v[_a],_b,_d);
    addend(v[_b],_a,_d);
	}
pune(x);
nod *nn;
while(!gata&&!cvida()){
	z=scoate();
	nn=v[z].vf;
	if(nn){
		while(nn){
			u=nn->b;
			if(!viz[u]) {
				pune(u);
				if(u>z) d[u]=d[z]+nn->d;
				else d[u]=d[z]-nn->d;
				}
			if(u==y) {dxy=d[u];gata=1;break;}
			nn=nn->next;
			}
		}
	if(gata) break;
	nn=w[z].vf;
	if(nn){
		while(nn){
			u=nn->b;
			if(!viz[u]) {
				pune(u);
				if(u>z) d[u]=d[z]+nn->d;
				else d[u]=d[z]-nn->d;
				}
			if(u==y) {dxy=d[u];gata=1;break;}
			nn=nn->next;
			}
		}
	}
printf("%d",dxy);
return 0;
}