题目链接:点击打开链接
题目大意:有n个点,给你r条边,再给你k块钱,r行数据中每行4个整数S,D,L,T代表从S到D点的长度为L,需要花费为T,问你从1到n点在k块钱足够的情况下最短路长是多少?
题目解析:简单的搜索,和POJ3411的做法差不多,记得如果当前状态已经大于答案了或者钱已经不够了就要及时return,没有必要再继续搜下去,然后就没啥啦。
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cstring>
#include <iomanip>
#include <string>
#include <vector>
#include <cstdio>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <set>
#define LL long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int m = 100007;
const double esp = 1e-6;
const double PI = 3.14159265359;
const int INF = 0x3f3f3f3f;
using namespace std;
const int maxn = 10005;
struct node{
int v,len,toll,next;
}Map[maxn];
int n,r,s,d,l,t,tot;
int head[105];
int vis[105],ans;
void add_edge(){
Map[tot].v = d;
Map[tot].len = l;
Map[tot].toll = t;
Map[tot].next = head[s];
head[s] = tot;
tot += 1;
}
void dfs(int fr,int toll,int dis){
if(dis > ans)
return ;
if(fr == n && toll >= 0){
ans = min(ans,dis);
return ;
}
for(int i=head[fr];i!=-1;i=Map[i].next){
if(!vis[Map[i].v] && toll >= Map[i].toll){
vis[Map[i].v] = true;
dfs(Map[i].v,toll-Map[i].toll,dis+Map[i].len);
vis[Map[i].v] = false;
}
}
return ;
}
int main(){
int k;
while(~scanf("%d %d %d",&k,&n,&r)){
memset(Map,INF,sizeof(Map));
memset(head,-1,sizeof(head));
tot = 0;
for(int i=0;i<r;i++){
scanf("%d %d %d %d",&s,&d,&l,&t);
add_edge();
}
memset(vis,false,sizeof(vis));
ans = INF;
dfs(1,k,0);
printf("%d\n",ans==INF?-1:ans);
}
return 0;
}