<差分约束>luogu 3275 糖果

本文介绍了一种解决特定图论问题的方法,通过构造有向图并利用SPFA算法来寻找满足条件的最短路径。针对不同类型的约束条件,文章详细解释了如何构建边及其权重,并最终求解出最小糖果数。

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

去题面的传送门
对于题目中的各种条件:
① a=b 建边a–>b=0,b–>a=0
②a>=b 建边 b–>a=0
③a>b 建边b–>a=1
为什么呢?
因为要求最少糖果数,那么对于a=b和a>=b的情况,就都让它们相差的最少,就是0,对于a>b,差的最少就是1,所以建边为1.
由于糖果数量最少的人的糖果至少也得有一个,所以建一个超级原点,到所有点的权值为1,然后跑最长路。因为对于一个学生的糖果数量,要尽可能满足所有人的要求。如果存在最长路,那么该同学的糖果数就能更满足其他较短路上的人的要求,所以最长路实际上求的是最小值。
不满足条件的情况,就是形成了正环。所以spfa判一下正环就好了
代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;

const long long maxn=200000+10;
long long n,k,cnt,ans;
long long fist[maxn],tot[maxn],nxt[maxn<<1],dis[maxn];
bool vis[maxn];
struct hh
{
    long long f,t,v;
}e[maxn<<1];
deque<long long>q;

