http://poj.org/problem?id=2983

题意:N 个 defense stations,M条消息,消息有精确和模糊之分,若前边为P。则为精确消息,有两个defense stations和他们之间的距离,若为P A B X 则是精确消息,并且A在B北边X光年远,V A B则是模糊消息,A在B北边,不知道距离,但至少是1光年远。

思路 :这个题要转化成差分约束。因为dist[a]-dist[b] = x,所以转化成差分约束x<=dist[a]-dist[b]<=x ,模糊消息因为至少为1,所以转化后为 dist[b] - dist[a] >= 1,化简之后为 dist[b] - dist[a] <= x    dist[a] - dist[b] <= -x    dist[a] - dist[b]  <= -1.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream> using namespace std ;
const int INF = ;
int cnt,m ,n;
int dist[] ;//源点到各点的距离 struct node
{
int u,v,w ;
}edge[] ; void addedge(int u,int v,int w)//加边
{
edge[cnt].u = u ;
edge[cnt].v = v ;
edge[cnt].w = w ;
cnt++ ;
} bool bellman_ford(int start)
{
for(int i = ; i <= n ; i++)
i == start ? dist[i] = : dist[i] = INF ;
for(int i = ; i < n ; i++)
{
bool flag = false ;
for(int j = ; j < cnt ; j++)
{
int u = edge[j].u,v = edge[j].v,w = edge[j].w ;
if(dist[v] > dist[u] + w)
{
dist[v] = dist[u] + w ;
flag = true ;
}
}
if(!flag)//若dist没有任何改变说明以后也不会变,可直接返回
return true ;
}
for(int j = ; j < cnt ; j++)
{
int u = edge[j].u,v = edge[j].v,w = edge[j].w ;
if(dist[v] > dist[u] + w)
return false ;
}
return true ;
} int main()
{
char ch ;
int u,v,w ;
while(~scanf("%d %d",&n,&m))
{
cnt = ;
for(int i = ; i < m ; i++)
{
getchar() ;
scanf("%c",&ch) ;
if(ch == 'P')
{
scanf("%d %d %d",&u,&v ,&w) ;//两点距离确定建立双向边
addedge(v,u,-w) ;
addedge(u,v,w) ;
}
if(ch == 'V')
{
scanf("%d %d",&u,&v) ;//两点距离不定,建立单向边
addedge(v,u,-) ;
}
}
if(bellman_ford())
printf("Reliable\n") ;
else printf("Unreliable\n") ;
}
return ;
}

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

    题目地址:POJ 2983 题意:有N个车站.给出一些点的精确信息和模糊信息.精确信息给出两点的位置和距离.模糊信息给出两点的位置.但距离大于等于一.试确定是否全部的信息满足条件. 思路:事实上就是让 ...

  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. Apple Watch: WatchKit 应用程序要点

    Apple Watch: WatchKit 应用程序要点 本文译自:Apple Watch: WatchKit App Essentials WatchKit 应用程序架构 上一篇文章简单介绍了 Wa ...

  2. php中的作用域

    在php中分为局部变量.全局变量和静态变量: 局部变量就是在函数体内声明的变量,例子: <?php  //作用域  $a=5;  function show($b){   $a=$b;//相当于 ...

  3. vc静态加载dll和动态加载dll

    如果你有a.dll和a.lib,两个文件都有的话可以用静态加载的方式: message函数的声明你应该知道吧,把它的声明和下面的语句写到一个头文件中 #pragma comment(lib, &quo ...

  4. C# IO操作(二)File类和Directory类的常用方法

    本篇主要介绍一些常用的IO操作,对文件和目录的操作:留给自己复习之用. 1.创建文件 string sPath1=Path.GetDirectoryName(Assembly.GetExecuting ...

  5. ###学习《C++ Primer》- 2

    点击查看Evernote原文. #@author: gr #@date: 2014-10-01 #@email: forgerui@gmail.com Part 2: STL顺序容器(第9章) 一.标 ...

  6. 几个动画demo

    一.点击扩散效果 这个效果没什么难度,主要是加深对核心动画的理解和使用,但是还是有几个想说明的地方.先看一下效果,具体内容代码里有注释. // // CircleButton.m // UITest ...

  7. StrHelper

    public class StrHelper { private static string passWord; //加密字符串 /// <summary> /// 判断输入是否数字 // ...

  8. Java语言----三种循环语句的区别

    ------- android培训.java培训.期待与您交流! ---------- 第一种:for循环 循环结构for语句的格式:       for(初始化表达式;条件表达式;循环后的操作表达式 ...

  9. vbscript multiple line syntax

    Vbscript 如何将输出内容换行? ' VbCrLf represetns Carriage return–linefeed combination, for more information s ...

  10. C# Linq简介

    LInq是Language Integrated Query的简称,它是微软在.net framework 3.5里面新加入的特性,用以简化查询查询操作.它主要包含了3块,Linq to Object ...