zoukankan      html  css  js  c++  java
  • 【luogu P3275 [SCOI2011]糖果】 题解

    题目链接:https://www.luogu.org/problemnew/show/P3275
    把不等式 A > B 转化成 A - B >= 1或者 B - A <= -1再差分约束
    B - A 不能是 <= 1
    2333

    // luogu-judger-enable-o2
    #include <queue>
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    #define ll long long
    using namespace std;
    const int maxn = 100000 + 10;
    ll n, m, ans, visnum[maxn], dis[maxn];
    bool vis[maxn];
    struct edge{
    	ll from, to, next, len;
    }e[maxn<<2];
    ll head[maxn], cnt;
    queue<int> q;
    /*struct cmp{
    	bool operator ()(ll &x, ll &y)
    	{
    		return dis[x] < dis[y];
    	}
    };
    priority_queue<ll, vector<ll>, cmp> q;*/
    inline int read()
    {
        int k=0,f=1;
        char c=getchar();
        while(!isdigit(c))
        {
            if(c=='-')f=-1;
            c=getchar();
        }
        while(isdigit(c))
        {
            k=(k<<1)+(k<<3)+c-48;
            c=getchar();
        }
        return k*f;
    }
    inline void add(ll u, ll v, ll w)
    {
    	e[++cnt].len = w;
    	e[cnt].next = head[u];
    	e[cnt].to = v;
    	head[u] = cnt;
    }
    bool SPFA()
    {
    	while(!q.empty())
    	{
    		ll now = q.front();q.pop();
    		vis[now] = 0;
    		if(visnum[now] == n-1) return 1;
    		visnum[now]++;			
    		for(ll i = head[now]; i != -1; i = e[i].next)
    		{
    			if(dis[e[i].to] > dis[now] + e[i].len)
    			{
    				dis[e[i].to] = dis[now] + e[i].len;
    				if(!vis[e[i].to])
    				{
    					vis[e[i].to] = 1;
    					q.push(e[i].to);
    				}
    			}
    		}
    	}
    	return 0;
    }
    int main()
    {
    	memset(head, -1, sizeof(head));
    	//scanf("%lld%lld",&n,&m);
    	n = read(); m = read();
    	for(ll i = 1; i <= m; i++)
    	{
    		ll opt, u, v;
    		//scanf("%lld%lld%lld",&opt,&u,&v);
    		opt = read(); u = read(); v = read();
    		if(opt == 1)
    		{
    			add(u, v, 0);
    			add(v, u, 0);
    		}
    		if(opt == 2)
    		{
    			if(u == v)
    			{
    				printf("-1
    ");
    				return 0;
    			}
    			add(u, v, -1);
    		}
    		if(opt == 3)
    		{
    			add(v, u, 0);
    		}
    		if(opt == 4)
    		{
    			if(u == v)
    			{
    				printf("-1
    ");
    				return 0;
    			}
    			add(v, u, -1);
    		}
    		if(opt == 5)
    		{
    			add(u, v, 0);
    		}
    	}
    	for(ll i = n; i >= 1; i--) 
    	{
    		add(0, i, -1);
    		dis[i] = 0x7fffffff;
    	}
    	int s = 0;
    	q.push(s);
    	dis[s] = 0; vis[s] = 1;
    	if(SPFA())
    	{
    		printf("-1
    ");
    		return 0;
    	}
    	else
    	{
    		for(int i = 1; i <= n; i++)
    		ans += dis[i];
    		printf("%lld
    ",-ans);
    		return 0;
    	}
    }
    
  • 相关阅读:
    Git:常用命令记录
    JS笔记(二):隐式转换
    vertical-align/line-height:水平垂直居中
    JS笔记(一):声明提升
    Array.prototype.sort():从一道面试题说起
    CSS笔记(一):选择器规范
    FreeCodeCamp:Profile Lookup
    tile_images_offset的简单使用
    vs2013快捷键等(转)
    Qt状态栏的使用(转)
  • 原文地址:https://www.cnblogs.com/MisakaAzusa/p/9429194.html
Copyright © 2011-2022 走看看