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

我按树做的,超时了,好像是因为递归栈溢出,谁说一下

Posted by shit at 2004-11-05 22:23:18 on Problem 1985
In Reply To:是有向图还是无向图啊?题目表述不清。 Posted by:tq at 2004-11-05 21:37:14
#include <iostream>
#include <cstdlib>
#include <memory>
#include <list>
using namespace std;
const int maxn=40001;
struct node_type
{
    int rec,val;
};
struct dist_type
{
    int pre,val,rec;
}dist[maxn];
list<node_type>li[maxn];
void solve(int root)
{
    list<node_type>::iterator i;
    if(li[root].empty())return;
    for(i=li[root].begin();i!=li[root].end();i++)
    {
        solve(i->rec);
        if(dist[i->rec].val+i->val>dist[root].val)
           dist[root].val=dist[i->rec].val+i->val;
    }
}
int main(int argc, char *argv[])
{
    int i,j,r,x,y,sum,max,temp_1,temp_2,n,m;char c;
    list<node_type>::iterator li_i;
    node_type node;
    while(cin>>n>>m)
    {
          memset(dist,0,sizeof(dist));
          for(i=1;i<=n;i++)
              li[i].clear();
          for(i=1;i<=n;i++)dist[i].rec=i;
          for(i=0;i<m;i++)
          {
              cin>>x>>y>>sum>>c;
              node.val=sum;
              if(c=='W' || c=='N')
              {
                  dist[x].pre=y;
                  node.rec=x;
                  li[y].push_back(node);
              }
              else 
              {
                   dist[y].pre=x;
                   node.rec=y;
                   li[x].push_back(node);
               }
           }
           for(j=1;j<=n;j++)
               if(!dist[j].pre)
               {
                   r=j;
                   solve(r);
                }
           max=0;
           for(i=1;i<=n;i++)
           {
                  temp_1=temp_2=0;
                  for(li_i=li[i].begin();li_i!=li[i].end();li_i++)
                  {
                      if(li_i->val+dist[li_i->rec].val>temp_1)
                         temp_1=li_i->val+dist[li_i->rec].val;
                      else
                      if(li_i->val+dist[li_i->rec].val>temp_2)
                         temp_2=li_i->val+dist[li_i->rec].val;
                  }
                  if(temp_1+temp_2>max)max=temp_1+temp_2;
           }
           for(i=1;i<=n;i++)
              if(dist[i].val>max)
                  max=dist[i].val;
            cout<<max<<endl;
    }
    system("PAUSE");	
    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