ROADS
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10804   Accepted: 3976

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

 
 //邻接表存储结构+剪枝,优化时间复杂度
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std; const int MAXWAY = ;
const int MAXCITY = ;
const int INF = 0xffffff0;
struct Node{
int s,d,l,t;
int next; //邻接表的表头指针
}Node[MAXWAY];
int k,n,r;
int totallen; //存储当前路径中的路径长度
int totalcost; //存储当前路径中需要的话费
int minLen; //存储当前情况下最短的路径长度
int visited[]; //存储是否访问过某个城市 int head[MAXWAY]; //存储链表信息 void DFS(int i)
{
if(i==n)
{
minLen = min(minLen,totallen);
return ;
}
else
{
for(int j=head[i];j!=-;j=Node[j].next) //遍历 以i为起点的其他的所有
{
if(!visited[Node[j].d])
{
if(totalcost+Node[j].t>k) //如果加上当前的这条路的费用超过了k,则跳过
continue;
if(totallen+Node[j].l>minLen) //如果在费用没有超过的情况下加上该条路的长度,超过了当前的最短长度,则跳过
continue;
totallen = totallen + Node[j].l;
totalcost = totalcost + Node[j].t;
visited[Node[j].d] = ;
DFS(Node[j].d);
visited[Node[j].d] = ;
totallen -= Node[j].l;
totalcost -= Node[j].t; }
}
}
} int main()
{
while(scanf("%d%d%d",&k,&n,&r)!=EOF)
{
memset(head,-,sizeof(head));
memset(visited,,sizeof(visited));
for(int i=;i<r;i++) //接受数据同时,利用头插法建立邻接表
{
scanf("%d%d%d%d",&Node[i].s,&Node[i].d,&Node[i].l,&Node[i].t);
Node[i].next = head[Node[i].s]; //如果当前不存在元素,则此时的Node[i]就是尾节点,它的next是-1
//否则,Node[i].next就是指向当前的链表最头部的那个Node元素。
head[Node[i].s] = i; //修改当前的head[Node[i].s]的指向,使head[i]的值始终指向该条链表的头部元素,以便执行头插法
}
totallen = ;
totalcost = ;
minLen = INF;
DFS();
if(minLen<INF)
printf("%d\n",minLen);
else
printf("-1\n");
}
return ;
}

poj1724的更多相关文章

  1. poj-1724(bfs+优先队列)

    题意:有向图,给你m条边,每条边有两个权值,路径长和通过这条路径的花费,问你在不超过k花费的前提下,最短的路径从1走到n 解题思路:因为边数很少,我们可以直接用暴力每条边的方式来找最小的路径长,也就是 ...

  2. poj1724【最短路】

    题意: 给出n个城市,然后给出m条单向路,给出了每条路的距离和花费,问一个人有k coins,在不超过money的情况下从1到n最短路径路径. 思路: 我相信很多人在上面那道题的影响下,肯定会想想,在 ...

  3. poj1724 ROADS

    题意: N个城市,编号1到N.城市间有R条单向道路.每条道路连接两个城市,有长度和过路费两个属性.Bob只有K块钱,他想从城市1走到城市N.问最短共需要走多长的路.如果到不了N,输出-12<=N ...

  4. POJ-1724 深搜剪枝

    这道题目如果数据很小的话.我们通过这个dfs就可以完成深搜: void dfs(int s) { if (s==N) { minLen=min(minLen,totalLen); return ; } ...

  5. poj分类 很好很有层次感。

    初期: 一.基本算法:      (1)枚举. (poj1753,poj2965)      (2)贪心(poj1328,poj2109,poj2586)      (3)递归和分治法.      ( ...

  6. 【转】POJ题目分类推荐 (很好很有层次感)

    OJ上的一些水题(可用来练手和增加自信) (poj3299,poj2159,poj2739,poj1083,poj2262,poj1503,poj3006,poj2255,poj3094)初期: 一. ...

  7. 【转】ACM训练计划

    [转] POJ推荐50题以及ACM训练方案 -- : 转载自 wade_wang 最终编辑 000lzl POJ 推荐50题 第一类 动态规划(至少6题, 和 必做) 和 (可贪心) (稍难) 第二类 ...

  8. POJ 题目分类(转载)

    Log 2016-3-21 网上找的POJ分类,来源已经不清楚了.百度能百度到一大把.贴一份在博客上,鞭策自己刷题,不能偷懒!! 初期: 一.基本算法: (1)枚举. (poj1753,poj2965 ...

  9. ACM训练计划建议(写给本校acmer,欢迎围观和指正)

    ACM训练计划建议 From:freecode#  Date:2015/5/20 前言: 老师要我们整理一份训练计划给下一届的学弟学妹们,整理出来了,费了不少笔墨,就也将它放到博客园上供大家参考. 菜 ...

随机推荐

  1. Linux用户管理(笔记)

    用户:UID, /etc/passwd组:GID, /etc/group 影子口令:用户:/etc/shadow组:/etc/gshadow 用户类别:管理员:0普通用户: 1-65535    系统 ...

  2. WPF动画

    System.Windows.Media.Animation 这个命名空间中,包含了三种动画类:线性插值动画类(17个).关键帧动画(22个).路径动画(3个). 线性插值动画类(17个)如下: By ...

  3. php 依据字符串生成相应数组方法

    php 依据字符串生成相应数组方法 比如: <?php $config = array( 'project|page|index' => 'content', 'project|page| ...

  4. Android系统进程间通信Binder机制在应用程序框架层的Java接口源代码分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6642463 在前面几篇文章中,我们详细介绍了A ...

  5. kaggle之数字序列预测

    数字序列预测 Github地址 Kaggle地址 # -*- coding: UTF-8 -*- %matplotlib inline import pandas as pd import strin ...

  6. javascript无缝流畅动画轮播,终于让我给搞出来了。

    自己一直想写一个真正能用的轮播图,以前是写过一个,但是不是无缝的轮播,感觉体验很差,这个轮播之前也搞了很多实例,看了很多代码,但是脑子总转不过弯,为什么在运动到一定距离后可以突然转回到原始位置,而没有 ...

  7. Android-----输入法的显示和隐藏

    /** * 控制手机虚拟键盘的显示和隐藏 */public class InputMethodUtil { /** * 隐藏虚拟键盘 * @param v  参数v为获取焦点对象view */ pub ...

  8. PHP学习笔记三十七【http】

    <?php print_r($_SERVER); //$_SERVER预编译变量[数组]输出请求报文,注意大小写 echo "<br/>"; foreach($_ ...

  9. windows xp通过VNC viewer远程连接RHEL5桌面

    环境: [root@localhost ~]# cat /etc/issue Red Hat Enterprise Linux Server release 5.2 (Tikanga) Kernel ...

  10. jwplayer去Logo、自定义公司信息、限制拖动

    function initplayer(){        jwplayer("mediaplayer").setup({            primary: "fl ...