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. 《30天自制操作系统》11_day_学习笔记

    harib08a: 鼠标的显示问题:我们可以看到,鼠标移到窗口最右侧之后就不能再移动了,而WIN中,鼠标是可以移动到最右边隐藏起来的.怎么办?把鼠标指针显示的范围扩宽就行!我们来修改一下HariMai ...

  2. redhat vim编辑器永久添加行号

    cd ~ vim .vimrc 第一行加入: set nu :wq 保存退出,即可 如果想取消设置,同理删除set nu即可

  3. Groupon面经Prepare: Sort given a range && Summary: Bucket Sort

    首先是如何sort一个只有0和1的数组,要求inplace. follow up是告诉一个range,如何在O(N)时间内sort好 两个pointer可解 package Sorting; impo ...

  4. java-JDBC-Oracle数据库连接

    java-JDBC连接oracle数据库,StateMent和PreparedStatement对比(查询query) 1. PreparedStatement接口继承Statement, Prepa ...

  5. drop,delete,truncate区别

    drop,delete,truncate区别 drop-->删除少量信息   eg:drop table 表名: delete-->删除某些数据   eg:delete from 表名: ...

  6. jni

    http://www.cnblogs.com/likwo/archive/2012/05/21/2512400.html

  7. logstash的性能测试

    logstash有简单的批量生成插件.generator.详情见官网:https://www.elastic.co/guide/en/logstash/current/plugins-inputs-g ...

  8. 安装Elasticsearch,Logstash,Kibana(5.0.1-mac版)

    安装Elasticsearch 1.下载https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.0.1.tar.gz包 ...

  9. UINavigationController详解三(转)ToolBar

    原文出自:http://blog.csdn.net/totogo2010/article/details/7682641,特别感谢. 1.显示Toolbar  在RootViewController. ...

  10. COM编程概述

    所谓COM,英文为Componet Object Model,中文为组件对象模型(其实这种解释只有在考试卷上才具有一点实际意义). [1]为什么需要COM? COM是为了解决OLE问题而产生的.COM ...