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

思路:

  第2种条件比较好确定,如果知道如何用最短路解差分约束的话。

  问题在第1种,明确地说明了距离,怎么办?拆成两条式子,比如 dis(a,b)=c,那么可以写成 b-a>=c ,b-a<=c 这样,只要满足这两个条件,原来明确说明的距离也会成立的。这样就可以根据两条式子建图了。再用spfa解就可以了。

 //#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <deque>
#define INF 0x7f7f7f7f
#define pii pair<int,int>
#define LL unsigned long long
using namespace std;
const int N=;
struct node
{
int from, to, cost;
node(){};
node(int from,int to,int cost):from(from),to(to),cost(cost){};
}edge[N*N];
int edge_cnt;
vector<int> vect[N]; void add_node(int from,int to,int cost)
{
edge[edge_cnt]=node(from, to, cost);
vect[from].push_back(edge_cnt++);
} int inq[N], cost[N], cnt[N]; bool spfa(int n)
{
memset(inq, , sizeof(inq));
memset(cost, , sizeof(cost));
memset(cnt, , sizeof(cnt));
deque<int> que;
for(int i=; i<=n; i++) que.push_back(i); while(!que.empty())
{
int x=que.front();
que.pop_front();
inq[x]=;
for(int i=; i<vect[x].size(); i++)
{
node e=edge[vect[x][i]];
if(cost[e.to]>cost[e.from]+e.cost)
{
cost[e.to]=cost[e.from]+e.cost;
if(!inq[e.to])
{
inq[e.to]=;
if(++cnt[e.to]>n) return false;
if(!que.empty()&& cost[e.to]<cost[que.front()])
que.push_front(e.to);
else
que.push_back(e.to);
}
}
}
}
return true;
} int main()
{
freopen("input.txt", "r", stdin);
int t, a, b, d, n, m;
char c;
while(~scanf("%d%d", &n, &m))
{
edge_cnt=;
for(int i=; i<=n; i++) vect[i].clear();
memset(edge,,sizeof(edge)); for(int i=; i<m; i++)
{
while(scanf("%c", &c), !isalpha(c) );
if(c=='P')//确定的,要拆
{
scanf("%d %d %d", &a, &b, &d);
add_node(a,b,d);
add_node(b,a,-d);
}
else
{
scanf("%d %d", &a, &b);
add_node(b,a,-);
}
}
if(spfa(n)) puts("Reliable");
else puts("Unreliable");
}
return ;
}

AC代码

POJ 2983 Is the Information Reliable? 信息可靠吗 (差分约束,spfa)的更多相关文章

  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?

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

  3. POJ 2983 Is the Information Reliable? 依旧差分约束

    http://poj.org/problem?id=2983 题目大意: 星际大战开始了.你购买了情报,需要判断它的准确性.已知地方的根据地在由南向北排成一条直线.P A B X,表示A在B北面距离X ...

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

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

  5. POJ 1201 &amp; HDU1384 &amp; ZOJ 1508 Intervals(差分约束+spfa 求最长路径)

    题目链接: POJ:http://poj.org/problem?id=1201 HDU:http://acm.hdu.edu.cn/showproblem.php? pid=1384 ZOJ:htt ...

  6. POJ 之 Is the Information Reliable?

    B - Is the Information Reliable? Time Limit:3000MS     Memory Limit:131072KB     64bit IO Format:%I6 ...

  7. poj Layout 差分约束+SPFA

    题目链接:http://poj.org/problem?id=3169 很好的差分约束入门题目,自己刚看时学呢 代码: #include<iostream> #include<cst ...

  8. POJ 1364 / HDU 3666 【差分约束-SPFA】

    POJ 1364 题解:最短路式子:d[v]<=d[u]+w 式子1:sum[a+b+1]−sum[a]>c      —      sum[a]<=sum[a+b+1]−c−1  ...

  9. POJ 3159 Candies(差分约束+spfa+链式前向星)

    题目链接:http://poj.org/problem?id=3159 题目大意:给n个人派糖果,给出m组数据,每组数据包含A,B,C三个数,意思是A的糖果数比B少的个数不多于C,即B的糖果数 - A ...

随机推荐

  1. 来认识下less css

    简介 感觉自己都out了,跟不上web时代的潮流了,前不久才刚接触这玩意,发觉lesscss在某些方面还挺有用的,说白了这东西就是一种动态的样式语言,语法类似于css,可以像java那样进行编译,生成 ...

  2. DB2学习

    1.为了使用ORG_ID,由于OCRM_F_MM_MKT_PLAN a 没有ORG_ID,但是OCRM_F_MM_EXECUTE_INST_DESC d 表里面有,可以使a表连接b表,创建临时表t. ...

  3. 未能正确加载“Microsoft.VisualStudio.Editor.Implementation.EditorPackage”

    VS2012启动/加载项目出问题 未能正确加载“Microsoft.VisualStudio.Editor.Implementation.EditorPackage, Microsoft.Visual ...

  4. Java集合框架(一)

    Java中集合类的关系图:  Collection 先来集合中的最大接口——Collection 可以通过查看JDK帮助文档,了解Collection接口中的最共性的方法.通过以下代码示例演示这些方法 ...

  5. 怎样在java代码中调用执行shell脚本

    // 用法:Runtime.getRuntime().exec("命令"); String shpath="/test/test.sh"; //程序路径 Pro ...

  6. ThreadLocal实现session中用户信息 的线程间共享(精)

    ThreadLocal并不难理解,我总结的最简单的理解就是: ThreadLocal像其它变量一样(局部.全局.静态)也是一种变量类型,只是他是线程变量,更直白的说他是一种变量作用域,即他的作用域是当 ...

  7. CentOS7安装配置DNS服务器

    准备工作(假设名称为bigcloud.local) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #更改主机名称 #vi /etc/sysconfig/netwo ...

  8. POJ3252——Round Number(组合数学)

    Round Numbers DescriptionThe cows, as you know, have no fingers or thumbs and thus are unable to pla ...

  9. logstash_agent.conf 语法注意事项

    编写配置文件时要注意语法,如新版本的logstash对参数host变更为hosts,去除了port参数等. [root@localhost logstash]# cat logstash_agent. ...

  10. PowerDesigner-自定义生成WORD

    PowerDesigner-自定义生成WORD_旧梦重温 分类: web 2014-02-26 21:08 1563人阅读 评论(48) 收藏 举报   目录(?)[+] 1统一建立模型 2导出自定义 ...