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 201006021028 at 2012-03-10 18:18:27 on Problem 1613
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
#define INF 1100
#define MAXN   99999999
struct node
{
    int t;
    int p;
}p[INF*INF];

struct edge
{
    int u,v,len;
    int mint,maxt;
}e[INF];
int s,tt,n,m,tot,MaxOfTime,MaxOfLen;
char str[360000];
int tim[40],next[INF],first[INF];
int vis[INF];
void insert(int u,int v,int t1,int t2,int len)
{
    if(t2-t1<len)  return;
    tot++;
    next[tot]=first[u];
    first[u]=tot;
    e[tot].u=u;   e[tot].v=v;   e[tot].mint=t1; e[tot].maxt=t2-len; e[tot].len=len;
    
    tot++;
    next[tot]=first[v];
    first[v]=tot;
    e[tot].u=v;   e[tot].v=u;   e[tot].mint=t1; e[tot].maxt=t2-len; e[tot].len=len;
}

int maxx(int x,int y)  {return x>y?x:y;}

void bfs()
{
    int u,v,len,i,t1,t2,t;
    memset(vis,0,sizeof(vis));
    queue<int>q;
    q.push(s);
    for(i=1;i<=n;i++)  p[i].t=MAXN;
    p[s].t=0;
        while(!q.empty()){
            u=q.front();  q.pop(); vis[u]=0;  
          //  printf("u=%d\n",u);
            for(i=first[u];i!=-1;i=next[i]){
      //          printf("i=%d\n",i);
    //            system("pause");
                v=e[i].v;  len=e[i].len;  t=p[u].t;
                t1=e[i].mint;   t2=e[i].maxt;
                if(t>t2) continue;
                t=maxx(t1,t);
                if(t+len<p[v].t)
                {
                    p[v].t=t+len;
                    if(vis[v]==0){
                        vis[v]=1;
                        q.push(v);
                    }
                }
            }
        }
        if(p[tt].t==MAXN)  printf("*\n");
        else printf("%d\n",p[tt].t);
    
}


int main()
{
     int u,v,len,NumOfTime,i,j;
     char c;
     while(scanf("%d",&n) && n){
         scanf("%d%d%d",&m,&s,&tt);
         tot=0; 
         memset(next,-1,sizeof(next));
         memset(first,-1,sizeof(first));
         memset(e,-1,sizeof(e));
         for(j=1;j<=m;j++){
             memset(tim,0,sizeof(tim));
             scanf("%d%d%d",&u,&v,&len);
             NumOfTime=0;
             while(1){
                 scanf("%c",&c);
                 if(c!=' ' && (c<'0' || c>'9'))  break;
                 scanf("%d",&tim[++NumOfTime]);   
             }
             for(i=1;i<=NumOfTime;i+=2)       insert(u,v,tim[i-1],tim[i],len);
             if(NumOfTime%2==0)   insert(u,v,tim[NumOfTime],MAXN,len);
         }
         if(s==tt)  {printf("0\n"); continue;}
  /*       for(i=1;i<=n;i++)  printf("first[%d]=%d\n",i,first[i]);
         for(i=1;i<=tot;i++)  printf("next[%d]=%d\n",i,next[i]);
    */     bfs();
     }
     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