POJ 3333 dfs / 最小费用流

题意

传送门 POJ 3333

题解
dfs

对于 t c [ i ] ≤ i tc[i]\leq i tc[i]i 的房间 i i i,在未访问过时选择 t r i c k trick trick 显然是最优的;反之,需要搜索 t r i c k trick trick n o r m a l normal normal 两种走法(如果数据不水复杂度 O ( 2 n ) O(2^n) O(2n) 要爆)。

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
using namespace std;
#define max(a, b) ((a) > (b) ? (a) : (b))
#define maxn 105
int n, res;
int d[maxn], td[maxn], tc[maxn];
bool vis[maxn];

void dfs(int v, int t)
{
    if (vis[v])
    {
        if (v == n)
        {
            res = max(res, t + d[v]);
            return;
        }
        else
        {
            dfs(v + 1, t + d[v]);
        }
    }
    else
    {
        vis[v] = 1;
        dfs(tc[v], t + td[v]);
        vis[v] = 0;
        if (tc[v] > v)
        {
            dfs(v + 1, t + d[v]);
        }
    }
}

int main()
{
    int t;
    scanf("%d", &t);
    while (t--)
    {
        scanf("%d", &n);
        if(n == 0){
            printf("0\n");
            continue;
        }
        for (int i = 1; i <= n; i++)
        {
            scanf("%d%d%d", d + i, td + i, tc + i);
        }
        memset(vis, 0, sizeof(vis));
        res = 0;
        dfs(1, 0);
        printf("%d\n", res);
    }
    return 0;
}
最小费用流

对于 t c [ i ] ≤ i tc[i]\leq i tc[i]i 的房间 i i i,在未访问过时选择 t r i c k trick trick 显然是最优的,预先将 t d [ i ] td[i] td[i] 计入答案;将房间的拓扑结构看作图,在去除满足 t c [ i ] ≤ i tc[i]\leq i tc[i]i 的房间 i i i t c [ i ] tc[i] tc[i] 的有向边后,变成了 D A G DAG DAG。此时问题转化为求 1 − > n 1->n 1>n 以及 t c [ i ] − > i ( t c [ i ] ≤ i ) tc[i]->i(tc[i]\leq i) tc[i]>i(tc[i]i) 的最大路径和;边权取负后则转化为最小费用流问题。

取源点 s s s,汇点 t t t,对于 1 − > n 1->n 1>n,从 s s s 1 1 1 连一条容量为 1 1 1 费用为 0 0 0 的边,从 n n n t t t 连一条容量为 1 1 1 费用为 − d [ n ] -d[n] d[n] 的边;对于 t c [ i ] − > i ( t c [ i ] ≤ i ) tc[i]->i(tc[i]\leq i) tc[i]>i(tc[i]i) 的,从 s s s t c [ i ] tc[i] tc[i] 连一条容量为 1 1 1 费用为 0 0 0 的边,从 i i i t t t 连一条容量为 1 1 1 费用为 0 0 0 的边;对于保留的 i < t c [ i ] i<tc[i] i<tc[i] t r i c k trick trick,从 i i i t c [ i ] tc[i] tc[i] 连一条容量为 1 1 1 费用为 − t d [ i ] -td[i] td[i] 的边;对于 n o r m a l normal normal 路径,从 i ( 1 ≤ i < n ) i(1\leq i<n) i(1i<n) i + 1 i+1 i+1 连一条容量为 i n f inf inf 费用为 − d [ i ] -d[i] d[i] 的边。

建图后求最小费用流,答案为预先计入的 ∑ t c [ i ] ≤ i t d [ i ] \sum_{tc[i]\leq i}td[i] tc[i]itd[i] 减最小费用流。复杂度 O ( n 2 l o g n ) O(n^2logn) O(n2logn),这才是正解。

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
using namespace std;
#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))
#define INF 0x3f3f3f3f

#define MAX_V 105
struct edge
{
    int to, cap, cost, rev;
    edge(int to, int cap, int cost, int rev) : to(to), cap(cap), cost(cost), rev(rev) {}
};

int V;
vector<edge> G[MAX_V];
int h[MAX_V], dist[MAX_V];
int prevv[MAX_V], preve[MAX_V];
bool inq[MAX_V];

void add_edge(int from, int to, int cap, int cost)
{
    G[from].push_back(edge(to, cap, cost, G[to].size()));
    G[to].push_back(edge(from, 0, -cost, G[from].size() - 1));
}

int min_cost_flow(int s, int t, int f)
{
    int res = 0;
    memset(h, 0, sizeof(h));
    // Spfa
    memset(inq, 0, sizeof(inq));
    memset(dist, 0x3f, sizeof(dist));
    queue<int> q;
    dist[s] = 0;
    q.push(s);
    inq[s] = 1;
    while (!q.empty())
    {
        int v = q.front();
        q.pop();
        inq[v] = 0;
        for (int i = 0; i < G[v].size(); i++)
        {
            edge &e = G[v][i];
            int d2 = dist[v] + e.cost + h[v] - h[e.to];
            if (e.cap > 0 && dist[e.to] > d2)
            {
                dist[e.to] = d2;
                if (!inq[e.to])
                {
                    q.push(e.to);
                    inq[e.to] = 1;
                }
            }
        }
    }
    for (int v = 0; v < V; v++)
        h[v] += dist[v];

    while (f > 0)
    {
        // Dijkstra
        priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> que;
        memset(dist, 0x3f, sizeof(dist));
        dist[s] = 0;
        que.push(pair<int, int>(0, s));
        while (!que.empty())
        {
            pair<int, int> p = que.top();
            que.pop();
            int v = p.second;
            if (dist[v] < p.first)
                continue;
            for (int i = 0; i < G[v].size(); i++)
            {
                edge &e = G[v][i];
                int d2 = dist[v] + e.cost + h[v] - h[e.to];
                if (e.cap > 0 && d2 < dist[e.to])
                {
                    dist[e.to] = d2;
                    prevv[e.to] = v;
                    preve[e.to] = i;
                    que.push(pair<int, int>(dist[e.to], e.to));
                }
            }
        }
        if (dist[t] == INF)
        {
            return -1;
        }
        for (int v = 0; v < V; v++)
            h[v] += dist[v];
        int d = f;
        for (int v = t; v != s; v = prevv[v])
        {
            d = min(d, G[prevv[v]][preve[v]].cap);
        }
        f -= d;
        res += d * h[t];
        for (int v = t; v != s; v = prevv[v])
        {
            edge &e = G[prevv[v]][preve[v]];
            e.cap -= d;
            G[v][e.rev].cap += d;
        }
    }
    return res;
}

void clear_graph()
{
    for (int v = 0; v < V; v++)
        G[v].clear();
}

#define maxn 105
int n;
int d[maxn], td[maxn], tc[maxn];

int main()
{
    int t;
    scanf("%d", &t);
    while (t--)
    {
        scanf("%d", &n);
        if (n == 0)
        {
            printf("0\n");
            continue;
        }
        int s = 0, t = n + 1, f = 1, res = 0;
        V = t + 1;
        clear_graph();
        add_edge(s, 1, 1, 0);
        for (int i = 1; i <= n; i++)
        {
            scanf("%d%d%d", d + i, td + i, tc + i);
            add_edge(i, i + 1, i == n ? 1 : INF, -d[i]);
            if (tc[i] <= i)
            {
                res += td[i], ++f;
                add_edge(s, tc[i], 1, 0);
                add_edge(i, t, 1, 0);
            }
            else
            {
                add_edge(i, tc[i], 1, -td[i]);
            }
        }
        printf("%d\n", res - min_cost_flow(s, t, f));
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值