MOOC程序设计与算法练习题——深搜#2815Roads

描述
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 <vector>
#include <cstring>
using namespace std;
struct road{
	int d,l,t;
};
vector<vector<road> > G(110); //图的邻接表
int K,N,R;
int totalCost;//当前方案的总费用
int totalLen;//当前方案的总路径长度
int minLen = 1 << 30;//最短路径长度
int mid[110][10010];//用于剪纸的数组
int visited[110];

void DFS(int s){
	if(s == N){
		minLen = min(minLen,totalLen);
		return;
	}
	for(int i = 0;i < G[s].size(); ++i){
		int d = G[s][i].d;
		if(!visited[d]){
			int cost = totalCost + G[s][i].t;
			if(cost > K)
				continue;//可行性剪枝
			if(totalLen + G[s][i].l >= minLen || totalLen + G[s][i].l >= mid[d][cost])
				continue;//最优性剪枝
			totalCost += G[s][i].t;
			totalLen += G[s][i].l;
			mid[d][cost] = totalLen;
			visited[d] = 1;
			DFS(d);
			visited[d] = 0;//当前点还能被其他路径选中
			totalLen -= G[s][i].l;
			totalCost -= G[s][i].t;
		}
	} 
}

int main(){
	int i,j;
	cin >> K >> N >> R;
	for(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));
	totalCost = 0;
	totalLen = 0;
	for(i = 0;i < 110; ++i)
		for(j = 0;j < 10010; ++j)
			mid[i][j] = 1 << 30;
	visited[1] = 1;
	DFS(1);
	if(minLen < 1 << 30)
		cout << minLen << endl;
	else
		cout << '-1' << endl;
	return 0;
}
发布了53 篇原创文章 · 获赞 0 · 访问量 746

猜你喜欢

转载自blog.csdn.net/weixin_38894974/article/details/103837247