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

求教。。一直tle mle ...

Posted by faintxcl at 2009-03-05 19:24:46 on Problem 3249
#include<iostream>
#include<queue>


#define inf -2100000000
#define N 100001
//using namespace std;


struct Node{

	int v;
	 Node *next;
	Node(){next=NULL;}
}*node[N];

int n,m;

int value[N];
int in[N];
int out[N];

//queue<int >Q;
int Q[N];

void AddEdge(int s,int e)
{
	Node *p;
	p=(Node *)malloc(sizeof(Node));
	p->v=e;
	p->next=node[s];
	node[s]=p;
	
}
int  dp[N];
void TopSort(){

	memset(Q,0,sizeof(Q));
	int front=0,tail=0;	
	for(int i=1; i<=n; i++)
		if(in[i]==0){
			//Q.push(i);
			Q[tail++]=i;
			dp[i]=value[i];
		}

	while(front!=tail)
	{
		int t=Q[front++];
		Node *adj;
		for(adj=node[t];adj!=NULL;adj=adj->next)
		{
			if(--in[adj->v]==0)Q[tail++]=adj->v;

			if(dp[t]+value[adj->v]>dp[adj->v])
				dp[adj->v]=dp[t]+value[adj->v];
		}
	}



}
int main(){

	int Max;
	while(scanf("%d %d",&n,&m)!=EOF)
	{
		for(int i=1; i<=n; i++){
			node[i]=NULL;
			dp[i]=inf;
		}		
		for(int i=1; i<=n; i++){
			scanf("%d",&value[i]);
		}

		int s,e;
		memset(in,0,sizeof(in));
		memset(out,0,sizeof(out));

		for(int i =0; i<m; i++)
		{
			scanf("%d %d",&s,&e);
			AddEdge(s,e);
			in[e]++;
			out[s]++;
		}

		TopSort();
		Max=inf;
		for(int i=1; i<=n; i++)
		{
			if(out[i]==0&&Max<dp[i])Max=dp[i];
	
	
		}

		printf("%d\n",Max);

	}
	
	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