void build(long long f,long long t,long long v)
{
    e[++cnt]=(hh){f,t,v};
    nxt[cnt]=fist[f];
    fist[f]=cnt;
}
bool spfa()
{
    vis[n+1]=true;
    dis[n+1]=0;
    tot[n+1]=1;
    q.push_back(n+1);
    while(!q.empty())
    {
        long long u=q.front();
        q.pop_front();
        vis[u]=false;
        for(long long i=fist[u];i!=-1;i=nxt[i])
        {
            long long v=e[i].t;
            if(dis[v]<dis[u]+e[i].v)
            {
                dis[v]=dis[u]+e[i].v;
                if(!vis[v])
                {
                    vis[v]=true;
                    if(q.empty()||dis[q.front()]>dis[v]) q.push_back(v);
                    else q.push_front(v);
                    tot[v]++;
                }
            }
            if(tot[v]>=n+3) return false;
        }
    }
    return true;
}
int main()
{
    memset(fist,-1,sizeof(fist));
    memset(dis,-63,sizeof(dis));
    scanf("%lld%lld",&n,&k);
    for(long long i=1;i<=k;++i)
    {
        long long x,a,b;
        scanf("%lld%lld%lld",&x,&a,&b);
        if(x==1) build(a,b,0),build(b,a,0);
        else if(x==2) build(a,b,1);
        else if(x==3) build(b,a,0);
        else if(x==4) build(b,a,1);
        else build(a,b,0);
    }
    for(long long i=1;i<=n;++i) build(n+1,i,1);
    if(!spfa()) printf("-1");
    else
    {
        for(long long i=1;i<=n;++i)
          ans+=dis[i];
        printf("%lld",ans);
    }
    return 0;
}
编译失败 /tmp/compiler_uamzogjd/src: 在函数‘void dfs(int, std::vector<std::pair<int, int> >&, ll, std::vector<long long int>&)’中: /tmp/compiler_uamzogjd/src:43:11: 警告:comparison of integer expressions of different signedness: ‘int’ and ‘std::vector<std::pair<int, int> >::size_type’ {aka ‘long unsigned int’} [-Wsign-compare] 43 | if (i == pf.size()) { | ~~^~~~~~~~~~~~ /tmp/compiler_uamzogjd/src:47:10: 警告:structured bindings only available with ‘-std=c++17’ or ‘-std=gnu++17’ 47 | auto [p, e] = pf[i]; | ^ /tmp/compiler_uamzogjd/src: 在函数‘int main()’中: /tmp/compiler_uamzogjd/src:66:20: 警告:structured bindings only available with ‘-std=c++17’ or ‘-std=gnu++17’ 66 | for (auto &[p, e] : pf) { | ^ /tmp/compiler_uamzogjd/src:73:19: 警告:structured bindings only available with ‘-std=c++17’ or ‘-std=gnu++17’ 73 | for (auto [p, a] : pf) { | ^ /tmp/compiler_uamzogjd/src:101:27: 警告:structured bindings only available with ‘-std=c++17’ or ‘-std=gnu++17’ 101 | for (auto [k, v] : contrib) { | ^ /tmp/compiler_uamzogjd/src:111:40: 错误:no matching function for call to ‘std::vector<std::map<int, int> >::push_back(std::vector<std::map<int, int> >&)’ 111 | s_options.push_back(options); | ^ In file included from /nix/store/bbmwawbq7wjb54fa35wr72alcm083d1f-luogu-gcc-9.3.0/include/c++/9.3.0/vector:67, from /nix/store/bbmwawbq7wjb54fa35wr72alcm083d1f-luogu-gcc-9.3.0/include/c++/9.3.0/queue:61, from /nix/store/bbmwawbq7wjb54fa35wr72alcm083d1f-luogu-gcc-9.3.0/include/c++/9.3.0/x86_64-unknown-linux-gnu/bits/stdc++.h:86, from /tmp/compiler_uamzogjd/src:1: /nix/store/bbmwawbq7wjb54fa35wr72alcm083d1f-luogu-gcc-9.3.0/include/c++/9.3.0/bits/stl_vector.h:1184:7: 附注:candidate: ‘void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = std::map<int, int>; _Alloc = std::allocator<std::map<int, int> >; std::vector<_Tp, _Alloc>::value_type = std::map<int, int>]’ 1184 | push_back(const value_type& __x) | ^~~~~~~~~ /nix/store/bbmwawbq7wjb54fa35wr72alcm083d1f-luogu-gcc-9.3.0/include/c++/9.3.0/bits/stl_vector.h:1184:35: 附注: no known conversion for argument 1 from ‘std::vector<std::map<int, int> >’ to ‘const value_type&’ {aka ‘const std::map<int, int>&’} 1184 | push_back(const value_type& __x) | ~~~~~~~~~~~~~~~~~~^~~ /nix/store/bbmwawbq7wjb54fa35wr72alcm083d1f-luogu-gcc-9.3.0/include/c++/9.3.0/bits/stl_vector.h:1200:7: 附注:candidate: ‘void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = std::map<int, int>; _Alloc = std::allocator<std::map<int, int> >; std::vector<_Tp, _Alloc>::value_type = std::map<int, int>]’ 1200 | push_back(value_type&& __x) | ^~~~~~~~~ /nix/store/bbmwawbq7wjb54fa35wr72alcm083d1f-luogu-gcc-9.3.0/include/c++/9.3.0/bits/stl_vector.h:1200:30: 附注: no known conversion for argument 1 from ‘std::vector<std::map<int, int> >’ to ‘std::vector<std::map<int, int> >::value_type&&’ {aka ‘std::map<int, int>&&’} 1200 | push_back(value_type&& __x) | ~~~~~~~~~~~~~^~~ /tmp/compiler_uamzogjd/src:138:24: 警告:structured bindings only available with ‘-std=c++17’ or ‘-std=gnu++17’ 138 | for (auto &[state, cnt] : dp) { | ^ /tmp/compiler_uamzogjd/src:142:39: 警告:comparison of integer expressions of different signedness: ‘int’ and ‘std::vector<int>::size_type’ {aka ‘long unsigned int’} [-Wsign-compare] 142 | for (int i = 0; i < primes.size(); i++) { | ~~^~~~~~~~~~~~~~~ /tmp/compiler_uamzogjd/src:144:48: 错误:no match for ‘operator[]’ (operand types are ‘std::pair<const int, int>’ and ‘int’) 144 | new_state[i] += contrib[p]; | ^ /tmp/compiler_uamzogjd/src:160:24: 警告:structured bindings only available with ‘-std=c++17’ or ‘-std=gnu++17’ 160 | for (auto &[state, cnt] : dp) { | ^ /tmp/compiler_uamzogjd/src:163:35: 警告:comparison of integer expressions of different signedness: ‘int’ and ‘std::vector<int>::size_type’ {aka ‘long unsigned int’} [-Wsign-compare] 163 | for (int i = 0; i < primes.size(); i++) { | ~~^~~~~~~~~~~~~~~ /tmp/compiler_uamzogjd/src:179:20: 警告:structured bindings only available with ‘-std=c++17’ or ‘-std=gnu++17’ 179 | for (auto &[k, v] : dp) ans = (ans + v) % MOD;
05-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值