题目地址:POJ 2983

题意:有N个车站。给出一些点的精确信息和模糊信息。精确信息给出两点的位置和距离。模糊信息给出两点的位置。但距离大于等于一。试确定是否全部的信息满足条件。

思路:事实上就是让你推断是否存在负环。好久才看明确。对于精确消息。能够得出两个差分公式:dis[v] <= dist[u] - w  &&  dist[u] <= dist[v] + w。对于模糊信息。能够得出dis[v] <= dis[u] -1。

PS:做差分约束感觉还是Bellman_ford好用啊。

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
#include <map>
using namespace std;
typedef long long LL;
const int inf=0x3f3f3f3f;
const double pi= acos(-1.0);
const double esp=1e-6;
const int maxn=2010;
int dis[maxn],head[2010];
int cnt;
struct node
{
int u,v,w;
int next;
}edge[1000010];
void add(int u,int v,int w)
{
edge[cnt].u=u;
edge[cnt].v=v;
edge[cnt].w=w;
edge[cnt].next=head[u];
head[u]=cnt++;
}
int Bellman_ford(int n)
{
int i,j;
memset(dis,inf,sizeof(dis));
dis[1]=0;
for(i=1;i<=n;i++){
int flag=0;
for(j=0;j<cnt;j++){
int u=edge[j].u;
int v=edge[j].v;
if(dis[v]>dis[u]+edge[j].w){
dis[v]=dis[u]+edge[j].w;
flag=1;
}
}
if(!flag) break;
}
for(i=0;i<cnt;i++){
if(dis[edge[i].v]>dis[edge[i].u]+edge[i].w)
return 0;
}
return 1;
}
int main()
{
int n,m,i;
int u,v,w;
char str;
while(~scanf("%d %d",&n,&m)){
memset(head,-1,sizeof(head));
cnt=0;
while(m--){
getchar();
scanf("%c",&str);
if(str=='P'){
scanf("%d %d %d",&u,&v,&w);
add(u,v,w);
add(v,u,-w);
}
else{
scanf("%d %d",&u,&v);
add(v,u,-1);
}
}
int ans=Bellman_ford(n);
if(ans)
puts("Reliable");
else
puts("Unreliable");
}
return 0;
}

POJ 2983-Is the Information Reliable?(差分约束系统)的更多相关文章

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

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

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

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

  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? 信息可靠吗 (差分约束,spfa)

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

  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?(差分约束系统)

    http://poj.org/problem?id=2983 题意:给出M条信息,判断这些信息的正确性.(1)V A B :表示A,B之间的距离>=1; (2)P A B X :表示A B之间的 ...

  7. POJ 3159 Candies (图论,差分约束系统,最短路)

    POJ 3159 Candies (图论,差分约束系统,最短路) Description During the kindergarten days, flymouse was the monitor ...

  8. 【POJ 1716】Integer Intervals(差分约束系统)

    id=1716">[POJ 1716]Integer Intervals(差分约束系统) Integer Intervals Time Limit: 1000MS   Memory L ...

  9. 【POJ 1275】 Cashier Employment(差分约束系统的建立和求解)

    [POJ 1275] Cashier Employment(差分约束系统的建立和求解) Cashier Employment Time Limit: 1000MS   Memory Limit: 10 ...

随机推荐

  1. hihoCoder 1403 后缀数组一·重复旋律(后缀数组+单调队列)

    #1403 : 后缀数组一·重复旋律 时间限制:5000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi平时的一大兴趣爱好就是演奏钢琴.我们知道一个音乐旋律被表示为长度为 N 的数构成 ...

  2. Codeforces 165E Compatible Numbers(二进制+逆序枚举)

    E. Compatible Numbers time limit per test 4 seconds memory limit per test 256 megabytes input standa ...

  3. 洛谷 P2114 [NOI2014]起床困难综合症 解题报告

    P2114 [NOI2014]起床困难综合症 题目描述 21世纪,许多人得了一种奇怪的病:起床困难综合症,其临床表现为:起床难,起床后精神不佳.作为一名青春阳光好少年,atm一直坚持与起床困难综合症作 ...

  4. 洛谷 P1136 迎接仪式 解题报告

    P1136 迎接仪式 题目描述 LHX教主要来X市指导OI学习工作了.为了迎接教主,在一条道路旁,一群Orz教主er穿着文化衫站在道路两旁迎接教主,每件文化衫上都印着大字.一旁的Orzer依次摆出&q ...

  5. nginx支持pathinfo

    server { root /webserver/www/api; listen ; server_name api.dnxia.com; location / { if (!-e $request_ ...

  6. codeforce 570 problem E&& 51Nod-1503-猪和回文

    1503 猪和回文 一只猪走进了一个森林.很凑巧的是,这个森林的形状是长方形的,有n行,m列组成.我们把这个长方形的行从上到下标记为1到n,列从左到右标记为1到m.处于第r行第c列的格子用(r,c)表 ...

  7. pdf.js如何跨域读取pdf文件?

    今天,上线一个客户网站之后(使用的是广州新一代虚拟空间)发现在读取上传的pdf文件的时候读取错误,通过直接在浏览器输入文件地址的时候发现文件地址被重定向了(呵呵!),结果就是pdf文件源由本地直接变成 ...

  8. 和菜鸟一起学linux之V4L2摄像头应用流程【转】

    转自:http://blog.csdn.net/eastmoon502136/article/details/8190262/ 上篇文章,知道了,C代码编译后存放在内存中的位置,那么C代码的整个编译过 ...

  9. Centos 查看硬盘空间

    df -h du -ah --max-depth=1 目录深度为1 -h:以人类可读的方式显示 -a:显示目录占用的磁盘空间大小,还要显示其下目录和文件占用磁盘空间的大小 -s:显示目录占用的磁盘空间 ...

  10. [jquery] 给动态生成的元素绑定事件 on方法

    用底下的方法尝试了好多次都失败 $('.del').on('click',function(){ alert('aa'); })// 失败!! 终于在准备放弃前看到一篇博文说的方法 $(documen ...