• AcWing1169 糖果(差分约数)


    转化成不等式后转化为最长路问题,另外多连0到他们的边后求一下最长路并判断是否有正环

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const int N=1e5+10,M=3e5+10;
    int h[N],e[M],w[M],ne[M],idx;
    ll dis[N];
    int cnt[N];
    int st[N];
    int n,k;
    void add(int a,int b,int c){
        e[idx]=b,ne[idx]=h[a],w[idx]=c,h[a]=idx++;
    }
    bool spfa(){
        stack<int> q;
        st[0]=1;
        q.push(0);
        memset(dis,-0x3f,sizeof dis);
        dis[0]=0;
        while(q.size()){
            int t=q.top();
            q.pop();
            int i;
            st[t]=0;
            for(i=h[t];i!=-1;i=ne[i]){
                int j=e[i];
                if(dis[j]<dis[t]+w[i]){
                    dis[j]=dis[t]+w[i];
                    cnt[j]++;
                    if(cnt[j]>=n+1)
                    return false;
                    if(!st[j]){
                        q.push(j);
                        st[j]=1;
                    }
                }
            }
        }
        return true;
    }
    int main(){
        cin>>n>>k;
        int i;
        memset(h,-1,sizeof h);
        for(i=1;i<=k;i++){
            int a,b,x;
            scanf("%d%d%d",&x,&a,&b);
            if (x == 1) add(b, a, 0), add(a, b, 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<<endl;
        }
        else{
            ll ans=0;
            for(i=1;i<=n;i++){
              //  cout<<dis[i]<<endl;
                ans+=dis[i];
            }
            
            cout<<ans<<endl;
        }
        
    }
    View Code
  • 相关阅读:
    UISearchBar clearButton
    github上不了改下host
    github命令
    quick-lua调试
    UIButton Making the hit area larger
    linux中crontab实现以秒执行任务
    学习linux必备服务器VPS
    JAVA线程全局异常处理
    spring基础
    <s:select>自动加标签
  • 原文地址:https://www.cnblogs.com/ctyakwf/p/12867934.html
Copyright © 2020-2023  润新知