• 【洛谷P3275】糖果


    题目大意:维护 M 个差分约束关系,问是否可以满足所有约束,如果满足输出一组解。(N<=1e5)

    题解:差分约束模型可以通过构建一张有向图来求解。是否满足所有约束可以利用 spfa 进行判断,但是这道题数据范围是 1e5,spfa 很可能会被卡。重新考虑无解的情况,若满足最短路约束,则图中存在负环无解,最长路约束的情况则是存在正环无解。可以利用这一性质,对整个有向图进行 scc 缩点,若一个强连通分量中的边权和大于 0,则无解。缩点之后,在 DAG 上执行 dp 即可求得一组符合条件的解。

    代码如下

    #include <bits/stdc++.h>
    using namespace std;
    const int maxn=1e5+10;
    typedef long long ll;
    
    inline int read(){
    	int x=0,f=1;char ch;
    	do{ch=getchar();if(ch=='-')f=-1;}while(!isdigit(ch));
    	do{x=x*10+ch-'0';ch=getchar();}while(isdigit(ch));
    	return f*x;
    }
    
    struct node{
    	int nxt,to,w;
    }e[maxn<<1];
    int tot=1,head[maxn];
    inline void add_edge(int from,int to,int w){
    	e[++tot]=node{head[from],to,w},head[from]=tot;
    }
    
    int n,m;
    int dfs_clk,dfn[maxn],low[maxn],stk[maxn],top,in[maxn];
    vector<pair<int,int>> G[maxn];
    int scc,cor[maxn],size[maxn],indeg[maxn],dp[maxn];
    queue<int> q;
    
    void read_and_parse(){
    	n=read(),m=read();
    	for(int i=1,opt,x,y;i<=m;i++){
    		opt=read(),x=read(),y=read();
    		if(opt==1)add_edge(x,y,0),add_edge(y,x,0);
    		else if(opt==2)add_edge(x,y,1);
    		else if(opt==3)add_edge(y,x,0);
    		else if(opt==4)add_edge(y,x,1);
    		else if(opt==5)add_edge(x,y,0);
    	}
    	for(int i=1;i<=n;i++)add_edge(0,i,1);
    }
    
    void tarjan(int u){
    	dfn[u]=low[u]=++dfs_clk;
    	stk[++top]=u,in[u]=1;
    	for(int i=head[u];i;i=e[i].nxt){
    		int v=e[i].to;
    		if(!dfn[v])tarjan(v),low[u]=min(low[u],low[v]);
    		else if(in[v])low[u]=min(low[u],dfn[v]);
    	}
    	if(low[u]==dfn[u]){
    		++scc;int v;
    		do{
    			v=stk[top--],in[v]=0;
    			cor[v]=scc,++size[scc];
    		}while(v!=u);
    	}
    }
    
    void topo(){
    	for(int i=1;i<=scc;i++)if(!indeg[i])q.push(i);
    	while(q.size()){
    		int u=q.front();q.pop();
    		for(int i=0;i<G[u].size();i++){
    			int v=G[u][i].first,w=G[u][i].second;
    			dp[v]=max(dp[v],dp[u]+w);
    			if(!--indeg[v])q.push(v);
    		}
    	}
    }
    
    void solve(){
    	tarjan(0);
    	for(int u=0;u<=n;u++)
    		for(int i=head[u];i;i=e[i].nxt){
    			int v=e[i].to,w=e[i].w;
    			if(cor[v]==cor[u]){
    				if(w)return (void)puts("-1");
    				continue;
    			}
    			else G[cor[u]].push_back(make_pair(cor[v],w)),++indeg[cor[v]];
    		}
    	topo();
    	ll ans=0;
    	for(int i=1;i<=scc;i++)ans=ans+(ll)size[i]*dp[i];
    	printf("%lld
    ",ans);
    }
    
    int main(){
    	read_and_parse();
    	solve();
    	return 0;
    } 
    
  • 相关阅读:
    如何使Linux系统上的程序开机后自动运行 (转)
    Makefile详解 (转--不错就是有点长)
    ./configure && make && make install详解 (转)
    从程序员角度看ELF | Linux-Programming (转)
    动态符号链接的细节 (转)
    GCC编译动态和静态链接库例子
    gcc编译静态库和动态库
    udhcp源码详解(五) 之DHCP包--options字段
    一个炒鸡好用的pdf阅读器
    Linux 创建用户 用户组 用户权限
  • 原文地址:https://www.cnblogs.com/wzj-xhjbk/p/10604488.html
Copyright © 2020-2023  润新知