逆序建超级源快十倍还行

#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. python自动将新生成的报告作为附件发送并进行封装

    发送报告作为自动化部署来讲是一个重要的环节,废话不多说直接上代码吧,如果想更细致的了解内容查阅本博主上篇基本发送文章 特别叮嘱一下:SMTP协议默认端口25,qq邮箱SMTP服务器端口是465 别出丑 ...

  2. 【C++函数题目】重载求数组中最小值的函数

    题目来源:https://acm.ujn.edu.cn Time Limit: 1 Sec  Memory Limit: 128 MB Description 写一个函数名称为miniElement( ...

  3. 合宙AIR105(二): 时钟设置和延迟函数

    目录 合宙AIR105(一): Keil MDK开发环境, DAP-Link 烧录和调试 合宙AIR105(二): 时钟设置和延迟函数 Air105 的时钟 高频振荡源 芯片支持使用内部振荡源, 或使 ...

  4. Vmware-Centos7-NAT 网络配置

    首先一句话总结 NAT模式下,将VMware Network Adapter VMnet8的IP改为与虚拟机IP同一网段即可. 操作步骤 1. 打开虚拟网络编辑器 2. 配置NAT 选择NAT模式,取 ...

  5. SAP Smartforms 参数配置

    DATA : sf_name TYPE rs38l_fnam. DATA : sf_output_options TYPE ssfcompop. DATA : sf_control_parameter ...

  6. SAP 下拉框(选择屏幕)

    一.选择屏幕下拉框. DATA: g_vrmid TYPE vrm_id, "id of value set gt_vlist TYPE vrm_values, "internal ...

  7. 深入理解springboot的自动注入

    一.开篇   在平时的开发过程中用的最多的莫属springboot了,都知道springboot中有自动注入的功能,在面试过程中也会问到自动注入,你知道自动注入是怎么回事吗,springboot是如何 ...

  8. 微服务探索之路04篇k8s增加子节点,metrics资源监控,ingress-nginx域名配置及https配置

    1 k8s增加子节点 1.1 子节点服务器安装docker,使用脚本自动安装 curl -fsSL https://get.docker.com | bash -s docker --mirror A ...

  9. 强化学习-学习笔记14 | 策略梯度中的 Baseline

    本篇笔记记录学习在 策略学习 中使用 Baseline,这样可以降低方差,让收敛更快. 14. 策略学习中的 Baseline 14.1 Baseline 推导 在策略学习中,我们使用策略网络 \(\ ...

  10. Mvcapi解决H5请求接口跨域问题

    using Newtonsoft.Json;using System;using System.Collections.Generic;using System.Linq;using System.N ...