zoukankan      html  css  js  c++  java
  • ROADS POJ

    http://poj.org/problem?id=1724

    题意:最短路的模板,不过每条边加上一个费用,要求总费用不超过k

    题解:不能用dijkstra ,直接暴力,dfs维护len和cost。

        普通的剪枝:如果当前的cost大于k直接跳出,如果当前的len大于minlen(目前的最优解),跳出。

        另一个剪枝:维护花费一定费用 到达某个点 的最短路minL[v][cost],如果当前的len大于L,则跳出。

    ac代码:

    #define _CRT_SECURE_NO_WARNINGS
    #include<cstring>
    #include<cctype>
    #include<cstdlib>
    #include<cmath>
    #include<cstdio>
    #include<string>
    #include<stack>
    #include<ctime>
    #include<list>
    #include<set>
    #include<map>
    #include<queue>
    #include<vector>
    #include<sstream>
    #include<iostream>
    #include<functional>
    #include<algorithm>
    #include<memory.h>
    //#define INF 0x3f3f3f3f
    #define eps 1e-6
    #define pi acos(-1.0)
    #define e exp(1.0)
    #define rep(i,t,n)  for(int i =(t);i<=(n);++i)
    #define per(i,n,t)  for(int i =(n);i>=(t);--i)
    #define mp make_pair
    #define pb push_back
    #define mmm(a,b) memset(a,b,sizeof(a))
    //std::ios::sync_with_stdio(false);
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ull;
    void smain();
    #define ONLINE_JUDGE
    int main() {
        ios::sync_with_stdio(false);
    #ifndef ONLINE_JUDGE
        freopen("in.txt", "r", stdin);
        freopen("out.txt", "w", stdout);
        long _begin_time = clock();
    #endif
        smain();
    #ifndef ONLINE_JUDGE
        long _end_time = clock();
        printf("time = %ld ms.", _end_time - _begin_time);
    #endif
        return 0;
    }
    const int maxn = 1e4 + 100;
    const ll mod = 1e5 + 7;
    const ll INF = (100000)*(200000ll) + 1000;
    
    ll n, d;
    ll k, r, s,  l, t;
    int minlen;
    int cost, len;
    int vis[110];
    int minL[110][maxn];
    ll a[maxn];
    struct road {
        int d, L, t;
    };
    vector<vector<road> >citymap(110);
    //vector<int> a[maxn];
    void Run() {
        
    }
    void dfs(int s) {
        if (s == n) { minlen = min(minlen, len); return;}
        rep(i, 0, citymap[s].size() - 1) {
            road now = citymap[s][i];
            if (vis[now.d])continue;
            if (cost + now.t > k)continue;
        
            if (len + now.L >= minlen)continue;
            if (minL[now.d][cost+now.t] <= len + now.L)continue;
            len += now.L;
            cost += now.t;
            minL[now.d][cost] = len;
            vis[now.d] = 1;
            dfs(now.d);
            vis[now.d] = 0;
            len -= now.L;
            cost -= now.t;
    
    
    
        }
    
    }
    
    
    void smain() {
        cin >> k >> n >> r;
        rep(i, 1, r) {
            road R;
            int s;
            cin >> s >> R.d >> R.L >> R.t;
            if (s != R.d)citymap[s].push_back(R);
    
        }
        rep(i,0,105)
            rep(j, 0, maxn - 1) {
            minL[i][j] = 1 << 30;
        }
        memset(vis, 0, sizeof(vis));
        len = 0, cost = 0;
        vis[1] = 1;
        minlen = (1 << 30);
        dfs(1);
        minlen < 1 << 30 ? cout << minlen : cout << -1;
    
    
    }
    成功的路并不拥挤,因为大部分人都在颓(笑)
  • 相关阅读:
    C# 文件类的操作---删除
    C#实现Zip压缩解压实例
    UVALIVE 2431 Binary Stirling Numbers
    UVA 10570 meeting with aliens
    UVA 306 Cipher
    UVA 10994 Simple Addition
    UVA 696 How Many Knights
    UVA 10205 Stack 'em Up
    UVA 11125 Arrange Some Marbles
    UVA 10912 Simple Minded Hashing
  • 原文地址:https://www.cnblogs.com/SuuT/p/8973222.html
Copyright © 2011-2022 走看看