ROADS
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 17419 | Accepted: 6214 |
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.
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 :
Notice that different roads may have the same source and destination cities.
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.
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
简介:这题解法比较多,我用了最简单易懂的dfs+剪枝,同时提高自己对最优性剪枝的理解。这里是剪枝的介绍:搜索剪枝的概述
#include<stdio.h>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<queue>
#include<set>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct road{//记录点的信息
int num,l,t;
};
vector<road>v[101];
int vis[101],minlen=1e9,minl[101][10000];
int k,n,r;
void dfs(int cur,int curl,int curt)//cur当前到达点,curl当前已走路长,curt当前花费
{
if(curl>minlen||curt>k)return;//剪枝1:还没走到n路长就超过minlen了就不要再浪费时间了,或者费用已经为负了也不考虑
if(curl>minl[cur][curt])return;
//剪枝2:到了某个以前走过的点,如果上次来的时候花费和本次一样,但是路更短,也不用再往后考虑了。但是也存在花费更少且录更短的可能,所以还能优化。
minl[cur][curt]=curl;//记录最短路长(当走到cur点,花费了curt时)
if(cur==n){//先剪枝再去看是否到达n点
minlen=min(minlen,curl);
return ;
}
vis[cur]=1;//标记访问过
for(int i=0;i<v[cur].size();i++)
if(!vis[v[cur][i].num])
dfs(v[cur][i].num,curl+v[cur][i].l,v[cur][i].t+curt);
vis[cur]=0;//回溯
}
int main()
{
for(int i=0;i<101;i++)
for(int j=0;j<10000;j++)
minl[i][j]=1e9;//初始化
cin>>k>>n>>r;
int s,e,l,t;
road d;
for(int i=0;i<r;i++){//这里点与点之前的信息比较多,所以用vector模拟邻接表
cin>>s>>d.num>>d.l>>d.t;
v[s].push_back(d);
}
dfs(1,0,0);
if(minlen!=1e9)
cout<<minlen<<endl;
else
cout<<"-1"<<endl;
return 0;
}