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 16ms

Posted by 1004101213 at 2012-03-12 18:49:42 on Problem 1062
不知道最短路咋做的,先dfs过了再说吧!
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<stdlib.h>
#include<algorithm>
using namespace std;
struct P
{
    int v,cl,m;
}node[105];
int map[105][105];
int s[105];
int m,n;
int low,top;
int dfs(int i)
{
    if(s[i])return 100000000;
    s[i]=1;
    if(top<node[i].cl)
    top=node[i].cl;
    if(low>node[i].cl)
    low=node[i].cl;
    int minn,tmp(0),j,k;
    minn=node[i].v;
       for(k=1;k<105;k++)
       if(map[i][k]&&abs(top-node[k].cl)<=m&&abs(low-node[k].cl)<=m)
       {
         tmp=dfs(k);
         if(minn>tmp+map[i][k])
         minn=tmp+map[i][k];
       }
       //cout<<i<<" "<<minn<<endl;
       s[i]=0;
   return minn;
}
int main()
{
    int i,j,a,b;
    scanf("%d%d",&m,&n);
    memset(map,0,sizeof(map));
    memset(s,0,sizeof(s));
    for(i=1;i<=n;i++)
    {
       scanf("%d%d%d",&node[i].v,&node[i].cl,&node[i].m) ;
       for(j=0;j<node[i].m;j++)
      {
       scanf("%d%d",&a,&b);
       map[i][a]=b;
      }
    }
    top=-1;low=1000;
    printf("%d\n",dfs(1));
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