题意:有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. FZU2165 v11(带权的重复覆盖)

    题意:有n个boss,m种武器,每种武器选用的时候需要有一定的花费ci,然后这个武器可以消灭掉其中一些BOSS,问你消灭完所有的BOSS,需要的最少花费是多少. 当时比赛的时候,看到这题以为是什么网络 ...

  2. Hungary(匈牙利算法)——二分图最大匹配

    在复习匈牙利算法的时候,发现这么一篇介绍匈牙利算法的文章,非常通俗易懂,所以就借鉴过来了. 复杂度:邻接矩阵:O(v^3)邻接表:O(V*E) 附上链接:趣写算法系列之--匈牙利算法 下面就附上代码吧 ...

  3. jQuery学习记录1

    jquery 和 js css里面都是坑呀 this.style.backgroundColor 和 css {background:#8df;} 是冲突的,用了前者,再$(this).addClas ...

  4. Android 注入详解

    Android下的注入的效果是类似于Windows下的dll注入,关于Windows下面的注入可以参考这篇文章Windows注入术.而Android一般处理器是arm架构,内核是基于linux,因此进 ...

  5. java编译做了哪些事?

    Javac编译器,主要做了如下的事情:1.解析与填充符号表:    2.注解处理器:    3.语义分析与字节码生成:        3.1.标注检查        3.2.数据及控制流分析      ...

  6. No ResultSet was produced

    遇到的详细问题: 出现了No ResultSet was produced的异常,但数据是成功插入, 大致判断异常发生在执行插入操作后,检查代码. 解决方案: 通常在executeQuery(sql) ...

  7. KETTLE、spoon使用

    ETL是Extract”.“ Transform” .“Load”三个单词的首字母缩写分别代表了抽取.转换.装载.是数据仓库中重要的一环.ETL是数据的抽取清洗转换加载的过程,是数据进入数据仓库进行大 ...

  8. 《jQuery风暴》第2章 必须知道的JavaScript知识

    第2章 必须知道的JavaScript知识 JavaScript是jQuery应用的基础,掌握JavaScript这门语言是使用jQuery的基础条件.本章不会全面细致的讲解JavaScript的全部 ...

  9. JavaWeb笔记——注册登录系统项目思路

    功能:   > 注册   > 登录 --------------------------------- JSP:   * login.jsp  --> 登录表单   * regist ...

  10. C语言:指针的几种形式二

    一.const指针 1.const int* p和int const* p:两者意义是相同的.指向的内容是只读数据,不可以q改变:但是指向的地址可以改变. 2.int* const p:必须先对指针初 ...