深搜+剪枝 POJ 1724 ROADS
POJ 1724 ROADS
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 12766 | Accepted: 4722 |
Description
Bob and Alice used to live in the city 1. After noticing that Alice was cheating in the card game they liked to play, Bob broke up with her and decided to move away - to the city N. He wants to get there as quickly as possible, but he is short on cash.
We want to help Bob to find the shortest path from the city 1 to the city N that he can afford with the amount of money he has.
Input
The second line contains the integer N, 2 <= N <= 100, the total number of cities.
The third line contains the integer R, 1 <= R <= 10000, the total number of roads.
Each of the following R lines describes one road by specifying integers S, D, L and T separated by single blank characters :
- S is the source city, 1 <= S <= N
- D is the destination city, 1 <= D <= N
- L is the road length, 1 <= L <= 100
- T is the toll (expressed in the number of coins), 0 <= T <=100
Notice that different roads may have the same source and destination cities.
Output
If such path does not exist, only number -1 should be written to the output.
Sample Input
5
6
7
1 2 2 3
2 4 3 3
3 4 2 4
1 3 4 1
4 6 2 1
3 5 2 0
5 4 3 2
Sample Output
11
#include<iostream>
using namespace std;
#include<cstdio>
#include<cstring>
#define N 105
#define R 10005
#define inf (1<<31)-1
struct Edge{
int v,last,lenth,val;
}edge[R];
int topt=,head[N],k,n,r,q;
bool vis[N]={};
int ans;
void input()
{
scanf("%d%d%d",&k,&n,&r);
for(int i=;i<=r;++i)
{
scanf("%d%d%d%d",&q,&edge[i].v,&edge[i].lenth,&edge[i].val);
edge[i].last=head[q];
head[q]=i;
}
}
void dfs(int u,int cost,int len)
{
if(cost>k||len>=ans) return;
if(u==n)
{
ans=min(ans,len);
return;
}
for(int l=head[u];l;l=edge[l].last)
{
if(vis[edge[l].v]) continue;
vis[edge[l].v]=true;
dfs(edge[l].v,cost+edge[l].val,len+edge[l].lenth);
vis[edge[l].v]=false;
}
}
int main()
{
input();
ans=inf;
dfs(,,);
if(ans==inf) printf("-1");
else printf("%d",ans);
return ;
}
深搜+剪枝 POJ 1724 ROADS的更多相关文章
- DFS(剪枝) POJ 1724 ROADS
题目传送门 题意:问从1到n的最短路径,同时满足花费总值小于等于k 分析:深搜+剪枝,如果之前走过该点或者此时的路劲长度大于最小值就不进行搜索. /************************** ...
- Hdu3812-Sea Sky(深搜+剪枝)
Sea and Sky are the most favorite things of iSea, even when he was a small child. Suzi once wrote: ...
- 深搜+回溯 POJ 2676 Sudoku
POJ 2676 Sudoku Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17627 Accepted: 8538 ...
- poj1190 生日蛋糕(深搜+剪枝)
题目链接:poj1190 生日蛋糕 解题思路: 深搜,枚举:每一层可能的高度和半径 确定搜索范围:底层蛋糕的最大可能半径和最大可能高度 搜索顺序:从底层往上搭蛋糕,在同一层尝试时,半径和高度都是从大到 ...
- UVA 10160 Servicing Stations(深搜 + 剪枝)
Problem D: Servicing stations A company offers personal computers for sale in N towns (3 <= N < ...
- ACM 海贼王之伟大航路(深搜剪枝)
"我是要成为海贼王的男人!" 路飞他们伟大航路行程的起点是罗格镇,终点是拉夫德鲁(那里藏匿着"唯一的大秘宝"--ONE PIECE).而航程中间,则是各式各样的 ...
- hdu 1518 Square(深搜+剪枝)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1518 题目大意:根据题目所给的几条边,来判断是否能构成正方形,一个很好的深搜应用,注意剪枝,以防超时! ...
- POJ-1724 深搜剪枝
这道题目如果数据很小的话.我们通过这个dfs就可以完成深搜: void dfs(int s) { if (s==N) { minLen=min(minLen,totalLen); return ; } ...
- 一本通例题-生日蛋糕——题解<超强深搜剪枝,从无限到有限>
题目传送 显然是道深搜题.由于蛋糕上表面在最底层的半径确认后就确认了,所以搜索时的面积着重看侧面积. 找维度/搜索面临状态/对象:当前体积v,当前外表面面积s,各层的半径r[],各层的高度h[]. 可 ...
随机推荐
- 从零开始学习Linux (cd命令)
上一篇博客中提到,我们学习命令大多都要参考 --help 这个选项.但是cd命令并没有这个选项. 我们可以通过 help cd 来查看cd的使用方式.其实cd命令挺简单的,它的作用是进入文件夹,也就是 ...
- DoTween小结
using UnityEngine; using System.Collections; using DG.Tweening; public class GetStart : MonoBehaviou ...
- 【Android】开源项目UI控件分类汇总之Dialog
接前文ProgressBar:Android开发的宝库越来越多,我开发中有需要的组件,主要参考Trinea的大作Android开源项目分类汇总(包含了后面的绝大多数).CSDN上直接拿来用!最火的An ...
- Exchange 2013 申请证书
最近在了解Exchange2013,Exchange2013相对于Lync安装相对容易一点,安装完成并不代表就可以用了,还要一些基本的配制,首先介绍一下如何从证书服务器申请 CA. 一.DNS 创建解 ...
- javascript函数中的三个技巧【三】
技巧三: [函数绑定] 在javascript与DOM交互中经常需要使用函数绑定,定义一个函数然后将其绑定到特定DOM元素或集合的某个事件触发程序上,绑定函数经常和回调函数及事件处理程序一起使用,以便 ...
- CSS 去掉inline-block元素间隙的几种方法
最近做移动端页面时,经常会用到inline-block元素来布局,但无可避免都会遇到一个问题,就是inline-block元素之间的间隙.这些间隙会导致一些布局上的问题,需要把间隙去掉.对于inlin ...
- ABAP中正则表达式的简单使用方法 (转老白BLOG)
在一个论坛上面看到有人在问正则表达式的问题,特举例简单说明一下.另外,REPLACE也支持REGEX关键字.最后:只能是ECC6或者更高版本才可以(ABAP supports POSIX regula ...
- 实验12:Problem C: 重载字符的加减法
Home Web Board ProblemSet Standing Status Statistics Problem C: 重载字符的加减法 Problem C: 重载字符的加减法 Time ...
- Android——微信界面(简易版)
前面我们简单的介绍了一下android的五大布局,那么现在我们来实践一下,写一个简单的微信界面 首先,我们新建一个weixin.xml的linnerlayout布局 我们日常使用的微信,从简单的方面来 ...
- iOS开发笔记9:NSUserDefaults存储自定义实体对象
NSUserDefaults常常用来本地存储一些简单的数据,例如用户信息等等,NSUserDefaults支持floats.doubles.integers.Booleans.URLs.NSData. ...