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 ...
随机推荐
- h5滚动条加载到底部
https://www.zhihu.com/question/31861301 重复加载问题 http://www.jianshu.com/p/12aa901bee1f?from=timeline w ...
- java io 网络编程 高性能NIO
很久没写了,一是觉得这后台不怎么方便,二是 写的时候突然觉得没兴趣了. 还好,今天突然想记一下,那就随便写吧. 1.一开始还是放几个连接. 什么是 同步,异步,阻塞,非阻塞 : http://bl ...
- 【CF1020D】The hat(交互,二分)
题意:有n个人围成一个圈,n为偶数,每个人有一个数字a[i],保证相邻两个人的数字差为1 最多可以询问60次,要求获得一个i使得a[i]=a[i+n/2] n<=1e5,abs(a[i])< ...
- weblogic12c配置免密码启动
在运行startWeblogic.sh时需要输入有效的账号密码才能启动weblogic,为简化操作,可以配置boot.properties来免输账号密码,配置方法如下:1.查看在./domains/x ...
- datatable to list 方法转换
#region 实体转换 /// <summary> /// add extension method for datable /// </summary> /// <p ...
- windows 自动关机
按WIN+R在窗口中输入:shutdown -S -T 3600 然后回车.即可在一小时后关机,如果你想在半个小时后关机,那么就把3600改成1800单位是秒
- HTTP GET与POST区别
HTTP定义了与服务器交互的不同方法,最基本的方法是 GET 和 POST. HTTP-GET和HTTP-POST是使用HTTP的标准协议动词,用于编码和传送变量名/变量值对参数,并且使用相关的请求语 ...
- 【linux高级程序设计】(第十三章)Linux Socket网络编程基础 3
使用之前的函数实现的简单聊天程序 TCP协议 双方实时发送/接收消息 实现后的问题: 可能是我虚拟机的IP地址配得有问题吧.在一台电脑上面开两个终端,用127.0.0.1的IP收发可以互通.但是两个虚 ...
- hdu 5109(构造数+对取模的理解程度)
Alexandra and A*B Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Jav ...
- war导出问题myeclpise
内附破解文件以及myeclipse10.7.1导出war问题解决办法一.原作者的PJ程序在myeclipse10.7.1环境下测试通过(win7 x64) 按照步骤操作就可以完成PJ过程.PJ前先将c ...