poj1724
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10804 | Accepted: 3976 |
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
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
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的更多相关文章
- poj-1724(bfs+优先队列)
题意:有向图,给你m条边,每条边有两个权值,路径长和通过这条路径的花费,问你在不超过k花费的前提下,最短的路径从1走到n 解题思路:因为边数很少,我们可以直接用暴力每条边的方式来找最小的路径长,也就是 ...
- poj1724【最短路】
题意: 给出n个城市,然后给出m条单向路,给出了每条路的距离和花费,问一个人有k coins,在不超过money的情况下从1到n最短路径路径. 思路: 我相信很多人在上面那道题的影响下,肯定会想想,在 ...
- poj1724 ROADS
题意: N个城市,编号1到N.城市间有R条单向道路.每条道路连接两个城市,有长度和过路费两个属性.Bob只有K块钱,他想从城市1走到城市N.问最短共需要走多长的路.如果到不了N,输出-12<=N ...
- POJ-1724 深搜剪枝
这道题目如果数据很小的话.我们通过这个dfs就可以完成深搜: void dfs(int s) { if (s==N) { minLen=min(minLen,totalLen); return ; } ...
- poj分类 很好很有层次感。
初期: 一.基本算法: (1)枚举. (poj1753,poj2965) (2)贪心(poj1328,poj2109,poj2586) (3)递归和分治法. ( ...
- 【转】POJ题目分类推荐 (很好很有层次感)
OJ上的一些水题(可用来练手和增加自信) (poj3299,poj2159,poj2739,poj1083,poj2262,poj1503,poj3006,poj2255,poj3094)初期: 一. ...
- 【转】ACM训练计划
[转] POJ推荐50题以及ACM训练方案 -- : 转载自 wade_wang 最终编辑 000lzl POJ 推荐50题 第一类 动态规划(至少6题, 和 必做) 和 (可贪心) (稍难) 第二类 ...
- POJ 题目分类(转载)
Log 2016-3-21 网上找的POJ分类,来源已经不清楚了.百度能百度到一大把.贴一份在博客上,鞭策自己刷题,不能偷懒!! 初期: 一.基本算法: (1)枚举. (poj1753,poj2965 ...
- ACM训练计划建议(写给本校acmer,欢迎围观和指正)
ACM训练计划建议 From:freecode# Date:2015/5/20 前言: 老师要我们整理一份训练计划给下一届的学弟学妹们,整理出来了,费了不少笔墨,就也将它放到博客园上供大家参考. 菜 ...
随机推荐
- iOS 推送证书制作 (JAVA/PHP)
// aps_development.cer 转化成pem openssl x509 -in aps_development.cer -inform der -out PushChatCert.pem ...
- java与.net比较学习系列(3) 基本数据类型和类型转换
在Java中,数据类型分为两类,一类是基本数据类型,另外一类是引用类型. 而在C#中,数据类型分为三类,分别是基元类型,值类型和引用类型.其中基元类型是.net framework框架中预定义的类型, ...
- [每日一题] OCP1z0-047 :2013-07-27 外部表――不能被DML和建索引
首先看官方文档上的解释: Managing External Tables Oracle Database allows you read-only access to data in externa ...
- HTML与CSS入门——第一章 理解Web的工作方式
知识点: 1.万维网的简史 2."网页"的含义,以及该术语不能反映所涉及的所有内容的原因 3.如何从你的个人计算机进入别人的浏览器 4.选择Web托管提供商的方法 5.不同的Web ...
- 关于asp:login控件和验证码的问题?(转)
1.验证码页面添加.2.将这验证码页面添加到login控件中:拖曳一Login控件,将之切换到模式下,在Html源文件中在表格中密码那行后添加: <tr> <td style= ...
- 无法安装或运行此应用程序。该应用程序要求首先在"全局程序集缓存(GAC)"中安装程序集
在做winform程序发布时遇到了这个问题,在我的机子上是可以正常运行的,但到别人的机子上就出现了这个错误.为此问题头疼了一上午终于搞定! 遇到这个问题一定是配置环境的原因, 1.你可以在程序 发布 ...
- 简化工作——我的bat文件
重启adb(radb.bat): @echo off call adb kill-server call adb start-server call adb remount push 一个apk(pu ...
- java序列化ClassNotFoundException
简单的想从保存的对象中重新解析出对象,用了逆序列化,可是报错: java.lang.ClassNotFoundException: xxxxxxxxxxxx at java.net.URLClassL ...
- 01 日志组件XLog
本文地址为:http://www.cnblogs.com/ADTL/p/5357259.html XLog为XCode的日志组件,为系统基本功能. 使用示例: 1.新建WinForm程序 2.引用Ne ...
- 整理SQL
由4张简单的不能再简单的表,演变出50道SQL 表结构: 表Student (S#,Sname,Sage,Ssex) 学生表 S# student_no Sage student_age Ss ...