| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
为什么会TLE呢,帮忙看看啊#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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator