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呢,帮忙看看啊

Posted by huyizhu at 2009-09-01 11:12:29 on Problem 3253
#include<iostream>
using namespace std;

struct node{
        int weight;
        int parent;
        int left;
        int right;
        };

struct node hf[20001000];

int n,m;
long long weight[20001000];
       
void HuffmanTree (int n)
{
     int i,j,mw1,mw2,node1,node2;
     
     for(i = 0;i < 2*n-1;i++)
     {
           if(i < n) {hf[i].weight = weight[i];}
           else hf[i].weight = 0;
           hf[i].parent = -1; hf[i].left = -1; hf[i].right = -1;
     }
     for(i = n;i < 2*n-1;i++)
     {
           mw1 = mw2 = 1000010000;
           node1 = node2 = -1;
           for(j = 0;j <= i-1;j++)
           {
				 if (hf[j].parent  != -1) continue;
                 if(hf[j].weight < mw1)
                 {
                                 mw2 = mw1;
                                 node2 = node1;
                                 mw1 = hf[j].weight;
                                 node1 = j;
                 }
                 else if (hf[j].weight < mw2)
                 {
                      mw2 = hf[j].weight;
                      node2 = j;
                 }
           }
     
     hf[i].weight = hf[node1].weight + hf[node2].weight;
     hf[node1].parent = i;
     hf[node2].parent = i;
     
     hf[i].left = node1;
     hf[i].right = node2;
     }
} 

int main()
{ 
   
     int i;
     cin>>n;
     for (i=0;i<n;i++) 
     cin>>weight[i];
     HuffmanTree(n);
     long long sum = 0;
     for (i = n; i < 2*n-1; ++i)
     {
		
     	sum += hf[i].weight;
	 }
	
	 cout << sum << endl;
	 
     
  
}        

       

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