逆序建超级源快十倍还行

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); (a) <= (c); ++(a))
#define nR(a,b,c) for(register int a = (b); (a) >= (c); --(a))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Swap(a,b) ((a) ^= (b) ^= (a) ^= (b)) #define ON_DEBUGG #ifdef ON_DEBUGG #define D_e_Line printf("\n----------\n")
#define D_e(x) cout << (#x) << " : " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt", "r", stdin) #else #define D_e_Line ;
#define D_e(x) ;
#define Pause() ;
#define FileOpen() ; #endif
using namespace std;
struct ios{
template<typename ATP>inline ios& operator >> (ATP &x){
x = 0; int f = 1; char ch;
for(ch = getchar(); ch < '0' || ch > '9'; ch = getchar()) if(ch == '-') f = -1;
while(ch >= '0' && ch <= '9') x = x * 10 + (ch ^ '0'), ch = getchar();
x *= f;
return *this;
}
}io; const int N = 100007; struct Edge{
int nxt, pre, w;
}e[N * 3];
int head[N], cntEdge;
inline void add(int u, int v, int w){
e[++cntEdge] = (Edge){head[u], v, w}, head[u] = cntEdge;
} int vis[N]; long long dis[N];
inline bool SPFA(int u){
vis[u] = true;
for(register int i = head[u]; i; i = e[i].nxt){
int v = e[i].pre;
if(dis[v] < dis[u] + e[i].w){
dis[v] = dis[u] + e[i].w;
if(vis[v] || SPFA(v) == true){
return true;
}
}
}
vis[u] = false;
return false;
}
//int tot[N], q[N], top;
//inline bool SPFA(int st, int &n){
// vis[st] = true;
// q[++top] = st;
// tot[st] = 1;
// while(top){
// int u = q[top--];
// vis[u] = false;
// for(register int i = head[u]; i; i = e[i].nxt){
// int v = e[i].pre;
// if(dis[v] < dis[u] + e[i].w){
// dis[v] = dis[u] + e[i].w;
// tot[v] = tot[u] + 1;
// if(tot[v] > n + 1){
// return true;
// }
// if(!vis[v]){
// vis[v] = true;
// q[++top] = v;
// }
// }
// }
// }
// return false;
//} int main(){
int n, m;
io >> n >> m;
R(i,1,m){
int opt, x, y;
io >> opt >> x >> y;
if(opt == 1){
add(x, y, 0);
add(y, x, 0);
}
else if(opt == 2){
if(x == y){
printf("-1");
return 0;
}
add(x, y, 1);
}
else if(opt == 3){
add(y, x, 0);
}
else if(opt == 4){
if(x == y){
printf("-1");
return 0;
}
add(y, x, 1);
}
else if(opt == 5){
add(x, y, 0);
}
} // R(i,1,n){
// add(0, i, 1);
// }
nR(i,n,1){
add(0, i, 1);
} if(SPFA(0) == true){
printf("-1");
}
else{
long long ans = 0;
R(i,1,n){
//printf("%d -> %d\n", i, dis[i]);
ans += dis[i];
}
printf("%lld", ans);
} return 0;
}
/*
1 : add both
a + 1 <= b
2: a -> b 1
3 : a >= b + 0
3 : b -> a 0
*/

