http://poj.org/problem?id=2983 (题目链接)

一个SB错误TLE了半个小时。。。

题意

  一条直线上有n个点,给出m条信息,若为P则表示点A在点B的北方X米,若为V则表示A在B的北方。判断给出的信息是否合法。

Solution

  对于P,A-B=X等价于是A-B>=X && A-B<=X(B-A>=-X)。

  对于V,A-B>=1。

  所以我们就可以利用差分约束去求解这个问题,在图上跑SPFA最长路判断是否存在正环。

  注意设置一个超级源点使得图联通。

代码

// poj2983
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
#define LL long long
#define inf 2147483640
#define Pi acos(-1.0)
#define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
using namespace std;
inline LL getint() {
int f,x=0;char ch=getchar();
while (ch<='0' || ch>'9') {if (ch=='-') f=-1;else f=1;ch=getchar();}
while (ch>='0' && ch<='9') {x=x*10+ch-'0';ch=getchar();}
return x*f;
} const int maxn=1010,maxm=100010;
struct edge {int to,next,w;}e[maxm<<2];
int head[maxn],dis[maxn],vis[maxn],cnts[maxn],n,cnt,m; void link(int u,int v,int w) {
e[++cnt].to=v;e[cnt].next=head[u];head[u]=cnt;e[cnt].w=w;
}
bool SPFA() {
deque<int> q;
for (int i=1;i<=n;i++) dis[i]=-inf,cnts[i]=0,vis[i]=0;
q.push_back(n+1);
dis[n+1]=0;
vis[n+1]=1;
cnts[n+1]=1;
while (!q.empty()) {
int x=q.front();
q.pop_front();
vis[x]=0;
for (int i=head[x];i;i=e[i].next)
if (dis[e[i].to]<dis[x]+e[i].w) {
dis[e[i].to]=dis[x]+e[i].w;
if (!vis[e[i].to]) {
if (++cnts[e[i].to]>n) return 1;
vis[e[i].to]=1;
if (!q.empty() && dis[e[i].to]<dis[q.front()]) q.push_back(e[i].to);
else q.push_front(e[i].to);
}
}
}
return 0;
}
int main() {
while (scanf("%d%d",&n,&m)!=EOF) {
for (int i=1;i<=n+1;i++) head[i]=0;
cnt=0;
while (m--) {
int u,v,w;
char ch[5];
scanf("%s ",ch);
if (ch[0]=='P') {
scanf("%d%d%d",&u,&v,&w);
link(v,u,w);
link(u,v,-w);
}
else {
scanf("%d%d",&u,&v);
link(v,u,1);
}
}
for (int i=1;i<=n;i++) link(n+1,i,0);
if (SPFA()) puts("Unreliable");
else puts("Reliable");
}
return 0;
}

  

【poj2983】 Is the Information Reliable?的更多相关文章

  1. 【POJ 2983】Is the Information Reliable?(差分约束系统)

    id=2983">[POJ 2983]Is the Information Reliable? (差分约束系统) Is the Information Reliable? Time L ...

  2. 【POJ 2983】 Is the information reliable?

    [题目链接] 点击打开链接 [算法] 差分约束系统,SPFA判负环 [代码] #include <algorithm> #include <bitset> #include & ...

  3. 【题解】hdu 3586 Information Disturbing 二分 树形dp

    题目描述 Information DisturbingTime Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java ...

  4. 【异常】jps6432 -- process information unavailable

    1 现象

  5. Python开发【项目】:RPC异步执行命令(RabbitMQ双向通信)

    RPC异步执行命令 需求: 利用RibbitMQ进行数据交互 可以对多台服务器进行操作 执行命令后不等待命令的执行结果,而是直接让输入下一条命令,结果出来后自动打印 实现异步操作 不懂rpc的请移步h ...

  6. 【LeetCode】字符串 string(共112题)

    [3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...

  7. 【kafka】Java连接出现Connection refused: no further information的解决方法

    在Linux机器(ip:10.102.16.203)安装完kafka(参考:kafka的安装及使用),在windows上使用Java接口访问服务时(参考:Java实现Kafka的生产者.消费者),报异 ...

  8. 【LeetCode】831. Masking Personal Information 解题报告(Python)

    [LeetCode]831. Masking Personal Information 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...

  9. 【论文速读】Shitala Prasad_ECCV2018】Using Object Information for Spotting Text

    Shitala Prasad_ECCV2018]Using Object Information for Spotting Text 作者和代码 关键词 文字检测.水平文本.FasterRCNN.xy ...

随机推荐

  1. web安全测试工具的局限性

    讨论安全漏洞的原理,谈谈工具的局限. 先说下扫描工具的原理: 扫描工具可以看做由两部分组成:爬虫+校验机构.爬虫的作用是搜集整个被采集对象的链接,然后校验机构对这些链接逐一进行验证. 说扫描工具的局限 ...

  2. java13-5 JDK1.5以后的一个新特性和Integer的面试题

    1.JDK5的新特性 自动装箱:把基本类型转换为包装类类型 自动拆箱:把包装类类型转换为基本类型 注意一个小问题: 在使用时,Integer x = null;代码就会出现NullPointerExc ...

  3. java9-6 内部类

    1. 内部类概述: 把类定义在其他类的内部,这个类就被称为内部类. 举例:在类A中定义了一个类B,类B就是内部类. 内部的访问特点: A:内部类可以直接访问外部类的成员,包括私有. B:外部类要访问内 ...

  4. parse_url等函数

    $_SERVER["REQUEST_URI"]://这个可以获取域名后的url,比如/test1/parse_url.php?id=7&name=wuhan 常见用法$ur ...

  5. Masonry

    Autolayout就像一个知情达理,善解人意的好姑娘,可惜长相有点不堪入目,所以追求者寥寥无几.所幸遇到了化妆大师cloudkite,给她来了一个完美的化妆,从此丑小鸭Autolayout变成了美天 ...

  6. poj 1046 Color Me Less

    Color Me Less Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 33007   Accepted: 16050 D ...

  7. WPF在XAML中Binding使用StringFormat属性

    1. 绑定Currency, 如果没有字符的话, =后面需要先加入{}. 不加的话会出问题. 1 <TextBlock Text="{Binding Amount, StringFor ...

  8. Download the WDK, WinDbg, and associated tools

    Download the WDK, WinDbg, and associated tools This is where you get your Windows Driver Kit (WDK) a ...

  9. Summary Ranges

    Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...

  10. 解决centos7重启后出现ata bus error

    昨天把centos7装在电脑上了,还把win7系统格掉了,从此电脑上只装centos,有一种弃暗投明的感觉. 装完重启后欣赏了一番成果,一个halt命令想把系统关掉,却发现屏幕没黑,机器不转了,电源灯 ...