题目链接:http://poj.org/problem?id=1724
Time Limit: 1000MS Memory Limit: 65536K
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
Problem solving report:
Description: 花的钱不超过k的情况下,从1到n的最短路径是多少。
Problem solving: 搜索,用邻接表建图, 深搜这个图一直更新步数的最小值。广搜利用优先队列,使其每次出队的都是路径的最小值。
Accepted Code:
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = 105;
const int MAXM = 10005;
const int inf = 0x3f3f3f3f;
bool vis[MAXN];
int f[MAXN], n, k, cnt, min_;
struct edge {
int s, t, w, v;
edge(int s_ = 0, int t_ = 0, int w_ = 0, int v_ = 0) : s(s_), t(t_), w(w_), v(v_) {}
}e[MAXM];
inline void Add(int s, int t, int w, int v) {
e[++cnt] = edge(f[s], t, w, v);
f[s] = cnt;
}
inline void init() {
cnt = 0;
memset(f, -1, sizeof(f));
memset(vis, false, sizeof(vis));
}
void DFS(int s, int w, int v) {
if (w >= min_)
return ;
if (s >= n) {
if (w < min_)
min_ = w;
return ;
}
for (int i = f[s]; ~i; i = e[i].s) {
int t = e[i].t;
if (!vis[t] && v + e[i].v <= k) {
vis[t] = true;
DFS(t, w + e[i].w, v + e[i].v);
vis[t] = false;
}
}
}
int main() {
init();
int m, s, t, w, v;
scanf("%d%d%d", &k, &n, &m);
for (int i = 0; i < m; i++) {
scanf("%d%d%d%d", &s, &t, &w, &v);
Add(s, t, w, v);
}
min_ = inf;
vis[1] = true;
DFS(1, 0, 0);
if (min_ < inf)
printf("%d\n", min_);
else printf("-1\n");
return 0;
}
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = 105;
const int MAXM = 10005;
int f[MAXN], cnt;
struct edga {
int s, t, w, v;
edga(int s_ = 0, int t_ = 0, int w_ = 0, int v_ = 0) : s(s_), t(t_), w(w_), v(v_) {}
}e[MAXM];
struct edge {
int x, w, v;
edge(int x_ = 0, int w_ = 0, int v_ = 0) : x(x_), w(w_), v(v_) {}
bool operator < (const edge &s) const {
if (s.w != w)
return s.w < w;
return s.v < v;
}
}p;
inline void Add(int s, int t, int w, int v) {
e[++cnt] = edga(f[s], t, w, v);
f[s] = cnt;
}
inline void init() {
cnt = 0;
memset(f, -1, sizeof(f));
}
void BFS(int s, int n, int val) {
priority_queue <edge> Q;
Q.push(edge(s, 0, 0));
while (!Q.empty()) {
p = Q.top();
Q.pop();
if (!(p.x != n)) {
printf("%d\n", p.w);
return ;
}
for (int i = f[p.x]; ~i; i = e[i].s) {
int t = e[i].t, w = e[i].w, v = e[i].v;
if (v + p.v <= val)
Q.push(edge(t, p.w + w, p.v + v));
}
}
printf("-1\n");
}
int main() {
init();
int n, m, s, t, w, v, k;
scanf("%d%d%d", &k, &n, &m);
for (int i = 0; i < m; i++) {
scanf("%d%d%d%d", &s, &t, &w, &v);
Add(s, t, w, v);
}
BFS(1, n, k);
return 0;
}