Luogu3275 [SCOI2011]糖果 (差分约束)的更多相关文章

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

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

  2. BZOJ 2330 SCOI2011糖果 差分约束

    2330: [SCOI2011]糖果 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2819  Solved: 820 题目连接 http://www ...

  3. BZOJ2330:[SCOI2011]糖果(差分约束)

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

  4. bzoj 2330 [SCOI2011]糖果 差分约束模板

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

  5. 洛谷P3275 [SCOI2011]糖果(差分约束)

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

  6. BZOJ 2330: [SCOI2011]糖果( 差分约束 )

    坑爹...要求最小值要转成最长路来做.... 小于关系要转化一下 , A < B -> A <= B - 1 ------------------------------------ ...

  7. [SCOI2011]糖果 (差分约束)

    题目链接 Solution 差分约束乱搞就好了. 需要注意的地方: 对于大于等于的直接联等于,应为等于,因为对于我满足条件而言,等于总是最好的. 对于等于的,注意要建双向边. 然后要开 \(long~ ...

  8. 【BZOJ2330】【SCOI2011】糖果 [差分约束]

    2330: [SCOI2011]糖果 Time Limit: 10 Sec  Memory Limit: 128 MB[Submit][Status][Discuss] Description 幼儿园 ...

  9. BZOJ 2330: [SCOI2011]糖果 [差分约束系统] 【学习笔记】

    2330: [SCOI2011]糖果 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 5395  Solved: 1750[Submit][Status ...

  10. bzoj2330糖果——差分约束

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2330 差分约束,再建立一个源点0,向所有点连边权为1的边,表示每个人都会分到糖果: 答案较大 ...

随机推荐

  1. 【Azure 存储服务】Java Azure Storage SDK V12使用Endpoint连接Blob Service遇见 The Azure Storage endpoint url is malformed

    问题描述 使用Azure Storage Account的共享访问签名(Share Access Signature) 生成的终结点,连接时遇见  The Azure Storage endpoint ...

  2. 硬件开发笔记(四):硬件开发基本流程,制作一个USB转RS232的模块(三):设计原理图

    前者   前面建立好的基础的元器件,下面开始设计原理图.   需求 USB转RS232,输出RS232 可以选择性输出5V的TTL 可以选择性输出3.3V的TTL   设计原理图 步骤一:CH340G ...

  3. 阿里巴巴开源限流组件Sentinel初探

    1 Sentinel主页 https://github.com/alibaba/Sentinel/wiki/主页 1.1 Sentinel介绍 随着微服务的流行,服务和服务之间的稳定性变得越来越重要. ...

  4. 【动态规划】统计蚂蚁 (ants)

    题目 描述 蚂蚁山上有T(1<=T<=1,000)种蚂蚁,标记为1..T,每种蚂蚁有N_i只蚂蚁(1<=N_i<=100),现有A(A<=5000)只蚂蚁,从中选出S,S ...

  5. 使用FileSystemWatcher监听文件状态

    更新记录 本文迁移自Panda666原博客,原发布时间:2021年7月2日. 一.FileSystemWatcher类型介绍 在.NET中使用 FileSystemWatcher 类型可以进行监视指定 ...

  6. 怎么理解相互独立事件?真的是没有任何关系的事件吗?《考研概率论学习之我见》 -by zobol

    1.从条件概率的定义来看独立事件的定义 2.从古典概率的定义来看独立事件的定义 3.P(A|B)和P(A)的关系是什么? 4.由P(AB)=P(A)P(B)推出"独立" 5.从韦恩 ...

  7. ssh-修改ssh服务端口号

    在启动selinux的状态下修改ssh的端口号 0.启动selinux     setenforce 1     getenforce 1.修改sshd_config的Port为2234 查看2234 ...

  8. python这不是有手就行?——python音频处理基础知识

    大家应该都知道声音的基础吧? 啊不知道当我没说吧~~~ 1.声音的基础 2.python读取.wav音频 Python学习交流Q群:660193417#### import wave import s ...

  9. LMC7660即-5V产生电路

    LMC7660为小功率极性反转电源转换器,通过LMC7660电路产生-5V电压,其芯片管脚定义如下表所示. LMC7660负电压产生电路如下图所示. 其中6脚当供电电压大于等于5V时该脚必须悬空,当供 ...

  10. Calendar类介绍_获取对象的方式和Calendar类的常用成员方法

    java.util.Calendar类:日历类 Calendar类是一个抽象类,里边提供了很多操作日历字段的方法(YEAR.MONTH.DAY_OF_MONTH.HOUR ) Calendar类无法直 ...