poj1724 ROADS

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

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

大意:有n个城市,m条路,初始时你有k个金币,每条路都有一个长度以及经过这条路的花费,问在你能够承受的花费之内,从1号城市到n号城市之间的最短距离是多少。

解法:我在网上看到了两种接法,一种是dfs递归追溯,另一种是优先队列,我的方法是第一种,不过很费时间,所以剪枝用的比较多。第二种方法,过几天在总结。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
struct Road{
	int d;
	int l;
	int t;
};
vector< vector <Road> > G(110);
int K,N,R;
int minLen;
int totalLen;
int totalCost;
int visited[110];
int minL[110][10010];
void dfs(int s){
	if(s==N){
		minLen=min(minLen,totalLen);
		return ;
	}
	for(int i=0;i<G[s].size();i++){//
		Road r=G[s][i];//
		if(!visited[r.d]){
			if(totalCost+r.t>K){//剪枝1 
				continue;
			}
			if(totalLen+r.l>=minLen){//剪枝2 
				continue;
			}
			if(totalLen+r.l>=minL[r.d][totalCost+r.t]){
				continue;//剪枝3 
			}
			minL[r.d][totalCost+r.t]=totalLen+r.l;
			totalLen+=r.l;
			totalCost+=r.t;
			visited[r.d]=1;
			dfs(r.d);
			visited[r.d]=0;
			totalLen-=r.l;
			totalCost-=r.t;
		}
	}
}
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){
			G[s].push_back(r);
		}
	} 
	memset(visited,0,sizeof(visited));
	minLen=(1<<30);
	totalLen=0;
	totalCost=0;
	for(int i=0;i<110;i++){
		for(int j=0;j<10010;j++){
			minL[i][j]=(1<<30);
		}
	}
	visited[1]=1;
	dfs(1);
	if(minLen<(1<<30)){
		cout<<minLen<<endl;
	}else{
		cout<<"-1"<<endl;//
	}
	return 0;
}

感悟:初碰这道题感觉还是比较难的,但是只要自己多看几遍视频,多敲几遍。功夫总是不会不负有心人的。

猜你喜欢

转载自blog.csdn.net/aiLMengi000/article/details/81806197