题目地址: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. freebsd网卡驱动程序详解

    freebsd网卡驱动程序详解 来源 https://blog.csdn.net/h_cszc/article/details/7776116 /* 注释:xie_minix */ /*此处为BSD申 ...

  2. BZOJ2435 [Noi2011]道路修建 【树形Dp 吧。。】

    题目 在 W 星球上有 n 个国家.为了各自国家的经济发展,他们决定在各个国家 之间建设双向道路使得国家之间连通.但是每个国家的国王都很吝啬,他们只愿 意修建恰好 n – 1条双向道路. 每条道路的修 ...

  3. if else以及大于、小于、等于逻辑表达式

    大多数情况下,可以使用测试命令来对条件进行测试.比如可以比较字符串.判断文件是否存在及是否可读等,通常用"[]"来表示条件测试.注意这里的空格很重要.要确保方括号的空格. if . ...

  4. Pandas之DataFrame——Part 3

    ''' [课程2.] 数值计算和统计基础 常用数学.统计方法 ''' # 基本参数:axis.skipna import numpy as np import pandas as pd df = pd ...

  5. 服务器 阿里云服务器Ubuntu挂载数据盘

    服务器 阿里云服务器Ubuntu挂载数据盘  转自:http://www.codingyun.com/article/24.html coding云运行在阿里云的Ubuntu 12.04 64位操作系 ...

  6. 51nod 1583 犯罪计划——矩阵乘法优化dp

    文泽想在埃及做案n次,并且想在最后不用得到惩罚.案件的被分成几种类型.比如说,案件A,当案件A被重复犯两次时,案件A将被认为不是犯罪案件,因此犯案人不用得到惩罚.也就是说,案件A被犯偶数次时,犯案人将 ...

  7. 货车运输(LCA+最大生成树)

    神奇传送门 恩,这是一道神奇的LCA+难度的题目. 题目是这样的: A 国有n座城市,编号从1到n,城市之间有 m条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 q辆货车在运输货物,司机们 ...

  8. android hook 框架 xposed 如何实现挂钩

    Android so注入-libinject2 简介.编译.运行 Android so注入-libinject2  如何实现so注入 Android so注入-Libinject 如何实现so注入 A ...

  9. UVALIVE 3972 March of the Penguins

    最大流建图比较容易第一次Dicnc抄了下别人的版 存一下以后方便查 #include <map> #include <set> #include <list> #i ...

  10. Webpack & The Hot Module Replacement热模块替换原理解析

    Webpack & The Hot Module Replacement热模块替换原理解析 The Hot Module Replacement(HMR)俗称热模块替换.主要用来当代码产生变化 ...