1724ROADS

广搜省时在这道题目就明显得看出来

而且不一定要走全,但是一定要走到最后那个点

我的代码超时

#include "iostream"
#include "string.h"
#include "algorithm"
using namespace std;
struct{
  int a,b,l,t,pre;
}edge[10010];

int f[120],next[120],longth,MIN,k,m,n,num;

void add(int a,int b,int l,int t){
  edge[num].b=b;
  edge[num].l=l;
  edge[num].t=t;
  edge[num].pre=next[a];
  next[a]=num++;
}



void dfs(int x,int sum){
  int flag=0,i;
  if(f[x]==4){return;}
  if(sum>k){return;}
  if(x==n){
    if(MIN>longth)MIN=longth;
    return;
  }
  for(i=next[x];i!=-1;i=edge[i].pre){
    f[edge[i].b]++;
    longth+=edge[i].l;
    //cout<<x<<' '<<edge[i].b<<' '<<sum<<' '<<longth<<endl;system("pause");
    dfs(edge[i].b,sum+edge[i].t);
    longth-=edge[i].l;
    f[edge[i].b]--;
  }
}

int main(){
  int i,a,b,l,t;
  cin>>k>>n>>m;
  num=0;
  memset(f,0,sizeof(f));
  memset(next,-1,sizeof(next));
  for(i=1;i<=m;i++){
    cin>>a>>b>>l>>t;
    add(a,b,l,t);
  }
  MIN=100000;
  longth=0;
  f[1]=1;
  dfs(1,0);
  if(MIN==100000)cout<<"impossible"<<endl;
  else cout<<MIN<<endl;
}

人家的代码

#include<stdio.h>
#include<string.h>
#include<vector>
#include<queue>
#include "string.h"
#include "algorithm"
using namespace std;
#define maxn 20000
#define max 0x7fffffff

struct node
{
    int d;
    int l;
    int t;
    int next;

}p[maxn*2];
struct nn
{
    int v;
    int l;
    int mon;
     friend bool operator <(nn a,nn b)
    {
        return a.l>b.l;
    }
};
priority_queue<nn>g;
int num,k,n,r,vis[maxn*2],next[maxn],ans;

void add(int s,int d,int l,int t)
{
    p[num].d=d;
    p[num].l=l;
    p[num].t=t;
    p[num].next=next[s];
    next[s]=num++;


}
int BFS()
{
    struct nn d,b;
    while(!g.empty())g.pop();
    d.l=0;
    d.mon=0;
    d.v=1;
    g.push(d);
    while(!g.empty())
    {
        d=g.top();g.pop();

        int a=d.v;
        if(a==n)return d.l;

        for(int i=next[a];i!=-1;i=p[i].next)
        {
            if(d.mon+p[i].t<=k)
            {   //printf("%d %d %d %d\n",a,p[i].d,d.l,d.mon);system("pause");
                b.l=d.l+p[i].l;
                b.mon=d.mon+p[i].t;
                b.v=p[i].d;
                g.push(b);
            }
        }
    }
    return -1;

}

int main()
{
    int i,s,d,l,t;
    while(scanf("%d%d%d",&k,&n,&r)!=EOF)
    {

        num=0;
        memset(next,-1,sizeof(next));
        for(i=0;i<r;i++)
        {
            scanf("%d%d%d%d",&s,&d,&l,&t);
            add(s,d,l,t);
        }
        memset(vis,0,sizeof(vis));


       int ans=BFS();
       printf("%d\n",ans);


    }

}

 

 

posted @ 2013-10-11 21:34  龙城星  阅读(206)  评论(0编辑  收藏  举报