正题
大意
有n个点,m辆车,每辆车有开车时间,需要走多久和需要多少钱。求在t个时间单位内到达可以需要花掉的最少价格
解题思路
暴力搜索能过就对了
代码
#include<cstdio>
#include<iostream>
using namespace std;
struct line{int next,to,st,ov,cost;
}a[101];
int n,ls[101],mins,xx,yy,sts,ovs,costs,m,mn,t;
bool walk[101];
void add(int xx,int yy,int sts,int ovs,int costs)
{a[++m].next=ls[xx];a[m].to=yy;if (xx==1 && sts==0) sts=1;a[m].st=sts;a[m].ov=ovs;a[m].cost=costs;ls[xx]=m;
}//邻接表加边
void dfs(int x,int ans,int longg)
{//printf(" |%d(%d)(%d)| ",x,ans,longg);if (x==n && longg<=t) {mins=min(mins,ans);//求最小值return;// printf("*(%d)",ans);}walk[x]=true;//封路for (int q=ls[x];q;q=a[q].next){if (!walk[a[q].to] && longg<a[q].st){// printf("\n%d(>)\n",q);dfs(a[q].to,ans+a[q].cost,a[q].ov);//搜索// printf(" |%d| ",x);}}walk[x]=false;//回朔//printf("\n(<)\n");
}
int main()
{freopen("shaxu.in","r",stdin);freopen("shaxu.out","w",stdout);mins=2147483647;scanf("%d%d%d",&n,&t,&mn);for (int i=1;i<=mn;i++){scanf("%d%d%d%d%d",&xx,&yy,&sts,&ovs,&costs);add(xx+1,yy+1,sts,ovs,costs);}dfs(1,0,0);if (mins!=2147483647) printf("%d",mins);else printf("-1");
}