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. 2D Skeletal Animation Ready

    骨骼动画 Cool 昨天研究了一天的2D骨骼动画,自己动手做了骨骼动画,感觉比用序列帧做动画方便多了,非常Cool ! 刚开始做骨骼动画用的是一整张图,做动画时在分配完权重之后,拉伸顶点上连接着的其它 ...

  2. php报错日志:PHP Deprecated:Automatically populating $HTTP_RAW_POST_DATA is deprecated

    前几天将线上php服务升级到5.6.x版本后,php-error.log报出错误:PHP Deprecated: Automatically populating $HTTP_RAW_POST_DAT ...

  3. JavaScript---基本语法

    字符串方法:str.lengthstr.charAt(i):取字符串中的某一个;str.indexOf('e');找第一个出现的位置;找不到返回-1;str.lastIndexOf('e'):找最后一 ...

  4. Wordpress 3.5.1的debug流水账

    一个合作网站是基于Wordpress做的, 不能正常使用FeedWordPress这个插件, 对方没有开发者, 只有一个类似于美工和编辑的人, 因为我在本地使用同样的版本(Wordpress 3.5. ...

  5. 08Spring_Spring和junit测试集成

    第一步: 在项目导入 spring-test-3.2.0.RELEASE.jar 第二步: 编写测试类

  6. Protocol Buffer多态

    java中有多态的概念,protobuf本身没有多态的概念,但是它有一个扩展的概念. 以聊天消息为例,先看下面这个类图,基类是ChatMessage,子类TextMessage和ImageMessag ...

  7. centos一键优化脚本

    centos一键优化脚本:细节:http://oldboy.blog.51cto.com/2561410/1336488网络状态优化:http://oldboy.blog.51cto.com/2561 ...

  8. JQuery 如何选择带有多个class的元素

    Q: 比如下面代码需要选择同时带有这几个class的元素,怎么写? 1 <div class="modal fade in"></div> A: 1. 依次 ...

  9. 免费Flash图表工具FusionChart

    图表显示是很多开发工作所必不可少的一项功能,今天我介绍一个前段时间发现的免费的Flash图表开发工具,可以通过Adobe Flash实现数据的图表化,动态化以及相互交互. FusionChart是一个 ...

  10. Jmeter测试webocket协议

    Jmeter本身不支持websocket协议的,所以需要安装第三方的插件还有6个依赖包. 首先,我们需要准备Jmeter的WebSocket协议的支持插件: JMeterWebSocketSample ...