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

同志们帮我看看吧,最简单的DFS一直报WA,起码你也报TLE啊

Posted by xjtudaniel at 2009-09-21 16:06:27 on Problem 1724
所有的测试数据都过了....max初始为-1应该也可以输出-1啊

#include <stdio.h>
#include <stdlib.h>

int l[128][128];
int t[128][128];
int visit[128];
int n;
int coins;
int max = -1;

void init()
{
     int i=0,j=0;
     for(i=0;i<128;i++)
     {
        for(j=0;j<128;j++)
          l[i][j] = -1;
        visit[i] = -1;
     }
}

void dfs(int start,int now,int length)
{
    int i=0;
    for(i=0;i<n;i++)
    {
        if(l[start][i] != -1 && visit[i] == -1 &&
              (max == -1 || length+l[start][i] <max) && now+t[start][i] <= coins)
        {
            if(i == n-1)
            {
               max = length + l[start][i];
               continue;
            }
            visit[i] = 1;
            dfs(i,now + t[start][i],length+l[start][i]);
            visit[i] = -1;
        }    
    } 
}

int main(void)
{
    init();
    int roads;
    scanf("%d %d %d",&coins,&n,&roads);
    int i=0;
    for(i=0;i<roads;i++)
    {
         int a,b;
         scanf("%d %d",&a,&b);
         a--;
         b--;
         scanf("%d %d",&l[a][b],&t[a][b]);
    }
    visit[0]=1;
    dfs(0,0,0);
    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