poj 1190 ROADS
2016-07-20 11:03
405 查看
poj 1190 ROADS
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 12881 Accepted: 4756
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
56
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#include <iostream> #include <vector> #include <cstring> #include <algorithm> using namespace std; const int MAX_CITY = 100 + 5; const int MAX_ROADS = 10000 + 5; const int MAX_COST = 10000 + 5; const int INF = 1 << 30; // 总钱数,城市数,城市间路的条数 int K, N, R; // 路线 struct Road { // 到达位置,长度和花费 int D, L, T; }; // 邻接表 vector<vector<Road> > cityMap(MAX_CITY); // 最小路径长度 int minLen; // 当前路线上的长度 int totalLen; // 当前路线上的花费 int totalCost; // 城市访问数组,0未访问,1访问过 int visited[MAX_CITY]; // minL[i][j]表示从城市1到当前城市i,花费为j的最短路径长度 int minL[MAX_CITY][MAX_COST]; void dfs(int S) { // 递归出口 if(S == N) { minLen = min(minLen, totalLen); return; } for(int i = 0; i < cityMap[S].size(); i++) { // S到D有路 int D = cityMap[S][i].D; if(!visited[D]) { // 走这条路到该城市的总花费 int cost = totalCost + cityMap[S][i].T; // 最优性剪枝:当前花费已经大于拥有的钱了 if(cost > K) { continue; } // 走这条路到该城市的总长度 int len = totalLen + cityMap[S][i].L; // 最优性剪枝:当前长度已经大于等于最小路径长度或者 // 大于等于从城市1到该城市,花费为cost的最短路径长度 if(len >= minLen || len >= minL[D][cost]) { continue; } totalCost += cityMap[S][i].T; totalLen += cityMap[S][i].L; visited[D] = 1; minL[D][cost] = totalLen; dfs(D); // 回溯 visited[D] = 0; totalCost -= cityMap[S][i].T; totalLen -= cityMap[S][i].L; } } } int main() { cin >> K >> N >> R; for(int i = 0; i < R; i++) { int s; Road r; cin >> s >> r.D >> r.L >> r.T; if(s != r.D) { cityMap[s].push_back(r); } } for(int i = 0; i < MAX_CITY; i++) { for(int j = 0; j < MAX_COST; j++) { minL[i][j] = INF; } } memset(visited, 0, sizeof(visited)); totalCost = 0; totalLen = 0; minLen = INF; visited[1] = 1; dfs(1); if(minLen < INF) { cout << minLen << endl; } else { cout << -1 << endl; } return 0; }
相关文章推荐
- Win2003利用dfs(分布式文件系统)在负载均衡下的文件同步配置方案
- win2003分布式文件系统(dfs)配置方法[图文详解]
- win2003分布式文件系统及其部署 图文教程
- Hadoop2.6+jdk8的安装部署(1)——使用jar包安装部署【详细】
- Hadoop FS Shell
- DFS使用方法总结
- FastDFS注意事项
- 无忧技术带您预览DFS(分布式文件系统)管理控制台
- C 语言实现迷宫 DFS算法
- 初学ACM - 组合数学基础题目PKU 1833
- POJ ACM 1001
- POJ ACM 1002
- 1611:The Suspects
- POJ1089 区间合并
- 一幅图弄清DFT与DTFT,DFS的关系
- HDFS---Namenode
- HDFS ---- Services startup
- POJ 2159 Ancient Cipher
- POJ 2635 The Embarrassed Cryptographe
- POJ 3292 Semi-prime H-numbers