POJ - ROADS(搜索)

题目链接: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;
}
全部评论

相关推荐

rbjjj:太杂了吧,同学,项目似乎都没深度,都是api调度耶,分层架构思想没有体现出来了,前端没有前端优化前端工程化体现,后端微服务以及分层架构没体现以及数据安全也没体现,核心再改改,注重于计算机网络,工程化,底层原理吧
点赞 评论 收藏
分享
想干测开的tomca...:让我来压力你!!!: 这份简历看着“技术词堆得满”,实则是“虚胖没干货”,槽点一抓一大把: 1. **项目描述是“技术名词报菜名”,没半分自己的实际价值** 不管是IntelliDoc还是人人探店,全是堆Redis、Elasticsearch、RAG这些时髦词,但你到底干了啥?“基于Redis Bitmap管理分片”是你写了核心逻辑还是只调用了API?“QPS提升至1500”是你独立压测优化的,还是团队成果你蹭着写?全程没“我负责XX模块”“解决了XX具体问题”,纯把技术文档里的术语扒下来凑字数,看着像“知道名词但没实际动手”的实习生抄的。 2. **短项目塞满超纲技术点,可信度直接***** IntelliDoc就干了5个月,又是RAG又是大模型流式响应又是RBAC权限,这堆活儿正经团队分工干都得小半年,你一个后端开发5个月能吃透这么多?明显是把能想到的技术全往里面塞,生怕别人知道你实际只做了个文件上传——这种“技术堆砌式造假”,面试官一眼就能看出水分。 3. **技能栏是“模糊词混子集合”,没半点硬核度** “熟悉HashMap底层”“了解JVM内存模型”——“熟悉”是能手写扩容逻辑?“了解”是能排查GC问题?全是模棱两可的词,既没对应项目里的实践,也没体现深度,等于白写;项目里用了Elasticsearch的KNN检索,技能栏里提都没提具体掌握程度,明显是“用过但不懂”的硬凑。 4. **教育背景和自我评价全是“无效信息垃圾”** GPA前10%这么好的牌,只列“Java程序设计”这种基础课,分布式、微服务这些后端核心课提都不提,白瞎了专业优势;自我评价那堆“积极认真、细心负责”,是从招聘网站抄的模板吧?没有任何和项目挂钩的具体事例,比如“解决过XX bug”“优化过XX性能”,纯废话,看完等于没看。 总结:这简历是“技术名词缝合怪+自我感动式凑数”,看着像“背了后端技术栈名词的应届生”,实则没干货、没重点、没可信度——面试官扫30秒就会丢一边,因为连“你能干嘛”都没说清楚。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务