ROADS

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 11977 Accepted: 4429

Description

N cities named with numbers 1 … N are connected with one-way roads. Each road has two parameters associated with it : the road length and the toll that needs to be paid for the road (expressed in the number of coins).

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 first line of the input contains the integer K, 0 <= K <= 10000, maximum number of coins that Bob can spend on his way.

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

The first and the only line of the output should contain the total length of the shortest path from the city 1 to the city N whose total toll is less than or equal K coins.

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

Source

CEOI 1998

网上的很多题解是最短路,不过我感觉还是搜索好写,DFS+剪枝,很轻松就过来

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#define LL long long
using namespace std; const int MAX = 11000; const int INF = 0x3f3f3f3f; struct node
{
int v;
int len;
int w;
int next;
}a[MAX]; int Head[120]; bool vis[120]; int top; int n,m,k; int MM; void DFS(int s,int len,int mon)
{
if(s==n)
{
if(mon<=k&&len<MM)
{
MM=len;
}
return ;
}
if(mon>k)
{
return ;
}
if(len>MM)
{
return ;
}
for(int i=Head[s];i!=-1;i=a[i].next)
{
if(!vis[a[i].v])
{
vis[a[i].v]=true;
DFS(a[i].v,len+a[i].len,mon+a[i].w);
vis[a[i].v]=false;
}
}
} int main()
{
int s,d,l,t;
while(~scanf("%d",&k))
{
scanf("%d %d",&n,&m);
top=0;
memset(Head,-1,sizeof(Head));
for(int i=0;i<m;i++)
{
scanf("%d %d %d %d",&s,&d,&l,&t);
a[top].len=l;
a[top].w=t;
a[top].v=d;
a[top].next=Head[s];
Head[s]=top++;
}
memset(vis,false,sizeof(vis));
MM = INF;
vis[1]=true;
DFS(1,0,0);
if(MM==INF)
{
printf("-1\n");
}
else
{
printf("%d\n",MM);
}
} return 0;
}

ROADS的更多相关文章

  1. poj 1251 Jungle Roads (最小生成树)

    poj   1251  Jungle Roads  (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000 ...

  2. Jungle Roads[HDU1301]

    Jungle Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  3. POJ1947 Rebuilding Roads[树形背包]

    Rebuilding Roads Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 11495   Accepted: 5276 ...

  4. Constructing Roads——F

    F. Constructing Roads There are N villages, which are numbered from 1 to N, and you should build som ...

  5. Constructing Roads In JGShining's Kingdom(HDU1025)(LCS序列的变行)

    Constructing Roads In JGShining's Kingdom  HDU1025 题目主要理解要用LCS进行求解! 并且一般的求法会超时!!要用二分!!! 最后蛋疼的是输出格式的注 ...

  6. 【CodeForces 567E】President and Roads(最短路)

    Description Berland has n cities, the capital is located in city s, and the historic home town of th ...

  7. POJ 1947 Rebuilding Roads

    树形DP..... Rebuilding Roads Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 8188 Accepted: ...

  8. poj 1724:ROADS(DFS + 剪枝)

    ROADS Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10777   Accepted: 3961 Descriptio ...

  9. Codeforces Round #212 (Div. 2) D. Fools and Foolproof Roads 并查集+优先队列

    D. Fools and Foolproof Roads   You must have heard all about the Foolland on your Geography lessons. ...

  10. Codeforces Round #369 (Div. 2) D. Directed Roads dfs求某个联通块的在环上的点的数量

    D. Directed Roads   ZS the Coder and Chris the Baboon has explored Udayland for quite some time. The ...

随机推荐

  1. 从零开始攻略PHP(5)——字符串操作与POSIX正则

    一.字符串操作 1.字符串的格式化 1.1 干掉空格 trim()函数可以除去字符串开始位置和结束位置的空格,并将结果字符串返回. ltrim()函数可以除去字符串开始位置的空格. rtrim()函数 ...

  2. 前端bower使用

    Bower是一个客户端技术的软件包管理器,是由twitter推出的.它可用于搜索.安装和卸载如JavaScript.HTML.CSS之类的网络资源.其他一些建立在Bower基础之上的开发工具,如Yeo ...

  3. window.cookie

    本地测试cookie用火狐来测试 首先cookie是document上的一个属性. 先弹出一个cookie alert(document.cookie); //弹出是空的 设置cookie,格式是有一 ...

  4. execute、executeQuery和executeUpdate之间的区别

    JDBCTM中Statement接口提供的execute.executeQuery和executeUpdate之间的区别 Statement 接口提供了三种执行 SQL 语句的方法:executeQu ...

  5. <s:iterator> 对list操作的一种方法

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA6IAAAH3CAIAAAAuRnW9AAAgAElEQVR4nOzdf4gk1333+wLDDffhPp

  6. Eclipse下配置C++开发环境(转)

    1. 首先确保你的电脑上已经安装了Java,如果没有,或者不确定,请到官网上下载并安装,网址如下(这一步我就不详述了): http://www.java.com/zh_CN/   2. 到官网上下载并 ...

  7. c++之路进阶——hdu3507(Print Article)

    参考博文:http://www.cnblogs.com/ka200812/archive/2012/08/03/2621345.html//讲的真的很好,有个小错误,博客里的num全为sum,像我这种 ...

  8. 直关的sql 联级更新语句

    在sql-server中用这种写法最直观:UPDATE a SET a.c = b.c FROM table1 ainner join table2 b on b.a=a.aWHERE a.c is ...

  9. struts2中Double类型的转换

    今天做项目,ssh + Extjs,页面js中定义了几个NumberField,对应的value都是double类型的,其中有个NumberField的name为 name,结果执行的时候报错了,说找 ...

  10. PAT乙级 1024. 科学计数法 (20)

    1024. 科学计数法 (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 HOU, Qiming 科学计数法是科学家用来表示很 ...