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? 依旧差分约束的更多相关文章

  1. POJ 2983 Is the Information Reliable?(差分约束系统)

    http://poj.org/problem?id=2983 题意:N 个 defense stations,M条消息,消息有精确和模糊之分,若前边为P.则为精确消息,有两个defense stati ...

  2. POJ 2983 Is the Information Reliable? 差分约束

    裸差分约束. //#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #i ...

  3. POJ 2983 Is the Information Reliable? 信息可靠吗 (差分约束,spfa)

    题意:有n个站排成一列,针对每个站的位置与距离关系,现有多个约束条件,约束条件分两种:(1)确定的.明确说明站a距离站b多少个单位距离.(2)不确定的.只知道a在b的左边至少1个单位距离.  根据已知 ...

  4. POJ 2983:Is the Information Reliable?(差分约束)

    题目大意:有n个点在一条直线上,有两类关系:P(x,y,v)表示x在y北边v距离处,V(x,y)表示x在y北边至少1距离出,给出一些这样的关系,判断是否有矛盾. 分析: 差分约束模板题,约束条件P:a ...

  5. ●POJ 2983 Is the Information Reliable?

    题链: http://poj.org/problem?id=2983 题解: 差分约束. 1).对于条件(P u v w),不难发现反映到图上就是: $dis[u]-dis[v]=w$,所以添加两条边 ...

  6. Is the Information Reliable?(差分约束)

    Description The galaxy war between the Empire Draco and the Commonwealth of Zibu broke out 3 years a ...

  7. poj2983--Is the Information Reliable?(差分约束)

    Is the Information Reliable? Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 11125   A ...

  8. Is the Information Reliable? -POJ2983差分约束

    Time Limit: 3000MS Memory Limit: 131072K Description The galaxy war between the Empire Draco and the ...

  9. 【POJ 2983】Is the Information Reliable?(差分约束系统)

    id=2983">[POJ 2983]Is the Information Reliable? (差分约束系统) Is the Information Reliable? Time L ...

随机推荐

  1. SpringBoot与Dubbo整合-项目搭建

    本章节建立生产者和消费者来演示dubbo的demo 生产者:springboot-dubbo-provider 和 消费者:springboot-dubbo-consumer 工程配置详解 Apach ...

  2. 图形界面备份Linux系统介绍

    详情见: http://os.51cto.com/art/200905/125680.htm             虽然Linux 是一个高度可靠的操作系统,但对于现在复杂的internet环境下, ...

  3. Mvc异步

    <h3>MVC 自带的yibu请求</h3> <%-- 1.要执行的方法,2.控制器,3.Ajax操作--%> <%using (Ajax.BeginForm ...

  4. 51Nod 1006 最长公共子序列Lcs问题 模板题

    给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的). 比如两个串为:   abcicba abdkscab   ab是两个串的子序列,abc也是,abca也是,其中abca是这两个 ...

  5. AIX 适配器

    1. 查看所有适配卡 lsdev -CHc adapter     2. 物理网卡适配卡 查看到物理网卡的个数与类型 lsdev -Cc adapter|grep ent   查看物理网卡具体插槽位( ...

  6. Linux-PS1变量详解

    1.PS1 要修改linux终端命令行颜色,我们需要用到PS1,PS1是Linux终端用户的一个环境变量,用来说明命令行提示符的设置.在终端输入命令:#set,即可在输出中找到关于PS1的定义如下: ...

  7. celery work logging 问题

    celery 的日志里只输出日志 不输入标准打印

  8. 最简单的基于FFmpeg的移动端例子:Android 视频转码器

    http://blog.csdn.net/leixiaohua1020/article/details/47056365

  9. [AngularFire] Resolve snapshotChanges doesn't emit value when data is empty

    Updated to AngularFire2 v5.0. One important change is that you need to call .snapshotChanges() or .v ...

  10. 使用Ant打包Android应用具体解释——Ant使用解析

    上篇<使用Ant打包Android应用具体解释>描写叙述了使用Ant打包的流程,但非常多步骤并没有说明如此做的原因,本篇将从Ant方面来理解,下一篇从APK生成的流程来说明. APK包的生 ...