Cod sursa(job #549049)

Utilizator cristiprgPrigoana Cristian cristiprg Data 8 martie 2011 09:37:24
Problema Lazy Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.43 kb
#include <cstdio>
#include <set>

using namespace std;
#define DIM 200002
#define ll long long
#define mp make_pair
const long long INF = 1ll<<60;

struct nod
{
	nod *next;
	int v, index;
	ll c1;
	ll c2;	
	bool friend operator <(const nod &a, const nod &b)
	{
		if (a.c1 < b.c1 )	
			return true;
		else
			if (a.c1 == b.c1 && a.c2 > b.c2)
				return true;
		return false;
				
	}
}*G[DIM];


int n, m;
ll d[DIM];
set<pair<int, int > > S;

void add(int i, int j, int index, ll c1, ll c2)
{
	nod *t = new nod;
	t -> index = index;
	t -> v = j;
	t -> c1 = c1;
	t -> c2 = (c2);
	t -> next = G[i];
	G[i] = t;
}

void afis()
{
	for (int i = 1; i <= n; ++i)
	{
		printf("%d: ", i);
		for (nod *t = G[i]; t != NULL; t = t->next)
			printf("%d ", t->v);
		printf("\n");
	}
}

void read()
{
	FILE *f = fopen("lazy.in", "r");
	fscanf(f, "%d%d", &n, &m);
	for (ll i = 1, x, y, c1, c2; i <= m; ++i)
	{
		fscanf(f, "%lld%lld%lld%lld", &x, &y, &c1, &c2);
		add(x, y, i, c1, c2);
		add(y, x, i, c1, c2);
	}
//	afis();
}

void prim()
{
	FILE *f = fopen("lazy.out", "w");
	for (int i = 1; i <= n; ++i)
		d[i] = INF;
	d[1] = 0;
	S.insert(mp(0, 1));
	while (!S.empty())
	{
		int i = S.begin()->second;
		S.erase(*S.begin());
		for (nod *t = G[i]; t; t = t->next)
			if (d[i] + t->c1 + t->c2 < d[t->v])
			{
				d[t->v] = d[i] + t->c1 + t->c2;
				S.insert(mp(d[t->v], t->v));
				fprintf(f, "%d\n", t->index);
			}
	}
	fclose(f);
}

int main()
{
	read();
	
	prim();
	return 0;
}