【玄学问题】【玄大量关注】萌新刚学 C++ 求问关于 Ub 导致的各种玄学错误
  • 板块学术版
  • 楼主da_ke
  • 当前回复13
  • 已保存回复13
  • 发布时间2024/10/9 21:54
  • 上次更新2024/10/10 09:37:10
查看原帖
【玄学问题】【玄大量关注】萌新刚学 C++ 求问关于 Ub 导致的各种玄学错误
766675
da_ke楼主2024/10/9 21:54

题目:P9549。

这是一个高度玄学的问题:

#include <bits/stdc++.h>

#define i64 long long
#define rep(i,l,r) for(int i=(l);i<=(r);i++)
#define fdn(i,r,l) for(int i=(r);i>=(l);i--)
#define pii pair<int,int>
using namespace std;

typedef long long ll;
typedef double db;
typedef __int128 i128;

std::mt19937 rnd(std::chrono::steady_clock::now().time_since_epoch().count());
std::mt19937_64 rnd64(std::chrono::steady_clock::now().time_since_epoch().count());

const int N=101;
const ll INF=1ll<<59;

struct Node
{
    int first,second,third;
    ll fourth;
    bool operator < (const Node& A) const 
    {
        return fourth>A.fourth;
    }
    bool operator > (const Node& A) const 
    {
        return fourth<A.fourth;
    }
};

struct Edge
{
    int first;ll second,third;
};

int n,m,K,G;
ll dis[N][N][N];
bool vis[N][N][N];
vector<Edge> linker[N];
priority_queue<Node> qu;

void update(int u,int s,int t,ll w)
{
    if(dis[u][s][t]>w)
    {
        dis[u][s][t]=w;
        qu.push({u,s,t,w});
    }
}

signed main()
{
#ifndef ONLINE_JUDGE
    freopen("in.in","r",stdin);
    freopen("out.out","w",stdout);
#endif
    ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
    vector<int> x(n+1),y(n+1),z(n+1);
    cin>>n>>m>>K>>G;
    rep(i,1,n) cin>>x[i]>>y[i]>>z[i];
    rep(i,1,m)
    {
        int u,v;ll p,q;
        cin>>u>>v>>p>>q;
        linker[u].push_back({v,p,q});
        linker[v].push_back({u,p,q});
    }
    rep(i,1,n) rep(j,0,K) rep(k,0,G) dis[i][j][k]=INF;
    dis[1][0][0]=0;
    qu.push({1,0,0,0ll});
    while(!qu.empty())
    {
        int u=qu.top().first,s=qu.top().second,t=qu.top().third;
        ll l=qu.top().fourth%(x[u]+y[u]+z[u]),d=qu.top().fourth;
        qu.pop();
        for(auto i:linker[u])
        {
            int v=i.first,p=i.second,q=i.third;
            // 按红绿灯讨论
            if(l<x[u])
            {
                /*不限速*/ update(v,s,t,d+p);
                /*限速*/ if(s<K) update(v,s+1,t,d+q);
            }
            else if(l<(x[u]+y[u]))
            {
                // 不闯黄灯
                {
                    /*不限速*/ update(v,s,t,d+x[u]+y[u]+z[u]-l+p);
                    /*限速*/ if(s<K) update(v,s+1,t,d+x[u]+y[u]+z[u]-l+q);
                }
                // 闯黄灯
                if(t<G)
                {
                    update(v,s,t+1,d+p);
                    if(s<K) update(v,s+1,t+1,d+q);
                }
            }
            else
            {
                update(v,s,t,d+x[u]+y[u]+z[u]-l+p);
                if(s<K) update(v,s+1,t,d+x[u]+y[u]+z[u]-l+q);
            }
        }
    }
    ll ans=INF;
    rep(j,0,K) rep(k,0,G) ans=min(ans,dis[n][j][k]);
    cout<<ans<<endl;
}

问题焦点再第 60 行,如果写的是 ll,测试样例时会有问题:

RE,使用 AtCoder 自定义测试,Exit Code 139

Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)

使用洛谷 https://www.luogu.com.cn/record/181140177 RE Signal 6。

但如果开 inthttps://www.luogu.com.cn/record/181141070,但样例输出结果错误(先解决 RE 再解决 WA 吧)。

显然是 UB 导致的,因为我机子无法使用 UB 检测器。其实我写代码经常 UB ,所以我直接不用Windows了,用的是 AtCoder 的自定义测试。

2024/10/9 21:54
加载中...