http://poj.org/problem?id=1724
居然直接Dfs+剪枝就可以过 受不了啦!!!!!!!
题目大意:
n个城市间有单向路 路有长度和花费两个参数
问在总花费不超过特定值的情况下从1到n的最短路 包括到不了的情况
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<algorithm>
#include<set>
using namespace std;
const int W=101;
struct node
{
struct tt *next;
}mem[W];
struct tt
{
int j;
int L,T;
struct tt *next;
};
int K,N,R,ans;
int had[W];
void build(int i,int j,int l,int T)
{
struct tt *t=new tt;
t->j=j;
t->L=l;
t->T=T;
t->next=mem[i].next;
mem[i].next=t;
}
void dele()
{
for(int i=1;i<=N;++i)
{
mem[i].next=NULL;
}
}
void Dfs(int x,int length,int pay)
{
//cout<<x<<endl<<endl;;
struct tt *t=new tt;
t=mem[x].next;
while(t!=NULL)
{
if(had[t->j]==false&&pay+t->T<=K)
{
if(t->j==N)
{
if(ans==-1||length+t->L<ans)
ans=length+t->L;
}else if(ans==-1||length+t->L<ans)
{
had[t->j]=true;
Dfs(t->j,length+t->L,pay+t->T);
had[t->j]=false;
}
}
t=t->next;
}
}
int main()
{
while(scanf("%d %d %d",&K,&N,&R)!=EOF)
{
int s,d,l,t;
while(R--)
{
scanf("%d %d %d %d",&s,&d,&l,&t);
build(s,d,l,t);
}
memset(had,false,sizeof(had));
ans=-1;
had[1]=true;
Dfs(1,0,0);
printf("%d\n",ans);
dele();
}
return 0;
}
浙公网安备 33010602011771号