深搜+剪枝 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[]. 可 ...
随机推荐
- python之import机制
1. 标准 import Python 中所有加载到内存的模块都放在 sys.modules .当 import 一个模块时首先会在这个列表中查找是否已经加载了此模块,如果加载了则只是将 ...
- 基于FreeBSD 64位内核的kFreeBSD无法在Virtualbox下安装
ArchBSD同上 感谢大A(豆瓣)的投稿 :)
- idea使用maven搭建springmvc
最近学着搭建springmvc,写此博客记录一下 idea版本:2016.3.1maven: apache-maven-3.3.9tomcat:apache-tomcat-8.5.8 1.New Pr ...
- mybatis hellworld
用maven来进行搭建项目的~~ 1. 搭建环境 pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" x ...
- css导航栏
几个导航栏也算对学过知识的回顾,总有新的收获,下面是学习过程中敲的代码: <!DOCTYPE HTML> <html> <head> <title>&l ...
- 解决SQL Server 2008 64位系统无法导入Access/Excel的问题 2012/08/01
操作系统Windows Server 2008 X64,数据库SQL Server 2008 X64,Office 2007(好像只有32位),在存储过程执行OpenDatasource导入Acces ...
- javascript-this,call,apply,bind简述3
上节介绍了call()和apply()的用法,这节再讨论一下arguments参数和bind函数的用法以及函数柯里化就算是完结了. bind()函数 先看定义: bind()方法会创建一个函数的实例, ...
- 实现跨域的N种方法
从域说起 域: 域是WIN2K网络系统的安全性边界.我们知道一个计算机网最基本的单元就是"域",这一点不是WIN2K所独有的,但活动目录可以贯穿一个或多个域.在独立的计算机上,域即 ...
- SAP内存/ABAP内存/共享内存区别
(1).读取和使用方法不同SAP内存使用SET/GET parameters方法:SET PARAMETER ID 'MAT' field p_matnr.GET PARAMETER ID 'MAT' ...
- sublime text 3 安装
sublime text 3 下载地址 http://www.sublimetext.com/3 下载windows版本,然后解压缩就可以直接使用了,不错不错哦, 为了更加便捷的管理,安装 packa ...