【题目链接】

点击打开链接

【算法】

当x = 1时,连边(a,b,0)和(b,a,0)

当x = 2时,连边(a,b,1)

当x = 3时,连边(b,a,0)

当x = 4时,连边(b,a,1)

当x = 5时,连边(a,b,0)

建立超级源点(Super Source),将这个点与所有点连一条权值为1的边,注意加边时要倒着加,否则会时间超限

【代码】

#include<bits/stdc++.h>
using namespace std;
#define MAXN 100010 struct Edge
{
int to;
long long w;
int nxt;
} e[MAXN<<]; int x,a,b,i,n,k,tot;
long long dis[MAXN];
int head[MAXN]; template <typename T> inline void read(T &x)
{
int f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) { if (c == '-') f = -f; }
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
}
template <typename T> inline void write(T x)
{
if (x < )
{
putchar('-');
x = -x;
}
if (x > ) write(x/);
putchar(x%+'');
}
template <typename T> inline void writeln(T x)
{
write(x);
puts("");
}
inline void add(int u,int v,long long w)
{
tot++;
e[tot] = (Edge){v,w,head[u]};
head[u] = tot;
}
inline long long spfa()
{
int i,cur,v;
long long w,ans = ;
queue<int> q;
static bool inq[MAXN];
static int cnt[MAXN];
q.push();
inq[] = true;
cnt[] = ;
while (!q.empty())
{
cur = q.front();
q.pop();
inq[cur] = false;
for (i = head[cur]; i; i = e[i].nxt)
{
v = e[i].to;
w = e[i].w;
if (dis[cur] + w > dis[v])
{
dis[v] = dis[cur] + w;
if (!inq[v])
{
inq[v] = true;
q.push(v);
cnt[v]++;
if (cnt[v] > n) return -;
}
}
}
}
for (i = ; i <= n; i++) ans += dis[i];
return ans;
} int main()
{ read(n); read(k);
for (i = n; i >= ; i--) add(,i,);
for (i = ; i <= k; i++)
{
read(x); read(a); read(b);
if (x == )
{
add(a,b,);
add(b,a,);
}
if (x == )
{
add(a,b,);
if (a == b)
{
writeln(-);
return ;
}
}
if (x == ) add(b,a,);
if (x == )
{
add(b,a,);
if (a == b)
{
writeln(-);
return ;
}
}
if (x == ) add(a,b,);
}
writeln(spfa()); return ; }

【SCOI 2011】 糖果的更多相关文章

  1. CDOJ 435 (SCOI 2011) 糖果 Label:差分约束系统

    糖果 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 131072/131072KB (Java/Others) Submit Stat ...

  2. BZOJ 2330 SCOI 2011 糖果

    2330: [SCOI2011]糖果 Time Limit: 10 Sec Memory Limit: 128 MB Description 幼儿园里有N个小朋友,lxhgww老师现在想要给这些小朋友 ...

  3. [SCOI 2011]糖果

    Description 题库链接 给出 \(N\) 个节点,节点有正点权, \(K\) 个三元组 \((X,A,B)\) 来描述节点点权之间的关系. 如果 \(X=1\) , 表示 \(A\) 的点权 ...

  4. 解题:SCOI 2011 糖果

    题面 能把差分约束卡死的题,因为正解并不是差分约束,然而被我用一种奇怪的姿势过去了... 差分约束就是相等互相连零边,不超过/不低于从不多的一方向另一方连零边,超过/低于从少的一方向另一方连最小的边权 ...

  5. C++之路进阶——codevs2404(糖果)

    2404 糖果 2011年省队选拔赛四川  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 大师 Master     题目描述 Description 幼儿园里有N个小朋友,l ...

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

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

  7. 原生js可爱糖果数字时间特效

    效果展示:http://hovertree.com/texiao/js/35/ 数字采用漂亮的糖果皮肤设计 效果图: 代码如下: <!DOCTYPE html> <html> ...

  8. [C#项目开源] MongoDB 可视化管理工具 (2011年10月-至今)

    正文 该项目从2011年10月开始开发,知道现在已经有整整5年了.MongoDB也从一开始的大红大紫到现在趋于平淡. MongoCola这个工具在一开始定位的时候只是一个Windows版本的工具,期间 ...

  9. BZOJ 2440: [中山市选2011]完全平方数 [容斥原理 莫比乌斯函数]

    2440: [中山市选2011]完全平方数 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3028  Solved: 1460[Submit][Sta ...

随机推荐

  1. php切换版本之后 redis 安装使用

    一:redis安装Download, extract and compile Redis with: $ wget http://download.redis.io/releases/redis-3. ...

  2. iOS APP EuclidStudy Service Support

    Hi,If you have any questions, you can send a message here or send them to us. We will answer you as ...

  3. react antD moment

    import moment from 'moment' console.log(moment().add(1, 'days').format('YYYY-MM-DD')) //当前时间前一天 cons ...

  4. ionic3开发环境搭建与配置(win10系统)

    1.安装nodeJS(不会的自行百度) 2.安装ionic和cordova,执行以下命令: npm install -g ionic cordova 3.安装Java JDK: 下载地址:http:/ ...

  5. 面试:B

    协程 材质和贴图 任意模块的深入理解

  6. LINUX-关机 (系统的关机、重启以及登出 )

     shutdown -h now 关闭系统(1) init 0 关闭系统(2) telinit 0 关闭系统(3) shutdown -h hours:minutes & 按预定时间关闭系统  ...

  7. 【Codeforces 986B】Petr and Permutations

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] n为奇数时3n和7n+1奇偶性不同 n为偶数时也是如此 然后交换任意一对数 逆序对的对数的奇偶性会发生改变一次 求出逆序对 对n讨论得出答案. ...

  8. 阿里巴巴json包 --------fastjson

    fastjson对null的处理----String str2 = JSONObject.toJSONString(jsonMap, SerializerFeature.WriteMapNullVal ...

  9. Poor Hanamichi

    Poor Hanamichi Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  10. 树上启发式合并(DSU on tree)

    //heavy-light decomposition style .//http://codeforces.com/blog/entry/44351 int cnt[maxn]; bool big[ ...