Cod sursa(job #541685)

Utilizator ChallengeMurtaza Alexandru Challenge Data 25 februarie 2011 13:18:18
Problema Lazy Scor 10
Compilator cpp Status done
Runda Romanian Master in Mathematics and Sciences 2011, Ziua 1 Marime 2.43 kb
#include <fstream>
#include <vector>

using namespace std;

const char InFile[]="lazy.in";
const char OutFile[]="lazy.out";
const int MaxN=200111;
const int INF=1<<30;
ifstream fin(InFile);
ofstream fout(OutFile);

struct s_vedge
{
	s_vedge(int to2,long long tcost1, long long tcost2, int ind2):ind(ind2),to(to2),cost1(tcost1),cost2(tcost2){}
	int ind;
	int to;
	long long cost1,cost2;
};

long long cost1,cost2;
long long C[MaxN],D[MaxN];
int N,M,x,y,sum,T[MaxN],H[MaxN],HP[MaxN],IND[MaxN];
char V[MaxN];
vector<s_vedge> a[MaxN];
vector<int> sol;

inline bool better(int b, int a)
{
	if(D[b]<D[a])
	{
		return true;
	}
	if (D[b]>D[a])
	{
		return false;
	}
	if(C[b]>C[a])
	{
		return true;
	}
	return false;
}

inline void init()
{
	H[0]=N;
	H[1]=1; D[1]=0; HP[1]=1;
	for(register int i=2;i<=N;++i)
	{
		H[i]=i; D[i]=INF; HP[i]=i;
	}
}

inline int left(int nod)
{
	return nod<<1;
}

inline int right(int nod)
{
	return (nod<<1)+1;
}

inline int father(int nod)
{
	return nod>>1;
}

inline void upheap(int x)
{
	if(x<=H[0])
	{
		int t=father(x);
		while(t!=0 && better(H[x],H[t]))
		{
			swap(H[t],H[x]);
			swap(HP[H[t]],HP[H[x]]);
			x=t;
			t=father(x);
		}
	}
}

inline void downheap(int x)
{
	int a=x,r=right(x),l=left(x);
	if(l<=H[0]) 
	{
		if(better(H[l],H[a]))
		{
			a=l;
		}
	}
	if(r<=H[0])
	{
		if(better(H[r],H[a]))
		{
			a=r;
		}
	}
	while(a!=x)
	{
		swap(H[x],H[a]);
		swap(HP[H[x]],HP[H[a]]);
		x=a;
		r=right(x);
		l=left(x);
		if(l<=H[0]) 
		{
			if(better(H[l],H[a]))
			{
				a=l;
			}
		}
		if(r<=H[0])
		{
			if(better(H[r],H[a]))
			{
				a=r;
			}
		}
	}
}

inline void delheap(int x)
{
	swap(H[H[0]],H[x]);
	swap(HP[H[H[0]]],HP[H[x]]);
	HP[H[H[0]]]=0;
	H[H[0]]=0;
	--H[0];
	downheap(x);
}

int main()
{
	fin>>N>>M;
	for(register int i=1;i<=M;++i)
	{
		fin>>x>>y>>cost1>>cost2;
		a[x].push_back(s_vedge(y,cost1,cost2,i));
		a[y].push_back(s_vedge(x,cost1,cost2,i));
	}
	fin.close();

	init();
	for(register int i=1;i<=N;++i)
	{
		sum+=D[H[1]];
		int nod=H[1];
		sol.push_back(IND[nod]);
		V[nod]=1;
		delheap(1);
		for(vector<s_vedge>::iterator it=a[nod].begin();it!=a[nod].end();++it)
		{
			if(V[it->to]==0)
			{
				if(D[it->to]>it->cost1 || (D[it->to]==it->cost1 && C[it->to]<it->cost2))
				{
					IND[it->to]=it->ind;
					D[it->to]=it->cost1;
					T[it->to]=nod;
					upheap(HP[it->to]);
				}
			}
		}
	}

	for(vector<int>::iterator it=(sol.begin()+1);it!=sol.end();++it)
	{
		fout<<*it<<"\n";
	}
	fout.close();
	return 0;
}