POJ 2983-Is the Information Reliable?(差分约束系统)
题目地址: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?(差分约束系统)的更多相关文章
- POJ 2983 Is the Information Reliable? 差分约束
裸差分约束. //#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #i ...
- POJ 2983 Is the Information Reliable?(差分约束系统)
http://poj.org/problem?id=2983 题意:N 个 defense stations,M条消息,消息有精确和模糊之分,若前边为P.则为精确消息,有两个defense stati ...
- 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 ...
随机推荐
- [luogu 4240] 毒瘤之神的考验
题目背景 Salamander的家门口是一条长长的公路. 又是一年春天将至,Salamander发现路边长出了一排毒瘤! Salamander想带一些毒瘤回家,但是,这时毒瘤当中钻出来了一个毒瘤之神! ...
- [洛谷P3857][TJOI2008]彩灯
题目大意:有$n$盏灯,$m$个开关($n,m\leqslant 50$),每个开关可以控制的灯用一串$OX$串表示,$O$表示可以控制(即按一下,灯的状态改变),$X$表示不可以控制,问有多少种灯的 ...
- A Dangerous Maze (II) LightOJ - 1395(概率dp)
A Dangerous Maze (II) LightOJ - 1395(概率dp) 这题是Light Oj 1027的加强版,1027那道是无记忆的. 题意: 有n扇门,每次你可以选择其中一扇.xi ...
- Codeforces Round #527 (Div. 3) ABCDEF题解
Codeforces Round #527 (Div. 3) 题解 题目总链接:https://codeforces.com/contest/1092 A. Uniform String 题意: 输入 ...
- Java正则表达式--Matcher.group函数的用法
原来,group是针对()来说的,group(0)就是指的整个串,group(1) 指的是第一个括号里的东西,group(2)指的第二个括号里的东西. 最近学习正则表达式,发现Java中的一些术语与其 ...
- HDU5037 Frog
Once upon a time, there is a little frog called Matt. One day, he came to a river. The river could b ...
- [转载]EasyUI中数据表格DataGrid添加排序功能
我们这里演示的是EasyUI数据表格DataGrid从服务器端排序功能,因为觉的本地数据排序没有多大的作用,一般我们DataGrid不会读取全部数据,只会读取当前页的数据,所以本地数据排序也只是对当前 ...
- Juce-强大的开源类库
介绍 Juce是一个完全围绕C++语言的类库,用来开发跨平台的应用程序. 完整的用doxgen生成的html形式的API手册可以在这里下到.或者可以从下载页面下载预编译的windows帮助文件. 想获 ...
- Appium+python自动化22-Appium Desktop【转载】
Appium Desktop 原滋原味的官方文档 Appium Desktop是一款用于Mac.Windows和Linux的开源应用,它提供了Appium自动化服务器在一个漂亮灵活的UI中的强大功能. ...
- centos7使用tomcat部署javaweb项目
1.下载二进制安装包 tomcat下载地址 2.下载jdk和配置环境变量 这里就不再多描述不清楚,可以 参考 我的yum安装的jdk 3.在centos7下安装 tomcat #切换到/u ...