【poj2983】 Is the Information Reliable?
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?的更多相关文章
- 【POJ 2983】Is the Information Reliable?(差分约束系统)
id=2983">[POJ 2983]Is the Information Reliable? (差分约束系统) Is the Information Reliable? Time L ...
- 【POJ 2983】 Is the information reliable?
[题目链接] 点击打开链接 [算法] 差分约束系统,SPFA判负环 [代码] #include <algorithm> #include <bitset> #include & ...
- 【题解】hdu 3586 Information Disturbing 二分 树形dp
题目描述 Information DisturbingTime Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java ...
- 【异常】jps6432 -- process information unavailable
1 现象
- Python开发【项目】:RPC异步执行命令(RabbitMQ双向通信)
RPC异步执行命令 需求: 利用RibbitMQ进行数据交互 可以对多台服务器进行操作 执行命令后不等待命令的执行结果,而是直接让输入下一条命令,结果出来后自动打印 实现异步操作 不懂rpc的请移步h ...
- 【LeetCode】字符串 string(共112题)
[3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...
- 【kafka】Java连接出现Connection refused: no further information的解决方法
在Linux机器(ip:10.102.16.203)安装完kafka(参考:kafka的安装及使用),在windows上使用Java接口访问服务时(参考:Java实现Kafka的生产者.消费者),报异 ...
- 【LeetCode】831. Masking Personal Information 解题报告(Python)
[LeetCode]831. Masking Personal Information 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...
- 【论文速读】Shitala Prasad_ECCV2018】Using Object Information for Spotting Text
Shitala Prasad_ECCV2018]Using Object Information for Spotting Text 作者和代码 关键词 文字检测.水平文本.FasterRCNN.xy ...
随机推荐
- NGUI OnChange Event
那些组件有OnChange? 下面这些组件都有OnChange事件,当你点击,下拉选择时,就会触发它们. NGUI中对应的组件 PopupList (下拉列表) Toggle (复选框) Input ...
- 关于eclipse入门开发c/c++文章推荐
1. 关于编译说明. http://www.ibm.com/developerworks/cn/linux/opensource/os-ecc/ 2. 关于快捷键与代码阅读 http://www.cn ...
- C++中的运算符重载
首先思考以下几个问题: 1.什么是运算符重载? 2.为什么要重载运算符,它有什么用? 3.可以重载哪些运算符? 4.重载运算符有哪些规则? 一.基本概念 我们在程序中使用各种操作符,比如加(+).赋值 ...
- kprobe原理解析(一)
kprobe是linux内核的一个重要特性,是一个轻量级的内核调试工具,同时它又是其他一些更高级的内核调试工具(比如perf和systemtap)的“基础设施”,4.0版本的内核中,强大的eBPF特性 ...
- CSS选择器的权重与优先规则
权重顺序 “important > 内联 > ID > 类 > 标签 | 伪类 | 属性选择 > 伪对象 > 继承 > 通配符”. 原文:http://w ...
- ruby on rails 里使用SideKiq 做后台任务
环境:ubuntu14.4,ruby2.1.5, rails4.2 一.新一个rais项目:rails new active_job --skip-bundle 进入项目文件夹: cd a ...
- no.1
#import requests import urllib import bs4 try: html=urllib.urlopen('http://anyun.org') except HTTPer ...
- 自定义progressBar的旋转圆圈
在手工打造下拉刷新功能 自带的progressBar太丑了 做个也不费事,一个简单的圆形 旋转动画加type是sweep的gradient渐变 <rotate //旋转动画xmlns:andro ...
- 系统级I/O 第八周11.1~11.8
第十章 系统级I/O 输入输出I/O是在主存和外部设备(如磁盘,网络和终端)之间拷贝数据的过程.输入就是从I/O设备拷贝数据到主存,而输出就是从主存拷贝数据到I/O设备. 所有语言的运行时系统都提供执 ...
- warning: LF will be replaced by CRLF
一. Git提供了一个换行符检查功能(core.safecrlf),可以在提交时检查文件是否混用了不同风格的换行符.这个功能的选项如下: false - 不做任何检查warn - 在提交时检查并警告t ...