ROADS POJ - 1724 约束最短路 暴搜 加剪枝

Posted COLORFUL

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ROADS POJ - 1724 约束最短路 暴搜 加剪枝相关的知识,希望对你有一定的参考价值。

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;


}

 

以上是关于ROADS POJ - 1724 约束最短路 暴搜 加剪枝的主要内容,如果未能解决你的问题,请参考以下文章

POJ 1724 ROADS(使用邻接表和优先队列的BFS求解最短路问题)

最短路练习 poj1724

[poj1724]Roads

ROADS POJ - 1724(拆点)

ROADS POJ - 1724(拆点)

poj1724 ROADS