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 前言: 老师要我们整理一份训练计划给下一届的学弟学妹们,整理出来了,费了不少笔墨,就也将它放到博客园上供大家参考. 菜 ...
随机推荐
- poj 3262 Protecting the Flowers 贪心
题意:给定n个奶牛,FJ把奶牛i从其位置送回牛棚并回到草坪要花费2*t[i]时间,同时留在草地上的奶牛j每分钟会消耗d[j]个草 求把所有奶牛送回牛棚内,所消耗草的最小值 思路:贪心,假设奶牛a和奶牛 ...
- python高级编程之描述符与属性03
# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' #属性Property #提供了一个内建描述符类型,它知道如何将一个 ...
- Hibernate(三)——框架中的关系映射
在设计数据库时我们会考虑,表与表之间的关系,例如我们前边经常提到的一对一,一对多,多对多关系,在数据库中我们通过外键,第三张表等来实现这些关系.而Hibernate时间实体类和数据库中的表进行的映射, ...
- web前端之 JS
JavaScript概述 JavaScript是一门编程语言,简称js,由浏览器编译并运行,JS说白了就是让页面能够动起来 js存在形式 1.在html页面中 <script> alert ...
- Monkey Tradition(中国剩余定理)
Monkey Tradition Time Limit: 2000MS Memory Limit: 32768KB 64bit IO Format: %lld & %llu Submi ...
- oracle查询优化
1. 排序合理查询条件 Oracle自下而上分析顺序WHERE收条,从优化性能的角度.它建议,这些条件将能够过滤掉大量行书写的WHERE在条款结束, 之间的连接条件置于其它WHERE子句之前,即对易排 ...
- 涂抹Oracle笔记2:数据库的连接-启动-关闭
一.数据库的连接sqlplus <username>[/<password>][@<connect_idertifier>]|/[as sysdba| as sys ...
- asp.net 内部重定向
1. /* * 2. * Context.RewritePath() * 使用给定路径重写 URL.(内部重写) * 内部请求重写 */ public static void TestTwo() { ...
- weblogic开机启动脚本
1.在/home/bea/startBeaAll目录内创建一个startBeaAll.sh文件,加入如下内容(把相应目录与命令修改即可,红字部分为修改地方): #!/bin/sh echo " ...
- IIS Hang Troubleshoot
Your website maybe stop working and response very lowly. How to find out the reason? Below are the g ...