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. Android应用中菜单(Menu)的位置显示问题

    http://blog.csdn.net/songjinshi/article/details/17381245 注意:为了适配4.0菜单能够横向显示,建议在activity中添加android:th ...

  2. HBuilder打包ios应用

    1先安装itunes在电脑上 2,在HBuilder的工具栏上的"工具"选项卡上安装ios插件

  3. eval() 函数

    eval() 函数可计算某个字符串,并执行其中的的 JavaScript 代码. var str = '12+45*45'; alert(eval(str))//计算结果 还有一个重要作用可以把字符串 ...

  4. 013医疗项目-模块一:加入工具类ResultUtil

    这篇文章要做的就是优化,封装.把之前的代码尽量封装进类,并且不要硬编码. 在UserServiceimpl中的insertSysuser()函数之前是这么写的: ResultInfo resultIn ...

  5. [Android学习笔记]理解焦点处理原理的相关记录

    焦点处理相关记录 以下所涉及的焦点部分,只是按键移动部分,不明确包含Touch Focus部分 需解决问题 控件的下一个焦点是哪? 分析思路 当用户通过按键(遥控器等)触发焦点切换时,事件指令会通过底 ...

  6. linux patch 格式与说明(收录)

    转:http://blog.chinaunix.net/uid-26813001-id-3282954.html 首先介绍一下diff和patch.在这里不会把man在线文档上所有的选项都介绍一下,那 ...

  7. 不可不知的C#基础 4. 延迟加载 -- 提高性能

    延迟加载(lazy loading) 设计模式是为了避免一些无谓的性能开销而提出来的,所谓延迟加载就是当在真正需要数据(读取属性值)的时候,才真正执行数据加载操作. 有效使用它可以大大提高系统性能. ...

  8. Winform调用百度地图接口

    using System; using System.IO; using System.Collections.Generic; using System.ComponentModel; using ...

  9. 专门用于微信公众平台的Javascript API

    1 /**! 2 * 微信内置浏览器的Javascript API,功能包括: 3 * 4 * 1.分享到微信朋友圈 5 * 2.分享给微信好友 6 * 3.分享到腾讯微博 7 * 4.新的分享接口, ...

  10. Nutch搜索引擎(第4期)_ Eclipse开发配置

    1.环境准备 1.1 本期引言 前三期分别介绍了Nutch与Solr在Linux上面的安装,并做了简单的应用,这一期从开发的角度进行,因为我们日常最熟悉的开发环境是Windows,所以本期详细介绍Wi ...