【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;
}
转载于:https://www.cnblogs.com/MashiroSky/p/5914106.html
【poj2983】 Is the Information Reliable?相关推荐
- 【多模态】Multi-modal chemical information reconstruction from images and texts for exploring the
Multi-modal chemical information reconstruction from images and texts for exploring the near-drug sp ...
- 【题解】hdu 3586 Information Disturbing 二分 树形dp
题目描述 Information Disturbing Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Jav ...
- 【LeetCode】字符串 string(共112题)
[3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...
- 【CV】ECCV2020图像分割开源论文合集
编辑丨极市平台 极市导读 本文整理了ECCV2020目前开源的分割方向的全部论文,涵盖实例分割.语义分割.点云分割.目标跟踪与分割以及视频目标分割等多个方向,并对每一篇论文进行了简要介绍,文末附论文打 ...
- 【2023最新】Kotlin基础教程 Kotlin Basics 2 第二阶段:Sold! 2.4 【信息单位】Units of information
[2023最新]Kotlin基础教程 Kotlin Basics 文章目录 [2023最新]Kotlin基础教程 Kotlin Basics 2 第二阶段:Sold! 2.4 [信息单位]Units ...
- 【ReID】AlignedReID ++ : Dynamically matching local information for person re-identification
[ReID]AlignedReID ++ : Dynamically matching local information for person re-identification 模型 实验 问题 ...
- [转]机器学习和深度学习资料汇总【01】
本文转自:http://blog.csdn.net/sinat_34707539/article/details/52105681 <Brief History of Machine Learn ...
- UVA665 LA5658 False coin【暴力】
The "Gold Bar" bank received information from reliable sources that in their last group of ...
- 【SCI】英文论文投稿经验分享
发表SCI或EI类英文文章的一些投稿经验[转载] 英文投稿的一点经验[转载] 1. 首先一定要注意杂志的发表范围, 超出范围的千万别投,要不就是浪费时间;另外,每个杂志都有他们的具体格式要求,一定要按 ...
最新文章
- 985硕士毕业进京东,抱怨身边全是二本和大专生!没什么本事却整天高高在上!打交道太累了!...
- restful可以转发么_什么是RESTFUL?REST的请求方法有哪些,有什么区别?
- osm2pgsql windows “illegal option -W” error
- mysql groupby 取值_mysql获取groupby总记录行数的方法
- NioEventLoop 的实例化过程
- JVM实用参数(六) 吞吐量收集器
- uva 1218——Perfect Service
- docker mysql:8.0.27
- Scratch:海龟绘图(九)
- 异常关闭MyEclipse 8.6后,不能重启
- 基于解释的学习一个例子
- python全套视频迅雷下载_[全套视频] 老王Python基础,进阶,项目篇(无KEY完整版) 视频教程 15G...
- 我们常用的软件测试工具有哪些?
- 让SQL用户快速进入Hadoop大数据时代 —— Transwarp Inceptor是怎样炼成的
- 如何用SOLIDWORKS实现文字雕刻?来试试这样做!
- Java-事务的传播特性和隔离级别
- 【极客时间】左耳听风
- ANSYS Workbench扫掠划分网格出现网格扭曲解决办法
- OpenCV4.x图像处理实例-自动包围曝光(Auto Exposure Bracketing,AEB)
- VUE DIFF算法之快速DIFF