zoukankan      html  css  js  c++  java
  • DFS(剪枝) POJ 1724 ROADS

    题目传送门

    题意:问从1到n的最短路径,同时满足花费总值小于等于k

    分析:深搜+剪枝,如果之前走过该点或者此时的路劲长度大于最小值就不进行搜索。

    /************************************************
    * Author        :Running_Time
    * Created Time  :2015/11/11 星期三 10:14:14
    * File Name     :POJ_1724.cpp
     ************************************************/
    
    //#include <bits/stdc++.h>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <queue>
    #include <cmath>
    using namespace std;
    
    #define lson l, mid, rt << 1
    #define rson mid + 1, r, rt << 1 | 1
    typedef long long ll;
    const int N = 1e2 + 10;
    const int E = 1e4 + 10;
    const int INF = 0x3f3f3f3f;
    const int MOD = 1e9 + 7;
    const double EPS = 1e-10;
    const double PI = acos (-1.0);
    int k, n, m;
    struct Edge {
        int v, w, len, nex;
        Edge () {}
        Edge (int v, int w, int len, int nex) : v (v), w (w), len (len), nex (nex) {}
    }edge[E];
    int head[N], d[N];
    bool vis[N];
    int e;
    
    void init(void) {
        memset (head, -1, sizeof (head));
        e = 0;
    }
    
    void add_edge(int u, int v, int w, int len) {
        edge[e] = Edge (v, w, len, head[u]);
        head[u] = e++;
    }
    
    int ans;
    void DFS(int u, int L, int cost)    {
        if (u == n) {
            if (L < ans)    ans = L;
            return ;
        }
        for (int i=head[u]; ~i; i=edge[i].nex)  {
            Edge &r = edge[i];
            if (cost - r.w < 0 || L + r.len > ans)  continue;
            if (vis[r.v])   continue;
            vis[r.v] = true;
            DFS (r.v, L + r.len, cost - r.w);
            vis[r.v] = false;
        }
    }
    
    int main(void)    {
        while (scanf ("%d%d%d", &k, &n, &m) == 3)   {
            init ();
            for (int u, v, w, len, i=1; i<=m; ++i)    {
                scanf ("%d%d%d%d", &u, &v, &len, &w);
                add_edge(u, v, w, len);
            }
            ans = INF;
            memset (vis, false, sizeof (vis));
            vis[1] = true;
            DFS (1, 0, k);
            if (ans == INF) ans = -1;
            printf ("%d
    ", ans);
        }
    
       //cout << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.
    ";
    
        return 0;
    }
    

      

    编译人生,运行世界!
  • 相关阅读:
    替换OSD操作的优化与分析
    Centos7下Jewel版本radosgw服务启动
    如何统计Ceph的RBD真实使用容量
    Ceph中的Copyset概念和使用方法
    Proftp最简匿名访问配置
    Windows could not set the offline local information.Error code:0X80000001解决方法
    《一百岁感言》 杨绛
    取扑克牌的问题
    马云的懒人理论
    明代地图总目
  • 原文地址:https://www.cnblogs.com/Running-Time/p/4955549.html
Copyright © 2011-2022 走看看