ROADS
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10777 | Accepted: 3961 |
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.
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 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
Source
DFS + 剪枝
这道题用DFS就可以做,当然需要剪枝,否则会超时。
题 意
思 路
根据输入的有向图来进行DFS搜索,每次到达一个城市,都要计算新的经过的路径长度和剩下的路费(剪枝:1、如果当前经过的路径长度超过了之前记录的最小达到N的路径长度,那么直接返回上一层;2、如果剩下的路费<0了,则不能继续走,返回上一层),直到到达编号为N的城市,这个时候比较一下,确定当前的最小到达N的路径长度。
最后输出最小路径长度。
代 码
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std; #define MAXN 110 struct Node{
int city;
int len;
int toll;
Node* next;
}city[MAXN]; //邻接表 bool isv[MAXN]; int K,n,r,s;
int Min; void dfs(int c,int l,int k)
{
//k为剩下的钱
if(k<)
return ;
if(l>=Min) //如果当前走过的路的长度超过了记录的最小值,则退出
return ;
if(c==n && l<Min){ //到达n城市
Min = l;
return ;
} Node* p = city[c].next;
while(p){
if(!isv[p->city]){ //没走过
isv[p->city] = true;
dfs(p->city,l + p->len,k - p->toll); //进入下一个城市
isv[p->city] = false;
}
p = p->next;
}
} int main()
{
while(scanf("%d",&K)!=EOF){
scanf("%d",&n);
scanf("%d",&r); memset(city,NULL,sizeof(city));
memset(isv,,sizeof(isv));
Min = 0x7ffffff; while(r--){
scanf("%d",&s);
Node* p = (Node*)malloc(sizeof(Node));
scanf("%d%d%d",&p->city,&p->len,&p->toll);
p->next = city[s].next;
city[s].next = p;
} isv[] = true;
dfs(,,K); if(Min==0x7ffffff)
printf("-1\n");
else
printf("%d\n",Min);
}
return ;
}
Freecode : www.cnblogs.com/yym2013