当前位置: 首页 > article >正文

糖果——差分约束 + 正环判定及其优化(手搓栈 + 标记法)

题目

思考

这里转为判定负环可以是可以,但是不能用超级源点了(改为把节点全部压入),因为按照题目条件,建立的应该是各个节点指向超级源点的有向边,这显然破坏了超级源点的功能

代码

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5+10, M = 3 * N;
int h[N], e[M], ne[M], idx, w[M];
int pre[N], col[N], cnt;
bool st[N];
int dist[N];
int q[N];
int n, k, hh, tt = -1;
ll ans;
void add(int a, int b, int c)  // 添加一条边a->b,边权为c
{
    e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;
}
bool dfs(int u)
{
    if(col[u]) return col[u] == 2;
    col[u] = 2;
    if(~pre[u] && dfs(pre[u])) return true;
    col[u] = 1;
    return false;
}
bool check()
{
    memset(col, 0, sizeof col);
    for(int i = 0; i <= n; i++)
        if(!col[i] && dfs(i)) return true;
    return false;
}
bool spfa()
{
    memset(pre, -1, sizeof pre);
    memset(dist, -0x3f, sizeof dist);
    q[++tt] = 0, dist[0] = 0, st[0] = 1;
    
    while(hh <= tt)
    {
        int u = q[tt--];
        st[u] = 0;
        
        for(int i = h[u]; ~i; i = ne[i])
        {
            int j = e[i];
            if(dist[j] < dist[u] + w[i])
            {
                dist[j] = dist[u] + w[i];
                pre[j] = u;
                if(++cnt > n) 
                {
                    cnt = 0;
                    if(check()) return false;
                }
                if(!st[j])
                    q[++tt] = j, st[j] = 1;
            }
        }
    }
    
    for(int i = 1; i <= n; i++)
        ans += dist[i];
    return true;
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    
    cin >> n >> k;
    memset(h, -1, sizeof h);
    for(int i = 1; i <= k; i++)
    {
        int x, a, b;
        cin >> x >> a >> b;
        if(x == 1) add(a, b, 0), add(b, a, 0);
        else if(x == 2) add(a, b, 1);
        else if(x == 3) add(b, a, 0);
        else if(x == 4) add(b, a, 1);
        else add(a, b, 0);
    }
    for(int i = 1; i <= n; i++)
        add(0, i, 1);
    
    if(!spfa()) cout << "-1";
    else cout << ans;
}


http://www.kler.cn/news/361921.html

相关文章:

  • Miniconda3 Linux安装教程
  • 【python openai function2json小工具】
  • SpringBoot实现的汽车票在线预订系统
  • vuex的store应用
  • golang正则表达式的使用及举例
  • 【C++】— 一篇文章让你认识STL
  • 搜维尔科技:Varjo XR-4 模拟驾驶
  • LeetCode 1750.删除字符串两端相同字符后的最短长度
  • Lattice_FPGA使用Synplify Pro进行综合
  • flv格式如何转换mp4?将flv转换成MP4格式的9种转换方法
  • 如何将 Elasticsearch 与流行的 Ruby 工具结合使用
  • Linux下进行用户的切换与创建以及细微设置
  • SIMPLOT: Enhancing Chart Question Answering by Distilling Essentials
  • LeetCode两数相加
  • ECharts饼图-饼图标签对齐,附视频讲解与代码下载
  • 情怀程序员,没有套路的坐下和大家掏心窝聊聊今年的1024 | 程序员节
  • 通过DevTools逃离Chrome沙盒(CVE-2024-6778和CVE-2024-5836)
  • 删除本地文件不影响Github
  • Centos7 安装部署Zookeeper
  • AMR机器人助力废料管理,实现生产空间最大化利用
  • 五年以上倾斜摄影osgb模型转3dtiles如何在mars3d加载
  • CSS 设置网页的背景图片
  • Spearman、Pearson、Euclidean、Cosine、Jaccard,用来衡量不同数据之间的相似性或差异性
  • 【Linux】从 fork() 到 exec():理解 Linux 进程程序替换的魔法
  • gbn,sr和tcp的区别
  • Oracle VM的网络中桥接网卡找不到网络