洛谷 P3275 [SCOI2011]糖果
题目链接
题解
差分约束 学过的应该都会做
不会的自行百度,这里不多讲
opt=1 连一条长度为0的双向边
opt=2 (u->v) \(len=-1\)
opt=3 (v->u) \(len=0\)
opt=4 (v->u) \(len=-1\)
opt=5 (u->v) \(len=0\)
0到其他点都连一条长度为-1的边(从n到1连玄学的力量, 正着加边会T)
然后spfa最短路即可
Code
#include<bits/stdc++.h>
#define LL long long
#define RG register
using namespace std;
inline int gi() {
int f = 1, s = 0;
char c = getchar();
while (c != '-' && (c < '0' || c > '9')) c = getchar();
if (c == '-') f = -1, c = getchar();
while (c >= '0' && c <= '9') s = s*10+c-'0', c = getchar();
return f == 1 ? s : -s;
}
const int N = 100010;
struct node {
int to, next, w;
}g[(N<<1)+N];
int last[N], gl;
inline void add(int x, int y, int z) {
g[++gl] = (node) {y, last[x], z};
last[x] = gl;
return ;
}
LL dis[N];
bool vis[N];
int cnt[N];
queue<int> q;
int main() {
int n = gi(), k = gi();
for (int i = 1; i <= k; i++) {
int k = gi(), u = gi(), v = gi();
if (k == 1)
add(u, v, 0), add(v, u, 0);
else if (k == 2) {
if (u == v) {
puts("-1");
return 0;
}
add(u, v, -1);
}
else if (k == 3) add(v, u, 0);
else if (k == 4) {
if (u == v) {
puts("-1");
return 0;
}
add(v, u, -1);
}
else add(u, v, 0);
}
for (int i = n; i >= 1; i--) add(0, i, -1);
q.push(0);
memset(dis, 127/3, sizeof(dis));
dis[0] = 0;
while (!q.empty()) {
int u = q.front();
q.pop();
if (++cnt[u] == n) {
puts("-1");
return 0;
}
for (int i = last[u]; i; i = g[i].next) {
int v = g[i].to;
if (dis[v] > dis[u]+g[i].w) {
dis[v] = dis[u]+g[i].w;
if (!vis[v]) q.push(v), vis[v] = 1;
}
}
vis[u] = 0;
}
LL ans = 0;
for (int i = 1; i <= n; i++)
ans += dis[i];
printf("%lld\n", -ans);
return 0;
}
洛谷 P3275 [SCOI2011]糖果的更多相关文章
- 洛谷——P3275 [SCOI2011]糖果
P3275 [SCOI2011]糖果 差分约束模板题,基本思路就是$d[v]+w[v,u]<=d[u]$,$Spfa$更新方法, 有点套路的是要建立原点,即图中不存在的点来向每个点加边,但同样这 ...
- 洛谷P3275 [SCOI2011]糖果(差分约束,最长路,Tarjan,拓扑排序)
洛谷题目传送门 差分约束模板题,等于双向连0边,小于等于单向连0边,小于单向连1边,我太蒻了,总喜欢正边权跑最长路...... 看遍了讨论版,我是真的不敢再入复杂度有点超级伪的SPFA的坑了 为了保证 ...
- 洛谷P3275 [SCOI2011]糖果 [差分约束系统]
题目传送门 糖果 题目描述 幼儿园里有N个小朋友,lxhgww老师现在想要给这些小朋友们分配糖果,要求每个小朋友都要分到糖果.但是小朋友们也有嫉妒心,总是会提出一些要求,比如小明不希望小红分到的糖果比 ...
- 【POJ 3159】Candies&&洛谷P3275 [SCOI2011]糖果
来补一下自己很久以前那个很蒟蒻很蒟蒻的自己没有学懂的知识 差分约束,说白了就是利用我们在求最短路的一个\(relax\)操作时的判断的原理 \[dis[v]>dis[u]+disj(u,v)\] ...
- 题解——洛谷P3275 [SCOI2011]糖果
一道条件非常多的差分约束 把\( a < b \)转化为\( a-b \le -1\)就可做了 \( a>b \)的情况同理 若有负环则无解输出-1 注意本题中要求每个人都有糖果 所以假设 ...
- 洛谷P3275 [SCOI2011]糖果 题解
题目链接: https://www.luogu.org/problemnew/show/P3275 分析: 本题就是一个裸的差分约束. 核心: x=1x=1x=1时,a=b,a−>b,b−> ...
- 洛谷P3275 [SCOI2011]糖果(差分约束)
题目描述 幼儿园里有 $N$ 个小朋友,$lxhgww $老师现在想要给这些小朋友们分配糖果,要求每个小朋友都要分到糖果.但是小朋友们也有嫉妒心,总是会提出一些要求,比如小明不希望小红分到的糖果比他的 ...
- 洛谷P3275 [SCOI2011]糖果
差分约束大坑题 #include<cstdio> #include<cstdlib> #include<algorithm> #include<cstring ...
- 洛谷P3275 [SCOI2011]糖果_差分约束_判负环
Code: #include<cstdio> #include<queue> #include<algorithm> using namespace std; co ...
随机推荐
- 551. Student Attendance Record I 从字符串判断学生考勤
[抄题]: You are given a string representing an attendance record for a student. The record only contai ...
- OpenNebula 深入分析
-------------------OpenNebula 深入分析------------------- #容量清单 属性 描述 NAME 如果名字是空的,那么默认名字是:one-<VID&g ...
- 数字图像处理实验(17):PROJECT 06-04,Color Image Segmentation 标签: 图像处理MATLAB 2017-05-27 21:13
实验报告: Objective: Color image segmentation is a big issue in image processing. This students need to ...
- xgboost 完全调参指南
http://www.2cto.com/kf/201607/528771.html xgboost: https://www.analyticsvidhya.com/blog/2016/03/comp ...
- Yii2中ACF和RBAC
ACF ( Access Control Filter) ACF ( Access Control Filter)官网的解释就是一个可以在模型或控制器执行行为过滤器,当有用户请求时,ACF将检查acc ...
- React官方网站学习
React官方网站 英文版 https://reactjs.org/tutorial/tutorial.html React官方网站 中文版 https://react.docschina.org ...
- PLSQL_Developer 连接win7_64位oracle11g
window7系统 安装的64位 oracle11g,连接32位PLSQL_Developer 1 . 下载 PLSQL_Developer 9.0以上版本(绿色含汉化) 官方的 instantc ...
- CentOS7 yum安装lamp环境
1.安装apache yum install httpd #根据提示,输入Y安装即可成功安装 systemctl start httpd.service #启动apache systemctl sto ...
- Mono for Android for Visual Studio 2010安装及试用
安装 Mono for Android for Visual Studio 2010 需要下面4个步骤: 1.安装 JDK 下载并安装 Java 1.6 (Java 6) JDK. 2.安装 Andr ...
- linux上使用tomcat及查看日志
启动 startup.sh #执行bin/startup.sh #启动tomcatbin/shutdown.sh #停止tomcattail -f logs/catalina.out #看tomcat ...