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 ...
随机推荐
- 看得懂的区块链,看不清的ICO人心【转】
比特币又开始下跌了,是狂欢尽头还是又一波调整,无从得知,背后的乱象会让监管者继续心烦,而这乱象对我来说,有时候会有些心寒. 你说我怎么可能想到,我一个写程序的人,突然有一天会发现,朋友圈里有一些搞技术 ...
- Java的运算符及结合性
优先级 运算符 结合性 1 ()[]. 从左向右 2 !+(正)-(负)~ ++ -- 从右向左 3 * / % 从左向右 4 +(加)-(减) 从左向右 5 << >&g ...
- poj 1061 青蛙的约会 (扩展欧几里得模板)
青蛙的约会 Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submit Status ...
- [C++对象模型][8]多重继承与虚函数表
转载: [C++对象模型][8]多重继承与虚函数表 一 多重继承 1) 代码: Code #include <iostream> using namespace std; class B1 ...
- R语言绘制相对性关系图
准备 第一步就是安装R语言环境以及RStudio 图绘制准备 首先安装库文件,敲入指令,回车 install.packages('corrplot') 然后安装excel导入的插件,点击右上角impo ...
- How to Use Instruments in Xcode
http://blog.csdn.net/woaifen3344/article/details/40748075 This is a blog post by iOS Tutorial Team m ...
- 【原创】Linux环境下的图形系统和AMD R600显卡编程(8)——AMD显卡DRM驱动初始化过程
前面几个blog对DRM驱动.显卡的显存管理机制.中断机制都进行了一些描述,现在阅读AMD drm驱动的初始化过程应该会轻松许多. 下面是一AMD的开发人员编写的文章(先暂时放在这里,后续有时间再添加 ...
- ADO 右键问题,当dbgrideh非只读的时候,会遇到这个问题
/// <summary> /// 再选中列上右键造成,选中这列的状态变化的情况的修复 /// </summary> procedure TfrmMain.XuanYouJia ...
- git 本地与远程关联流程
git init git add -A git commit -m '提交' git remote add origin git@github.com:laniu/liuna.git git push ...
- 异步 JavaScript 之理解 macrotask 和 microtask(转)
这个知识点... https://blog.keifergu.me/2017/03/23/difference-between-javascript-macrotask-and-microtask/? ...