牛客寒假算法基础集训营3 - B 处女座的比赛资格 拓扑排序+记忆化搜索

本文探讨了在有向无环图(DAG)中,如何利用拓扑排序和记忆化搜索解决带有负权边的最小费用流问题。通过实例讲解了如何在存在三种边权的情况下,找到最优路径,特别关注于处女座角色的经费消耗情况分析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接

题意:一个有向无环图( D A G DAG DAG 图),每条边有三种权重,经费负责人根据其中两种权重,按最小花费给处女座经费,处女座拿着经费按两种权重,走最小花费的路径,问处女座的经费的消耗情况

思路:由于存在负权边,所以没法用 d j dj dj 来求最短路,但是由于是 D A G DAG DAG 图,所以可以按拓扑序来求解最小花费,然后判断两种花费情况即可。

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<map>
#include<vector>
#include<queue>
#include<deque>
#include<climits>
using namespace std;
#define ll long long
#define PI acos(-1)
#define INF 0x3f3f3f3f
#define NUM 100005
#define debug true
#define lowbit(x) ((-x)&x)
#define ffor(i,d,u) for(int i=(d);i<=(u);++i)
#define _ffor(i,u,d) for(int i=(u);i>=(d);--i)
#define mst(array,Num,Kind,Count) memset(array,Num,sizeof(Kind)*(Count))
const ll mod = 9999973;
template <typename T>
void read(T& x)
{
    x=0;
    char c;T t=1;
    while(((c=getchar())<'0'||c>'9')&&c!='-');
    if(c=='-'){t=-1;c=getchar();}
    do(x*=10)+=(c-'0');while((c=getchar())>='0'&&c<='9');
    x*=t;
}
template <typename T>
void write(T x)
{
    int len=0;char c[21];
    if(x<0)putchar('-'),x*=(-1);
    do{++len;c[len]=(x%10)+'0';}while(x/=10);
    _ffor(i,len,1)putchar(c[i]);
}
namespace Solve
{
int T, n, m;
int head[NUM], edgenum;
struct edge
{
    int to, next;
    ll w1, w2;
} e[NUM << 1];
ll dist1[NUM], dist2[NUM];
int du[NUM];
bool flag[NUM];
inline void BFS()//按拓扑序记忆化搜索
{
    int x, y;
    queue<int> q;
    q.push(1);
    dist1[1] = dist2[1] = 0;
    while (!q.empty())
    {
        x = q.front(), q.pop();
        for (int i = head[x]; i != 0; i = e[i].next)
        {
            y = e[i].to;
            --du[y];
            dist1[y] = min(dist1[y], dist1[x] + e[i].w1);
            dist2[y] = min(dist2[y], dist2[x] + e[i].w2);
            if (!du[y])
            {
                if (y == n)
                    return;
                q.push(y);
            }
        }
    }
}
inline void ToPo()//求从1为起点的所有路径上点的拓扑序
{
    int x, y;
    queue<int> q;
    q.push(1);
    flag[1] = true;
    while (!q.empty())
    {
        x = q.front(), q.pop();
        for (int i = head[x]; i != 0; i = e[i].next)
        {
            y = e[i].to;
            if (flag[y] == false)
                flag[y] = true, q.push(y);
        }
    }
    ffor(i, 1, n)
    {
        if (!flag[i])
            continue;
        for (int j = head[i]; j != 0; j = e[j].next)
        {
            if (!flag[e[j].to])
                continue;
            ++du[e[j].to];
        }
    }
}
inline void AC()
{
    ll x, y, z;
    read(T);
    while(T--)
    {
        mst(flag, false, bool, n + 1), mst(du, 0, int, n + 1), mst(head, 0, int, n + 1);
        read(n), read(m);
        edgenum = 0;
        ffor(i, 1, n)
            dist1[i] = dist2[i] = 99999999999999999;
        ffor(i, 1, m)
        {
            read(x), read(y);
            e[++edgenum].to = y, e[edgenum].next = head[x], head[x] = edgenum;
            read(z), read(x), read(y);
            e[edgenum].w1 = z - x, e[edgenum].w2 = z - y;
        }
        ToPo();
        BFS();
        if (dist1[n] < 0)
            dist1[n] = 0;
        if (dist2[n] < 0)
            dist2[n] = 0;
        if (dist1[n] > dist2[n])
        {
            puts("rip!!!");
            write(dist1[n] - dist2[n]);
            putchar('\n');
        }
        else
        {
            if (dist1[n] == dist2[n])
            {
                puts("oof!!!");
            }
            else
            {
                puts("cnznb!!!");
                write(dist2[n] - dist1[n]);
                putchar('\n');
            }
        }
    }
}
}
int main()
{
    Solve::AC();
    return 0;
}
### 关于2020牛客寒假算法基础集训营中的欧几里得算法2020年的牛客寒假算法基础集训营中,确实存在涉及欧几里得算法的相关题目。具体来说,在第四场竞赛的第一题即为“A. 欧几里得”,该题目的核心在于利用扩展欧几里得定理来解决问题[^5]。 #### 扩展欧几里得算法简介 扩展欧几里得算法主要用于求解形如 ax + by = gcd(a, b) 的线性不定方程的一组特解(x,y),其中gcd表示最大公约数。此方法不仅能够计算两个整数的最大公因数,还能找到满足上述条件的具体系数x和y。 对于给定的数据范围较小的情况可以直接通过递归来实现;而对于较大数据则需考虑效率优化问题。下面给出了一段基于C++语言编写的用于解决此类问题的模板代码: ```cpp #include<bits/stdc++.h> #define int long long using namespace std; // 定义全局变量存储结果 int x, y; void ex_gcd(int a, int b){ if(b == 0){ x = 1; y = 0; return ; } ex_gcd(b, a % b); int tmp = x; x = y; y = tmp - (a / b) * y; } ``` 这段程序实现了经典的扩展欧几里得算法逻辑,并且可以作为处理类似问题的基础工具函数调用。 #### 实际应用案例分析 回到原题本身,“A. 欧几里得”的解答思路就是先预处理斐波那契数列前若干项数值存入数组`a[]`内以便快速查询,之后针对每一次询问直接输出对应位置处两相邻元素之和即可得出最终答案。这实际上巧妙运用到了广为人知的裴蜀定理——任意一对互质正整数都可由它们自身的倍数组合而成,而这里正是借助了这一性质简化了解决方案的设计过程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值