zoukankan      html  css  js  c++  java
  • 洛谷1993 小K的农场

    原题链接

    裸的差分约束。

    1. (X_a-X_bgeqslant C)
    2. (X_a-X_bleqslant CRightarrow X_b-X_ageqslant -C)
    3. (X_a-X_bgeqslant 0,X_b-X_ageqslant 0)

    并建立一个超级源点(0),对每个点连一条权值为(0)的边,然后跑(SPFA)判断是否有正环即可。
    注意该题数据较强,基于(BFS)(SPFA)难以跑过,需用基于(DFS)的版本。

    #include<cstdio>
    #include<cstring>
    using namespace std;
    const int N = 1e4 + 10;
    const int M = 3e4 + 10;
    int fi[N], di[M], ne[M], da[M], dis[N], l;
    bool v[N];
    inline int re()
    {
    	int x = 0;
    	char c = getchar();
    	bool p = 0;
    	for (; c < '0' || c > '9'; c = getchar())
    		p |= c == '-';
    	for (; c >= '0' && c <= '9'; c = getchar())
    		x = x * 10 + c - '0';
    	return p ? -x : x;
    }
    inline void add(int x, int y, int z)
    {
    	di[++l] = y;
    	da[l] = z;
    	ne[l] = fi[x];
    	fi[x] = l;
    }
    bool dfs(int x)
    {
    	int i, y;
    	v[x] = 1;
    	for (i = fi[x]; i; i = ne[i])
    		if (dis[y = di[i]] < dis[x] + da[i])
    		{
    			dis[y] = dis[x] + da[i];
    			if (v[y])
    				return false;
    			if (!dfs(y))
    				return false;
    		}
    	v[x] = 0;
    	return true;
    }
    int main()
    {
    	int i, n, m, x, y, p, z;
    	n = re();
    	m = re();
    	for (i = 1; i <= m; i++)
    	{
    		p = re();
    		x = re();
    		y = re();
    		if (!(p ^ 3))
    		{
    			add(y, x, 0);
    			add(x, y, 0);
    		}
    		else
    		{
    			z = re();
    			if (!(p ^ 2))
    				add(x, y, -z);
    			else
    				add(y, x, z);
    		}
    	}
    	for (i = 1; i <= n; i++)
    		add(0, i, 0);
    	memset(dis, 250, sizeof(dis));
    	dis[0] = 0;
    	if (dfs(0))
    		printf("Yes");
    	else
    		printf("No");
    	return 0;
    }
    
  • 相关阅读:
    jquery validate使用总结
    javascript 学习笔记
    jquery easyui 学习总结
    javascript模式及javascript学习终极篇
    javascript学习笔记基础
    javascript学习笔记常见问题及技巧
    一道ITAT的题(C语言实现)
    JAVA实现约瑟夫算法
    JAXWS例子
    practical java笔记(实践1~5)
  • 原文地址:https://www.cnblogs.com/Iowa-Battleship/p/9706324.html
Copyright © 2011-2022 走看看