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 前言: 老师要我们整理一份训练计划给下一届的学弟学妹们,整理出来了,费了不少笔墨,就也将它放到博客园上供大家参考. 菜 ...
随机推荐
- OpenStack Mixture HypervisorsDriver configure and implementation theory
通过本文,您将可以了解在 OpenStack 中如何进行混合 Hypervisor 的配置及其实现原理的基本分析.本文主要结合作者在 Nova 中的实际开发经验对 OpenStack 中混合 Hype ...
- windows media player 中播放pls的方法
windows media player目前只能播放 wpl 和 asm格式的列表文件.而linux下mplayer和vlc支持的pls,很遗憾没法支持. 不过,老外写了个“open pls in w ...
- Qt之模型/视图(委托)
# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' from PyQt4.Qt import * from PyQt4. ...
- 打印log 保存log
using UnityEngine; using System.Collections; using System.IO; using System; using System.Text; names ...
- Dynamics CRM2013 missing prvReadComplexControl privilege
左右ComplexControl 权限设置,SDK例如,在以下的说明,仅供内部使用的实体,但是你可以没有找到这个叫配置安全角色ComplexControl的东西的. 在msdn上面查下就会发现这么一段 ...
- NET基础课-- 类型基础(NET之美)
1.类型:值类型 引用类型. 分类依据:类型在内存的分配方式.值类型在堆栈,引用类型在托管堆. 名词:栈--所有变量都会被分配在栈上,只不过值类型直接含有数据,引用类型含有一个指向托管堆对象的地址. ...
- javascript 模仿回车键事件
<script> $(function(){ var _login = function (){ var _name = $('#name'); var _password = $('#p ...
- confirm的用法 一般用于按钮操作时确定是否执行
<script type = "text/javascript" language = "javascript"> function clear1( ...
- Sql server 数据库 单用户切换为多用户
使用master 下的sysprocesses 查询 db正在使用的spid 如 select spid from sysprocesseswhere dbid=DB_ID('DbName') 然后执 ...
- Comparator TreeSet
package study; import java.util.Comparator;import java.util.TreeSet; public class TreeSetTest { publ ...