描述

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.

输入

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.

输出

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.

样例输入

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

样例输出

11
#include <iostream>
#include <cstring>
#include <vector>
using namespace std; struct road{int d,L,t;
};int k,n,R;
vector< vector<road> > v(110);
int vis[110];
int minLen = 1 << 30;
int totleLen = 0;
int totleCost = 0;
int midL[110][10010];void dfs(int s){if(s == n){minLen = min(totleLen,minLen);return;}for(int i = 0;i<v[s].size();i++){int d = v[s][i].d;if(!vis[d]){int cost = totleCost + v[s][i].t;//可行性剪枝if(cost > k)continue;   //最优化剪枝 if((totleLen + v[s][i].L) >= minLen || (totleLen + v[s][i].L) >= midL[d][cost]) continue;midL[d][cost] = totleLen + v[s][i].L;totleLen += v[s][i].L;totleCost += v[s][i].t; vis[d] = 1;dfs(d); vis[d] = 0;totleLen -= v[s][i].L;totleCost -= v[s][i].t;}}
} int main(){cin >> k >> n >> R;int s;road r;for(int i = 0;i<R;i++){cin >> s >> r.d >> r.L >> r.t;if(s != r.d)v[s].push_back(r);}for(int i = 0;i<110;i++){for(int j = 0;j<10010;j++){midL[i][j] = (1 << 30);} }memset(vis,0,sizeof(vis));vis[1] = 1;dfs(1);if(minLen < (1<<30)) cout << minLen << endl;else cout << -1 << endl;return 0;}

百练1724ROADS相关推荐

  1. 百练,4103,踩方格

    百练,4103,踩方格 普通做法:(也可以找规律) #include #include//要调用memset函数,头文件 using namespace std; int visited[50][50 ...

  2. 北大OJ百练——4075:矩阵旋转(C语言)

    百练的这道题很简单,通过率也达到了86%,所以我也就来贴个代码了...下面是题目: 不过还是说一下我的思路: 这道题对一个新来说,可能是会和矩阵的转置相混淆,这题并不是要我们去求矩阵的转置. 这题,我 ...

  3. 威佐夫博弈:百练OJ:1067:取石子游戏

    威佐夫博弈(Wythoff's game):有两堆各若干个物品,两个人轮流从任一堆取至少一个或同时从两堆中取同样多的物品,规定每次至少取一个,多者不限,最后取光者得胜. 百练OJ:1067:取石子游戏 ...

  4. 百练OJ:4003:十六进制转十进制(python三行代码实现)

    题目链接:百练OJ:4003 描述 将十六进制数转换成十进制数 输入 第一行有一个整数T,表示共有T组数据 接下来T行,每一行为一个16进制无符号正整数,位数不超过8位,数中的a-f均为大写字母,数前 ...

  5. 百练OJ:2742:统计字符数

    题目链接: 百练OJ:2742:统计字符数 描述:判断一个由a-z这26个字符组成的字符串中哪个字符出现的次数最多 输入:第1行是测试数据的组数n,每组测试数据占1行,是一个由a-z这26个字符组成的 ...

  6. java函数实现进制转换与java实现八进制到十进制的转换(百练OJ:2735:八进制到十进制)

    java进制转换函数介绍:Java二进制.八进制.十进制.十六进制相互转换: 二进制转十进制 Integer.parseInt("0110", 2); 八进制转十进制 Intege ...

  7. 百练OJ:2973:Skew数

    题目链接: 百练OJ:2973:Skew数 描述:在 skew binary表示中, 第 k 位的值xk表示xk*(2k+1-1). 每个位上的可能数字是0 或 1,最后面一个非零位可以是2, 例如, ...

  8. 百练 Let it Bead

    百练 Let it Bead 总时间限制: 内存限制: 1000ms 65536kB 描述 "Let it Bead" company is located upstairs at ...

  9. 百练 求排列的逆序数

    百练 求排列的逆序数 总时间限制: 内存限制: 1000ms 65536kB 描述 在Internet上的搜索引擎经常需要对信息进行比较,比如可以通过某个人对一些事物的排名来估计他(或她)对各种不同信 ...

  10. 百练 06 股票买卖

    百练 06 股票买卖 总时间限制: 内存限制: 1000ms 65536kB 描述 最近越来越多的人都投身股市,阿福也有点心动了.谨记着"股市有风险,入市需谨慎",阿福决定先来研究 ...

最新文章

  1. Android使用BroadCastRecevier广播实现接收短信,并利用Toast弹出显示内容
  2. 16. 3Sum Closest
  3. MacBook 无法启动(白苹果+无限菊花)解决方案及心得
  4. Winbond W25QXX SPI Flash使用笔记
  5. 多线程处理list_跟大佬聊天,被反问Redis6的多线程真的能提高性能吗?
  6. 一文了解11个常见的多变量分析方法!
  7. 你的账户配置为阻止使用计算机,Win10打开软件提示为了对电脑进行保护,已经阻止此应用解决方法...
  8. 笔记本电脑无法连接WiFi,如何解决
  9. APP上架各大应用市场对比
  10. 炒鸡福利:买云服务送智能摄像头
  11. 安全防护与信息加密:一个新的挑战
  12. Dennard scaling(MOSEFT scaling)
  13. 最新WebStrom 2018.3 破解教程永久激活
  14. EasyExcel基本教程
  15. 高等代数 二次型与矩阵的合同(第6章)1 二次型,标准形,规范形
  16. Ubuntu上安装搜狗中文输入法
  17. VUE教程(持续更新中)
  18. 长尾关键词是什么意思? 1
  19. 结构方程模型与Mplus软件应用
  20. codeforces泛做

热门文章

  1. yar php使用,PHP yar的使用简介
  2. 内蒙古自治区高分二号卫星影像获取/高分一号卫星影像
  3. 透彻的掌握 Spring 中@transactional 的使用
  4. 格拉姆矩阵 Gram Matrix 简单理解
  5. RHEL6: Server panicked in 'redirfs' module
  6. Druid 统计监控页面无法打开
  7. ZigBee Dotdot
  8. 狐妖小红娘服务器维护,3月7游戏更新公告 狐妖小红娘版本上线
  9. Game boy模拟器(3):GPU的时序
  10. 小学和初中计算机的图案,信息技术和小学数学“图形和几何”的有效融合