题意:有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. HDU 1260 Tickets(简单dp)

    Tickets Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  2. python--httplib模块使用

    httplib是一个相对底层的http请求模块,其上有专门的包装模块,如urllib内建模块,goto等第三方模块,但是封装的越高就越不灵 活,比如urllib模块里请求错误时就不会返回结果页的内容, ...

  3. 【zoj2562】反素数

    题意:给定一个数N,求小于等于N的所有数当中,约数最多的一个数,如果存在多个这样的数,输出其中最小的一个.(1 <= n <= 10^16) 题目:http://acm.hust.edu. ...

  4. ios开发--集成银联3.3.0

    项目最近需要集成银联,在网上搜了一下发现都并不是最新版的银联集成教程,自己摸索了一下,总结写了下来. 附上3.3.0的下载网址 https://open.unionpay.com/upload/dow ...

  5. Android 闹钟设置

    在Android中可以通过AlarmManager 来实现闹钟,AlarmManager类是专门用来设定在某个指定的时间去完成指定的事件.AlarmManager 提供了访问系统警报的服务,只要在程序 ...

  6. minhash

    minhash是一种基于jaccard index 相似度的算法.属于LSH(Location Sensitive Hash)家族中的一员. jaccard index :有两个集合A={a , b ...

  7. CF 2013-2014CTS01E04(Killer Challenge-将质因数存在 进行Bitmask)

    首先,把P进行质因数分解,每一个不用的质因数压成1位 f[i][j]表示1前i位用j“拥有”的质因数表示. 然后都懂得... #include<cstdio> #include<cs ...

  8. usb协议分析-设备描述符配置包-描述符

    /* usb协议分析仅供大家参考---设备描述符配置包,设备描述符, 地址设置, 配置描述符, 字符串描述符 */ /* -1- usb设备描述符配置包 */ typedef struct _USB_ ...

  9. Android之TelephonyManager类的使用案例

    TelephonyManager类主要提供了一系列用于访问与手机通讯相关的状态和信息的get方法.其中包括手机SIM的状态和信息.电信网络的状态及手机用户的信息.在应用程序中可以使用这些get方法获取 ...

  10. 运行所有sdk目录下的示例,查看它们的功能,方便以后查寻

    运行所有sdk目录下的示例,查看它们的功能,方便以后查寻