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;

}

原文地址:https://www.cnblogs.com/SuuT/p/8973222.html

时间: 2024-10-18 06:44:34

ROADS POJ - 1724 约束最短路 暴搜 加剪枝的相关文章

hdu 4848 Wow! Such Conquering! (暴搜+强剪枝)

Wow! Such Conquering! Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 0    Accepted Submission(s): 0 Problem Description There are n Doge Planets in the Doge Space. The conqueror of Doge Space

【深搜加剪枝五】HDU 1010 Tempter of the Bone

Tempter of the BoneTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 64326    Accepted Submission(s): 17567 Problem Description The doggie found a bone in an ancient maze, which fascinated him a l

Hdu 4016 Magic Bitwise And Operation (暴搜 dfs)

题目大意: 在n个数中选取k个数,是他们的按位与最小. 思路分析: 开始往dp想,但是这道题是不满足子问题的. 当前的值最小,但是丢掉了和后面的1错开的最多的状态. 暴搜的剪枝: 1.与后面所有的树相与都比ans小,剪掉,因为越与越小. 2.先将所有的数排序,先取小的. 3.ans可以不断更新,不需要达到k的时候更新,原因和1相同. #include <cstdio> #include <iostream> #include <cstring> #include <

深搜+剪枝 POJ 1724 ROADS

POJ 1724 ROADS Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12766   Accepted: 4722 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

poj 1724 ROADS (bfs+优先队列)

题目链接 题意:在有费用k限制的条件下,求从1到n的最短距离,如果最短距离相同求费用最小的,边为有向边,其中可能有 多个相同的源点和目标点,但是距离和费用不同. 分析:用bfs和邻接表来把每一个边搜一下,因为用了优先队列,所以先到n的一定是最小的 . 1 #include <iostream> 2 #include <cstring> 3 #include <cstdlib> 4 #include <cmath> 5 #include <cstdio&

POJ 1724 ROADS

点击打开链接 ROADS Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10202   Accepted: 3786 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 t

poj The Clocks(暴搜)

mnesia在频繁操作数据的过程可能会报错:** WARNING ** Mnesia is overloaded: {dump_log, write_threshold},可以看出,mnesia应该是过载了.这个警告在mnesia dump操作会发生这个问题,表类型为disc_only_copies .disc_copies都可能会发生. 如何重现这个问题,例子的场景是多个进程同时在不断地mnesia:dirty_write/2 mnesia过载分析 1.抛出警告是在mnesia 增加dump

[POJ 1204]Word Puzzles(Trie树暴搜)

Description Word puzzles are usually simple and very entertaining for all ages. They are so entertaining that Pizza-Hut company started using table covers with word puzzles printed on them, possibly with the intent to minimise their client's percepti

poj 1724 ROADS 解题报告

题目链接:http://poj.org/problem?id=1724 题目意思:给出一个含有N个点(编号从1~N).R条边的有向图.Bob 有 K 那么多的金钱,需要找一条从顶点1到顶点N的路径(每条边需要一定的花费),前提是这个总花费  <= K. 首先这里很感谢 yunyouxi0 ,也就是我们的ACM队长啦~~~,他一下子指出了我的错误——存储重边的错误.这条题卑鄙的地方是,有重边,discuss 中的数据过了也不一定会AC啦.大家不妨试试这组数据(队长深情奉献^_^) 2 2 2 1