首页 技术 正文
技术 2022年11月21日
0 收藏 885 点赞 4,286 浏览 3132 个字

Description

刁姹接到一个任务,为税务部门调查一位商人的账本,看看账本是不是伪造的。账本上记录了n个月以来的收入情况,其中第i 个月的收入额为Ai(i=1,2,3…n-1,n), 。当 Ai大于0时表示这个月盈利Ai 元,当 Ai小于0时表示这个月亏损Ai 元。所谓一段时间内的总收入,就是这段时间内每个月的收入额的总和。 刁姹的任务是秘密进行的,为了调查商人的账本,她只好跑到商人那里打工。她趁商人不在时去偷看账本,可是她无法将账本偷出来,每次偷看账本时她都只能看某段时间内账本上记录的收入情况,并且她只能记住这段时间内的总收入。 现在,刁姹总共偷看了m次账本,当然也就记住了m段时间内的总收入,你的任务是根据记住的这些信息来判断账本是不是假的。

Input

第一行为一个正整数w,其中w < 100,表示有w组数据,即w个账本,需要你判断。每组数据的第一行为两个正整数n和m,其中n < 100,m < 1000,分别表示对应的账本记录了多少个月的收入情况以及偷看了多少次账本。接下来的m行表示刁姹偷看m次账本后记住的m条信息,每条信息占一行,有三个整数s,t和v,表示从第s个月到第t个月(包含第t个月)的总收入为v,这里假设s总是小于等于t。

Output

包含w行,每行是true或false,其中第i行为true当且仅当第i组数据,即第i个账本不是假的;第i行为false当且仅当第i组数据,即第i个账本是假的。

Sample Input

2
3 3
1 2 10
1 3 -5
3 3 -15
5 3
1 5 100
3 5 50
1 2 51

Sample Output

true
false

题解

解法一 差分约束

首先,我们令$sum_i$表示前$i-1$个月的收入和。

对于输入的三元组$(u,v,c)$,显然就是$sum_{v+1}-sum_u == c$,等价于$sum_{v+1}-sum_u >= c$ && $sum_{v+1}-sum_u <= c$,就变成的差分约束的模板。

等于说我们就加上$(u,v+1,c)$和$(v+1,u,-c)$这两条边,判断这个差分约束系统是否有解。

 //It is made by Awson on 2017.10.15
#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define sqr(x) ((x)*(x))
using namespace std;
const int N = ;
const int M = ; int n, m;
struct tt {
int to, next, cost;
}edge[(M<<)+];
int path[N+], top;
bool vis[N+];
int dist[N+]; bool dfs_SPFA(int u) {
vis[u] = ;
for (int i = path[u]; i; i = edge[i].next)
if (dist[edge[i].to] > dist[u]+edge[i].cost) {
if (vis[edge[i].to]) {vis[u] = ; return true;}
dist[edge[i].to] = dist[u]+edge[i].cost;
if (dfs_SPFA(edge[i].to)) {vis[u] = ; return true;}
}
vis[u] = ;
return false;
}
void add(int u, int v, int c) {
edge[++top].to = v;
edge[top].next = path[u];
edge[top].cost = c;
path[u] = top;
}
void work() {
memset(dist, , sizeof(dist));
memset(path, , sizeof(path)); top = ;
scanf("%d%d", &n, &m); n++;
for (int u, v, c, i = ; i <= m; i++) {
scanf("%d%d%d", &u, &v, &c);
add(u, v+, c); add(v+, u, -c);
}
for (int i = ; i <= n; i++) if (dfs_SPFA(i)) {
printf("false\n"); return;
}
printf("true\n");
}
int main() {
int t; scanf("%d", &t);
while (t--) work();
return ;
}

差分约束

解法二 带权并查集

我们令$cost_u$表示$u$这个节点到根节点的花销。

对于输入$(u,v,c)$,令$p = root_u$,$q = root_v$。

如果$p != q$即$u$,$v$不在一棵树上,那么我们将$father_p ← q$,此时$cost[p] = cost[v]-c-cost[u]$;

若$p == q$即在同一棵树树上,那么我们只要判断$cost[v]-cost[u] == c$即可。若是,则满足,否则为$false$。

 //It is made by Awson on 2017.10.15
#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define sqr(x) ((x)*(x))
using namespace std;
const int N = ;
const int M = ; int n, m;
#define set SET
#define cost COST
int set[N+], cost[N+];
int find_father(int r) {
if (set[r]) {
int t = find_father(set[r]);
cost[r] += cost[set[r]]; set[r] = t;
return t;
}
return r;
} void work() {
memset(set, , sizeof(set));
memset(cost, , sizeof(cost));
scanf("%d%d", &n, &m);
while (m--) {
int u, v, c;
scanf("%d%d%d", &u, &v, &c); v++;
int p = find_father(u);
int q = find_father(v);
if (p != q) {
set[p] = q;
cost[p] = cost[v]-c-cost[u];
}else {
if (cost[v]-cost[u] != c) {
while (m--) scanf("%d%d%d", &u, &v, &c);
printf("false\n"); return;
}
}
}
printf("true\n");
}
int main() {
int t; scanf("%d", &t);
while (t--) work();
return ;
}

带权并查集

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,020
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,513
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,359
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,142
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,772
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,850