Cod sursa(job #280660)

Utilizator runnaway90Oprescu Radu Constantin runnaway90 Data 13 martie 2009 15:10:25
Problema Radiatie Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.03 kb
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

#define N 15002
#define INF 1<<18


typedef struct capac{long v, c;} graf;
graf *a[N];
struct muchii{long x, y;} s[N];

long d[N], n, m, k, sol[N], h[N], fol[N], q[2*N];


void citire(), push(long, long, long);
int cmp(const void *b, const void *c){
long 	B = *(long *) b,
	C = *(long *) c;
	if ((s[B].x > s[C].x)||(s[B].x == s[C].x && s[B].y > s[C].y)) return 1;
	else	return -1;
}

void lee(long w){
long i, p , u, cur;
	for (i = 1; i <= n; i++)
		d[i] = INF;
	memset(fol+1, 0, n*sizeof(long));

	q[0] = w; fol[w] = 1; d[w] = 0;

	for (p = 0, u = 0; p <= u; p++){
	    cur = q[p];

	    for (i = 1; i <= a[cur][0].v; i++)
		if (d[cur] > a[cur][i].c){
			if (d[cur] < d[a[cur][i].v]){
				d[a[cur][i].v] = d[cur];
				if (!fol[a[cur][i].v]){
					q[++u] = a[cur][i].v;
					fol[a[cur][i].v] = 1;
				}
			}
		}
		else
			if (a[cur][i].c < d[a[cur][i].v]){
				d[a[cur][i].v] = a[cur][i].c;
				if (!fol[a[cur][i].v]){
					q[++u] = a[cur][i].v;
					fol[a[cur][i].v] = 1;
				}
			}

	    fol[cur] = 0;

	}

}



int main(){
int i;
	freopen("radiatie.in","r",stdin);
	freopen("radiatie.out","w",stdout);

	citire();

	for (i = 1; i <= k; i++)
	    if(s[h[i]].x != s[h[i-1]].x){
		lee(s[h[i]].x);
		sol[h[i]] = d[s[h[i]].y];
	    }
	    else
		sol[h[i]] = d[s[h[i]].y];

	for (i = 1; i <= k; i++)
	    printf("%ld\n", sol[i]);

	return 0;
}

void push(long xx, long yy, long zz){
	a[xx][0].v ++;
	a[xx] = (graf *) realloc(a[xx], (a[xx][0].v + 1) * sizeof(graf));
	a[xx][a[xx][0].v].v = yy;
	a[xx][a[xx][0].v].c = zz;

}


void citire(){
long i, xx, yy, zz;
	scanf("%ld %ld %ld", &n, &m, &k);
	for (i = 1; i <= n; i++){
		a[i] = (graf*) malloc ( sizeof(graf) );
		a[i][0].v = 0;
	}

	for (i = 1; i <= m; i++){
		scanf("%ld %ld %ld", &xx, &yy, &zz);
		push(xx, yy, zz);
		push(yy, xx, zz);

	}

	for (i = 1; i <= k; i++){
		scanf("%ld %ld", &s[i].x, &s[i].y);
		h[i] = i;
	}

	qsort(h+1, k, sizeof(long), cmp);
}