Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

为什么Dij+heap还TLE呢?貌似这种情况很多。

Posted by gxlzlihao at 2010-03-23 17:43:55 on Problem 3013
麻烦哪位大牛看看:
#include<iostream>
#define MAX 50001
#define MAXNUMBER 9223372036854775807
using namespace std;
__int64 dist[MAX];
unsigned long weights[MAX];
struct node
{
	int w;
	unsigned long value;
	node* next;
}*graph[MAX];
int numberofnodes,numberofedges;

int array[MAX];
int currentsize;
void insert(int x)
{
	array[++currentsize]=x;
	int hole=currentsize;
	for(;hole/2>0 && dist[hole/2]>dist[hole];hole/=2)
		array[hole]=array[hole/2];
	array[hole]=x;
}
void percolatedown(int hole)
{
	int child=hole;
	int temp=array[hole];
	for(child=hole;child*2<=currentsize;hole=child)
	{
		child=hole*2;
		if(child+1<=currentsize && dist[child+1]<dist[child])
			child++;
		if(dist[child]<temp)
			array[hole]=array[child];
		else
			break;
	}
	array[hole]=temp;
}
int delmin()
{
	int temp=array[1];
	array[1]=array[currentsize--];
	percolatedown(1);
	return temp;
}

void inti()
{
	memset(graph,NULL,sizeof(graph));
	for(int i=0;i!=MAX;++i)
		dist[i]=MAXNUMBER;
	memset(weights,0,sizeof(weights));
	numberofnodes=numberofedges=0;
	currentsize=0;
}
void put(int x,int y,unsigned long value)
{
	node* temp=graph[x];
	graph[x]=new node;
	graph[x]->w =y;
	graph[x]->value =value;
	graph[x]->next =temp;
}
void release()
{
	for(int i=0;i!=MAX;++i)
	{
		node* temp=graph[i],* tt;
		while(temp!=NULL)
		{
			tt=temp->next;
			delete temp;
			temp=tt;
		}
	}
}
void find(int s)
{
	int v,w;
	node* link;
	dist[s]=0;
	insert(s);
	while(currentsize!=0)
	{
		v=delmin();
		if(dist[v]!=MAXNUMBER)
			for(link=graph[v];link!=NULL;link=link->next )
				if(dist[v]+link->value <dist[w=link->w])
				{
					dist[w]=dist[v]+link->value ;
					insert(w);
				}
	}
}
bool check()
{
	for(int i=1;i<=numberofnodes;++i)
		if(graph[i]==NULL)
			return false;
	return true;
}
int main()
{
	int cases;
	cin>>cases;
	while(cases--)
	{
		inti();
		scanf("%d %d",&numberofnodes,&numberofedges);
		int i;
		for(i=1;i<=numberofnodes;++i)
			scanf("%d",&weights[i]);
		int x,y;
		unsigned long value;
		for(i=1;i<=numberofedges;++i)
		{
			scanf("%d %d %d",&x,&y,&value);
			put(x,y,value);
			put(y,x,value);
		}
		if(check() && numberofnodes!=0)
		{
		find(1);
		__int64 price=0;
		for(i=1;i<=numberofnodes;++i)
			price+=dist[i]*weights[i];
		printf("%I64d",price);
		}
		else
			cout<<"No Answer";
		release();
		cout<<endl;
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator