POJ 2983 Is the Information Reliable? 依旧差分约束
http://poj.org/problem?id=2983
题目大意:
星际大战开始了。你购买了情报,需要判断它的准确性。已知地方的根据地在由南向北排成一条直线。P A B X,表示A在B北面距离X光年的地方,另一种是V A B,表示只知道A在B的北面至少1光年的地方。
思路:
可转化为差分约束。
对于P A B X来说因为A-B=X (因为他们相距X光年,我们取北边为正方向) 可以记做: A-B >=X && A-B<=X,即A-B>=X && B-A>=-X
对于V A B ,可以记做 A-B>=1
然后老样子差分约束。记得建立一个连接所有顶点的超级顶点来保证图的连通性~
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int MAXN=1000+10;
const int MAXM=200000+1000;
const int INF=-9999999;
struct edge
{
int to;
int val;
int next;
}e[MAXM];
int head[MAXN],dis[MAXN],len,n,m; void add(int from,int to,int val)
{
e[len].to=to;
e[len].val=val;
e[len].next=head[from];
head[from]=len++;
} bool spfa()
{
int start=n+1;
for(int i=1;i<=n;i++)
dis[i]=INF; bool vis[MAXN]={0};
int cnt[MAXN]={0};
deque<int> q;
q.push_back(start);
vis[start]=1;
cnt[start]=1;
dis[start]=0;
while(!q.empty())
{
int cur=q.front();
q.pop_front();
vis[cur]=false;
for(int i=head[cur];i!=-1;i=e[i].next)
{
int id=e[i].to;
if(dis[id] < dis[cur] + e[i].val)
{
dis[id]=dis[cur] + e[i].val;
if(!vis[id])
{
if(++cnt[id] > n)
return true;
vis[id]=true;
if(!q.empty() && dis[id] > dis[q.front()])
q.push_back(id);
else
q.push_front(id);
}
}
}
}
return false;
} int main()
{
while(~scanf("%d%d",&n,&m))
{
memset(head,-1,sizeof(head));
len=0; for(int i=0;i<m;i++)
{
char cmd[5];
int from,to,val; scanf("%s",cmd);
if(cmd[0]=='P')
{
scanf("%d%d%d",&from,&to,&val);
add(from,to,-val);
add(to,from,val);
}
else
{
scanf("%d%d",&from,&to);
add(to,from,1);
}
}
for(int i=1;i<=n;i++)
add(n+1,i,0); if(spfa())
puts("Unreliable");
else
puts("Reliable");
}
return 0;
}
POJ 2983 Is the Information Reliable? 依旧差分约束的更多相关文章
- POJ 2983 Is the Information Reliable?(差分约束系统)
http://poj.org/problem?id=2983 题意:N 个 defense stations,M条消息,消息有精确和模糊之分,若前边为P.则为精确消息,有两个defense stati ...
- POJ 2983 Is the Information Reliable? 差分约束
裸差分约束. //#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #i ...
- POJ 2983 Is the Information Reliable? 信息可靠吗 (差分约束,spfa)
题意:有n个站排成一列,针对每个站的位置与距离关系,现有多个约束条件,约束条件分两种:(1)确定的.明确说明站a距离站b多少个单位距离.(2)不确定的.只知道a在b的左边至少1个单位距离. 根据已知 ...
- POJ 2983:Is the Information Reliable?(差分约束)
题目大意:有n个点在一条直线上,有两类关系:P(x,y,v)表示x在y北边v距离处,V(x,y)表示x在y北边至少1距离出,给出一些这样的关系,判断是否有矛盾. 分析: 差分约束模板题,约束条件P:a ...
- ●POJ 2983 Is the Information Reliable?
题链: http://poj.org/problem?id=2983 题解: 差分约束. 1).对于条件(P u v w),不难发现反映到图上就是: $dis[u]-dis[v]=w$,所以添加两条边 ...
- Is the Information Reliable?(差分约束)
Description The galaxy war between the Empire Draco and the Commonwealth of Zibu broke out 3 years a ...
- poj2983--Is the Information Reliable?(差分约束)
Is the Information Reliable? Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 11125 A ...
- Is the Information Reliable? -POJ2983差分约束
Time Limit: 3000MS Memory Limit: 131072K Description The galaxy war between the Empire Draco and the ...
- 【POJ 2983】Is the Information Reliable?(差分约束系统)
id=2983">[POJ 2983]Is the Information Reliable? (差分约束系统) Is the Information Reliable? Time L ...
随机推荐
- C#接口,类,集成
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- ubuntu系统配置WinQQ
首先安装Wine sudo add-apt-repository ppa:wine/wine-builds sudo apt-get update sudo apt-get install wineh ...
- Spark 1.6.1 源码分析
由于gitbook网速不好,所以复制自https://zx150842.gitbooks.io/spark-1-6-1-source-code/content/,非原创,纯属搬运工,若作者要求,可删除 ...
- 【Django】AJAX
目录 JSON 序列化 stringify 反序列化 parse JSON与XML对比 AJAX简介 AJAX常见应用场景 jQuery实现AJAX JS实现AJAX AJAX请求设置csrf_tok ...
- C# Unable to load DLL 'WzCanDll.dll':找不到指定的模块
一.打开app无法加载DLL 我用C++编写的DLL,然后用C#写的界面APP,在自己的电脑上打开没有问题,放在其它电脑上就出现无法加载DLL库的问题,一连接APP就会出现问题,如下图所示: 二.解决 ...
- Mysql学习总结(17)——MySQL数据库表设计优化
1.选择优化的数据类型 MySQL支持很多种不同的数据类型,并且选择正确的数据类型对于获得高性能至关重要.不管选择何种类型,下面的简单原则都会有助于做出更好的选择: (1).更小通常更好 一般来说,要 ...
- Web页面转换成Word文件,利用wordXML
简介:处理流程表单数据以WordXML形式填充Word文档表格换行符丢失问题 //将前台收集的XML中“$”循环拆分成"<w:br/>" by pengyc 解决表格填 ...
- 公众平台调整SSL安全策略,请开发者注意升级
公众平台调整SSL安全策略,请开发者注意升级 近一段时间HTTPS加密协议SSL曝出高危漏洞,可能导致网络中传输的数据被黑客监听,对用户信息.网络账号密码等安全构成威胁.为保证用户信息以及通信安全,微 ...
- monkey基础知识(二)
- Code froces 831 A. Unimodal Array
A. Unimodal Array time limit per test 1 second memory limit per test 256 megabytes input standard in ...