逆序建超级源快十倍还行

#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. 企业应用架构研究系列二十六:信号量SemaphoreSlim与Semaphore

    在进行多线程程序的开发和设计的过程中,不可避免的需要引入semaphore信号量这个组件,这是.net框架提供的一个对多线程计数互斥的方案,就是允许指定的线程个数访问特定的资源而增加的 一个" ...

  2. 「ABC 249Ex」Dye Color

    考虑停时定理. 初始势能为 \(\sum \Phi(cnt_i)\),末势能为 \(\Phi(n)\),我们希望构造这样一个 \(\Phi:Z\to Z\) 函数,使得每一次操作期望势能变化量为常数. ...

  3. vs2022+resharper C++ = 拥有一个不输clion的代码体验

    这篇文章详细讲一下resharper C++在vs2022中的配置,让他拥有跟clion一样好用的代码补全功能. 为什么clion写代码体验很好好用为啥还要用vs呢,因为网上很多教程都是基于visua ...

  4. Crontab在服务端进行设置定时执行任务

    Crontab简crontab是一个可以根据时间.日期.月份.星期的组合调度对重复任务的执行的守护进程.也可以讲Linux crontab是用来定期执行程序的命令. 当安装完成操作系统之后,默认便会启 ...

  5. easy-captcha生成验证码

    通常一些网页登陆时,都需要通过验证码去登录: 生成验证码的方法有很多,这次分享一个验证码即能是汉字的 又能是算术的. 首先maven坐标: <dependency> <groupId ...

  6. 使用nodejs的wxmnode模块,开发一个微信自动监控提醒功能,做个天气预报。

    这个模块是一个公众号的模块,名字叫"帮你看着". 原本这个公众号是做股票监控提醒的,我也没炒股.因为接口支持写入任何内容,所以可以有其他的用处.比如做成天气预报定时提醒. 我们去n ...

  7. Vue回炉重造之封装一个实用的人脸识别组件

    前言 人脸识别技术现在越来越火,那么我们今天教大家实现一个人脸识别组件. 资源 element UI Vue.js tracking-min.js face-min.js 源码 由于我们的电脑有的有摄 ...

  8. RabbitMD大揭秘

    RabbitMD大揭秘 欢迎关注H寻梦人公众号 通过SpringBoot整合RabbitMQ的案例来说明,RabbitMQ相关的各个属性以及使用方式:并通过相关源码深刻理解. Queue(消息队列) ...

  9. ASP.NET MVC之读取服务器文件资源的两种方式

    初次认识asp.net mvc时,以为所有文件都需要走一遍路由,然后才能在客户端显示, 所以我首先介绍这一种方式 比如说:我们在服务器上有图片: ~/resource/image/5.jpg 我们就需 ...

  10. archlinux-小米pro15_2020款使用archlinux(MX350显卡驱动安装)

    1.官网下载archlinux ISO镜像 https://archlinux.org/download/   使用磁力链接下载 2.使用软碟通将镜像写入U盘,制作成U盘启动盘 3.进入BIOS 关掉 ...