题目链接: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\n");
return 0;
}
add(u, v, -1);
}
if(opt == 3)
{
add(v, u, 0);
}
if(opt == 4)
{
if(u == v)
{
printf("-1\n");
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\n");
return 0;
}
else
{
for(int i = 1; i <= n; i++)
ans += dis[i];
printf("%lld\n",-ans);
return 0;
}
}

【luogu P3275 [SCOI2011]糖果】 题解的更多相关文章

  1. [luogu P3275] [SCOI2011]糖果

    [luogu P3275] [SCOI2011]糖果 题目描述 幼儿园里有N个小朋友,lxhgww老师现在想要给这些小朋友们分配糖果,要求每个小朋友都要分到糖果.但是小朋友们也有嫉妒心,总是会提出一些 ...

  2. 洛谷P3275 [SCOI2011]糖果 题解

    题目链接: https://www.luogu.org/problemnew/show/P3275 分析: 本题就是一个裸的差分约束. 核心: x=1x=1x=1时,a=b,a−>b,b−> ...

  3. P3275 [SCOI2011]糖果 题解

    一道差分约束的模板题. 题目 题意:n个人每个人至少一个糖果,另外要满足k个不等式,求最少糖果数. 差分约束系统 给定一组不等式 $ x[i]-x[j]<=c[k] $ (或 $ x[i]-x[ ...

  4. P3275 [SCOI2011]糖果 && 差分约束(二)

    学习完了差分约束是否有解, 现在我们学习求解最大解和最小解 首先我们回想一下是否有解的求解过程, 不难发现最后跑出来任意两点的最短路关系即为这两元素的最短路关系. 即: 最后的最短路蕴含了所有元素之间 ...

  5. [Luogu 3275] SCOI2011 糖果

    [Luogu 3275] SCOI2011 糖果 第一道差分约束.感谢 AZe. 我的理解是根据一些不等关系建一个图,在图上边跑一个最长路(有时候是最短路). 因为可能存在负环,所以必须用 SPFA! ...

  6. 洛谷——P3275 [SCOI2011]糖果

    P3275 [SCOI2011]糖果 差分约束模板题,基本思路就是$d[v]+w[v,u]<=d[u]$,$Spfa$更新方法, 有点套路的是要建立原点,即图中不存在的点来向每个点加边,但同样这 ...

  7. 差分约束详解&&洛谷SCOI2011糖果题解

    差分约束系统: 如果一个系统由n个变量和m个约束条件组成,形成m个形如ai-aj≤k的不等式(i,j∈[1,n],k为常数),则称其为差分约束系统(system of difference const ...

  8. 题解——洛谷P3275 [SCOI2011]糖果

    一道条件非常多的差分约束 把\( a < b \)转化为\( a-b \le -1\)就可做了 \( a>b \)的情况同理 若有负环则无解输出-1 注意本题中要求每个人都有糖果 所以假设 ...

  9. 洛谷P3275 [SCOI2011]糖果 [差分约束系统]

    题目传送门 糖果 题目描述 幼儿园里有N个小朋友,lxhgww老师现在想要给这些小朋友们分配糖果,要求每个小朋友都要分到糖果.但是小朋友们也有嫉妒心,总是会提出一些要求,比如小明不希望小红分到的糖果比 ...

随机推荐

  1. Java中forEach, 用来遍历数组

    这里的for是Java中forEach, 用来遍历数组的.for(int i : d) 就是遍历int型数组d的 每一次访问数组d的时候读取的数据放入int型的i中.和for(int i=0;i< ...

  2. connection reset by peer, socket write error问题排查

    2018-03-15更新:弄明白connection reset产生的原因,见重新分析connection reset by peer, socket write error错误原因 在开发文件上传功 ...

  3. TOJ 3248 Flip Game

    Description Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of i ...

  4. TOJ 3486 Divisibility

    Description On the planet Zoop, numbers are represented in base 62, using the digits 0, 1, . . . , 9 ...

  5. unet知识点

    https://www.bilibili.com/video/av8483444/?from=search&seid=17755425906400905363 https://www.jian ...

  6. The Definitive C++ Book Guide and List--reference

    http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list Reference Style - All ...

  7. git提交代码报错 trailing whitespace的解决方法

    1. git提交代码报错 trailing whitespace 禁止执行pre-commit脚本 进入到项目目录中 chmod a-x .git/hooks/pre-commit 2.git提交代码 ...

  8. 前端测试框架 puppeteer 文档翻译

    puppeteer puppeteer 是一个通过DevTools 协议提供高级API 来控制 chrome,chromium 的 NODE库; puppeteer默认运行在 headless 模式, ...

  9. inteliJ IDEA使用SVN进行代码管理

    inteliJ 自带版本控制,所以不用像网上其他人说的那样,装第三方插件. 首先装完inteliJ 后,在File-->Setting-->Version Control中选择Subver ...

  10. LaTeX 修订

    LaTeX多人协同编辑的时候,修订起来与word相比较而言麻烦一些.不过随着技术的发展和需求的增多,会有越来越多的工具支持LaTeX的修订. (1)在线LaTeX ShareLaTeX是一个很优秀的在 ...