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之间的距离为x。
思路:dis[i]表示i到原点的距离,由(1)知 dis[A]<=dis[B]-1 即B->A之间有一条边,权值为-1;由(2)知: dis[A]<=dis[B]-x && dis[B] <= dis[A]+x,即A->B的权值为-x,B->A的权值为x。增加一个超级源点,与所有的点相连且权值为0.建图,spfa判断是否有负环(即判断进站次数,若大于点数则存在负环)。若存在负环则信息为假,否则为真。
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
const int N=;
const int INF=<<;
int head[],vis[];
int dis[N],num[];
int cnt,n; struct node
{
int u,v,w;
int next;
} edge[N];
void init()
{
cnt = ;
memset(head,-,sizeof(head));
memset(vis,,sizeof(vis));
memset(num,,sizeof(num));
}
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 spfa()
{
queue<int>q;
for (int i = ; i <= n; i ++)
dis[i] = INF;
dis[] = ;
q.push();
vis[] = ;
while(!q.empty())
{
int u = q.front();
vis[u] = ;
q.pop();
for (int j = head[u]; j!=-; j=edge[j].next)
{
int v = edge[j].v;
int w = edge[j].w;
if (dis[v] > dis[u]+w)
{
dis[v] = dis[u]+w;
if (!vis[v])
{
q.push(v);
vis[v] = ;
++num[v];
if (num[v] > n)
return ;
}
}
}
}
return ;
}
int main()
{
int m,u,v,w;
char s[];
while(~scanf("%d%d",&n,&m))
{
init();
for (int i = ; i <= n; i++)
add(,i,);
for (int i = ; i < m; i++)
{
scanf("%s",s);
if (s[]=='P')
{
scanf("%d %d %d",&u,&v,&w);
add(u,v,w);
add(v,u,-w);
}
if (s[]=='V')
{
scanf("%d %d",&u,&v);
add(v,u,-);
}
}
if (spfa())
printf("Reliable\n");
else
printf("Unreliable\n");
}
return ;
}
Is the Information Reliable?(差分约束系统)的更多相关文章
- POJ 2983-Is the Information Reliable?(差分约束系统)
题目地址:POJ 2983 题意:有N个车站.给出一些点的精确信息和模糊信息.精确信息给出两点的位置和距离.模糊信息给出两点的位置.但距离大于等于一.试确定是否全部的信息满足条件. 思路:事实上就是让 ...
- POJ 2983 Is the Information Reliable? 差分约束
裸差分约束. //#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #i ...
- 【POJ 2983】Is the Information Reliable?(差分约束系统)
id=2983">[POJ 2983]Is the Information Reliable? (差分约束系统) Is the Information Reliable? Time L ...
- poj2983--Is the Information Reliable?(差分约束)
Is the Information Reliable? Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 11125 A ...
- Burn the Linked Camp(bellman 差分约束系统)
Burn the Linked Camp Time Limit: 2 Seconds Memory Limit: 65536 KB It is well known that, in the ...
- POJ2983 Is the Information Reliable?
http://acm.sdut.edu.cn:8080/vjudge/contest/view.action?cid=267#problem/B B - ...
- 【POJ 2983】 Is the information reliable?
[题目链接] 点击打开链接 [算法] 差分约束系统,SPFA判负环 [代码] #include <algorithm> #include <bitset> #include & ...
- UVA11478 Halum [差分约束系统]
https://vjudge.net/problem/UVA-11478 给定一个有向图,每条边都有一个权值.每次你可以选择一个结点v和一个整数d,把所有以v为终点的边的权值减小d,把所有以v为起点的 ...
- BZOJ 2330: [SCOI2011]糖果 [差分约束系统] 【学习笔记】
2330: [SCOI2011]糖果 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 5395 Solved: 1750[Submit][Status ...
随机推荐
- linux初步学习有感
经过了一段时间对linux的接触,从最开始接触到的deepin到后来我最喜欢的KaliLinux,感受到了这个我曾经并不了解的操作系统的独特魅力. 我是到了大学才知道linux这个系统的,但是在小时候 ...
- 深入理解PHP之foreach
招聘 标签(空格分隔): 招聘 PHP 国贸 语言基础 foreach 语法结构提供了遍历数组的简单方式. php5之前, foreach仅能用于数组php5+, 利用foreach可以遍历对象 fo ...
- Python爬虫入门教程: 半次元COS图爬取
半次元COS图爬取-写在前面 今天在浏览网站的时候,忽然一个莫名的链接指引着我跳转到了半次元网站 https://bcy.net/ 打开之后,发现也没有什么有意思的内容,职业的敏感让我瞬间联想到了 c ...
- 在此计算机中仅有部分visual studio2010产品已升级到SP1,只有全部升级,产品才能正常运行
先说废话: 本人机子刚装系统Win10 专业版 1709 开始安装vs2010的时候中途报错了,有一个什么驱动不兼容,被我给关闭了,继续安装完,然后找不到vs的启动快捷方式,开始里面没有,于是我开始修 ...
- linux下git+github个人使用记录
Linux: 安装git的命令: sudo apt install git 查看版本确认安装成功: git --version 生成密钥: ssh-keygen -t rsa -C "you ...
- Codeforces Round #239(Div. 2) 做后扯淡玩
今天补了下 cf 239div2 顿时信心再度受挫 老子几乎已经木有时间了啊 坐着等死的命.哎!!! 到现在还只能做大众题,打铁都不行. 每次D题都是有思路敲错,尼玛不带这么坑爹的. 哎!不写了,写这 ...
- poj 2114 树的分治 可作模板
/* 啊啊啊啊啊啊啊本题证明一个问题,在实际应用中sort比qsort块 还有memset这类初始化能不加尽量别加,很浪费时间 原来的程序把qsort该成sort,去掉一个无用memset就a了时间不 ...
- codevs——1675 大质数 2
1675 大质数 2 时间限制: 1 s 空间限制: 1000 KB 题目等级 : 钻石 Diamond 题解 题目描述 Description 小明因为没做作业而被数学老师罚站,之 ...
- 玲珑杯 ACM Round #12
A =w= B 占坑 C 题意:有长度为n的序列A和长度为n的序列W,以及一个G,对于Ui,1<=Ui<=Wi,求Σgcd(Ai,Ui)=G的方案数,n<=1e3,Ai<=1e ...
- 链表中倒数第N个元素——剑指Offer
https://www.nowcoder.net/practice/529d3ae5a407492994ad2a246518148a?tpId=13&tqId=11167&tPage= ...