POJ 2983 Is the Information Reliable?(差分约束系统)
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?(差分约束系统)的更多相关文章
- POJ 2983 Is the Information Reliable? 差分约束
裸差分约束. //#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #i ...
- POJ 2983-Is the Information Reliable?(差分约束系统)
题目地址:POJ 2983 题意:有N个车站.给出一些点的精确信息和模糊信息.精确信息给出两点的位置和距离.模糊信息给出两点的位置.但距离大于等于一.试确定是否全部的信息满足条件. 思路:事实上就是让 ...
- POJ 2983 Is the Information Reliable? 依旧差分约束
http://poj.org/problem?id=2983 题目大意: 星际大战开始了.你购买了情报,需要判断它的准确性.已知地方的根据地在由南向北排成一条直线.P A B X,表示A在B北面距离X ...
- POJ 2983 Is the Information Reliable? 信息可靠吗 (差分约束,spfa)
题意:有n个站排成一列,针对每个站的位置与距离关系,现有多个约束条件,约束条件分两种:(1)确定的.明确说明站a距离站b多少个单位距离.(2)不确定的.只知道a在b的左边至少1个单位距离. 根据已知 ...
- ●POJ 2983 Is the Information Reliable?
题链: http://poj.org/problem?id=2983 题解: 差分约束. 1).对于条件(P u v w),不难发现反映到图上就是: $dis[u]-dis[v]=w$,所以添加两条边 ...
- 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之间的 ...
- POJ 3159 Candies (图论,差分约束系统,最短路)
POJ 3159 Candies (图论,差分约束系统,最短路) Description During the kindergarten days, flymouse was the monitor ...
- 【POJ 1716】Integer Intervals(差分约束系统)
id=1716">[POJ 1716]Integer Intervals(差分约束系统) Integer Intervals Time Limit: 1000MS Memory L ...
- 【POJ 1275】 Cashier Employment(差分约束系统的建立和求解)
[POJ 1275] Cashier Employment(差分约束系统的建立和求解) Cashier Employment Time Limit: 1000MS Memory Limit: 10 ...
随机推荐
- 浅谈用java解析xml文档(二)
上一文中总结了dom解析xml文档的方式,本文开始总结使用SAX解析xml 的方式及它的优缺点! SAX(Simple API for XML),是指一种接口,或者一个软件包. 首先我们应该知道SAX ...
- 理解Java的引用对象
SoftReferenceWeakReference 的特性基本一致, 最大的区别在于 SoftReference会尽可能长的保留引用,不会在GC时就回收对象,而是直到JVM 内存不足时才会被回收(虚 ...
- cl: cannot open file 'kernel32.lib'
在测试 jni时, 使用 cl命令, 如 cl -I xx\jdk1.7.0_17\include -I xx\jdk1.7.0_17\include\win32 -LD HelloNative.c ...
- PHP 实现对象的持久层,数据库使用MySQL
http://www.xuebuyuan.com/1236808.html 心血来潮,做了一下PHP的对象到数据库的简单持久层. 不常用PHP,对PHP也不熟,关于PHP反射的大部分内容都是现学的. ...
- HTML招聘简历解析
使用 jsoup 对 HTML 文档进行解析和操作 Jsoup解析html简历与dom4j解析xml是一个道理:首先必须知道html的格式,不知道格式,无法解析.根据格式,再将需要的内容通过下面的方法 ...
- .net远程连接oracle数据库不用安装oracle客户端
asp.net远程连接oracle数据库不用安装oracle客户端的方法下面是asp.net连接远程Oracle数据库服务器步骤: 1.asp.net连接oracle服务器需要添加Sytem.Data ...
- 层叠上下文 Stacking Context
层叠上下文 Stacking Context 在CSS2.1规范中,每个盒模型的位置是三维的,分别是平面画布上的x轴,y轴以及表示层叠的z轴.对于每个html元素,都可以通过设置z-index属性来设 ...
- (CodeForces 510C) Fox And Names 拓扑排序
题目链接:http://codeforces.com/problemset/problem/510/C Fox Ciel is going to publish a paper on FOCS (Fo ...
- mysql innodb 数据打捞(四)innodb 簇不连续页扫描提取(试验)
一,用winhex把正常页有意做成不连续的两部分,把后8K向后移动4K,中间隔开4K,启动第一次扫描; 扫描结果是,没有提取到有效页面,但在输出目录生成两个文件:upper.pages和upper.l ...
- 九度OJ 1385 重建二叉树
题目地址:http://ac.jobdu.com/problem.php?pid=1385 题目描述: 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都 ...