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 ...
随机推荐
- vue的路由配置
路由,其实就是指向的意思,当我点击页面上的home按钮时,页面中就要显示home的内容,如果点击页面上的about 按钮,页面中就要显示about 的内容.Home按钮 => home 内容, ...
- jQuery图片延迟加载插件jQuery.lazyload 的使用
使用方法 引用jquery和jquery.lazyload.js到你的页面 1 2 <script src="jquery-1.11.0.min.js"></sc ...
- JavaScript--小白入门篇1
一.JavaScript简介 1.1 JavaScript的用途 JavaScript用来制作web页面交互效果,提升用户体验. 简单列出几个JavaScript能够制作的页面效果,它能干什 ...
- 最高的奖励 - 优先队列&贪心 / 并查集
题目地址:http://www.51cpc.com/web/problem.php?id=1587 Summarize: 优先队列&贪心: 1. 按价值最高排序,价值相同则按完成时间越晚为先: ...
- python黑科技库:FuckIt.py,让你代码从此远离bug
今天给你推荐的这个库叫 “FuckIt.py”,名字一看就是很黄很暴力的那种,作者是这样介绍它的: FuckIt.py uses state-of-the-art technology to make ...
- Leetcode 132.分割回文串II
分割回文串 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回符合要求的最少分割次数. 示例: 输入: "aab" 输出: 1 解释: 进行一次分割就可将 s ...
- Butterfly
Butterfly 时间限制:C/C++ 2秒,其他语言4秒空间限制:C/C++ 131072K,其他语言262144K64bit IO Format: %lld 题目描述 给定一个n*m的矩阵,矩阵 ...
- 常州模拟赛d8t2 绘画
分析:考虑记录每个坐标上每个颜色出现了几次,并由此算出每个颜色在这个坐标上的贡献.答案肯定是原图的答案扣去矩形的答案,再加上那个矩形同种颜色的贡献,这里的答案指的是Σdis.我们先要记录每个颜色在各个 ...
- ckeditor 设置含有html标签的值
ckeditor 设置含有html标签的值 需要使用ajax请求拿到那个字符串,然后用editor.setData(text);
- centos 如何安装软件
yum命令 (一)yum基本概念 1)yum是什么 yum = Yellow dog Updater, Modified 主要功能是更方便的添加/删除/更新RPM包. 它能自动解决包的倚赖性问题. 它 ...