POJ 2983 Is the Information Reliable? 信息可靠吗 (差分约束,spfa)
题意:有n个站排成一列,针对每个站的位置与距离关系,现有多个约束条件,约束条件分两种:(1)确定的。明确说明站a距离站b多少个单位距离。(2)不确定的。只知道a在b的左边至少1个单位距离。 根据已知条件,问有没有冲突?不冲突则输出reliable。
思路:
第2种条件比较好确定,如果知道如何用最短路解差分约束的话。
问题在第1种,明确地说明了距离,怎么办?拆成两条式子,比如 dis(a,b)=c,那么可以写成 b-a>=c ,b-a<=c 这样,只要满足这两个条件,原来明确说明的距离也会成立的。这样就可以根据两条式子建图了。再用spfa解就可以了。
- //#include <bits/stdc++.h>
- #include <iostream>
- #include <cstdio>
- #include <vector>
- #include <cstring>
- #include <deque>
- #define INF 0x7f7f7f7f
- #define pii pair<int,int>
- #define LL unsigned long long
- using namespace std;
- const int N=;
- struct node
- {
- int from, to, cost;
- node(){};
- node(int from,int to,int cost):from(from),to(to),cost(cost){};
- }edge[N*N];
- int edge_cnt;
- vector<int> vect[N];
- void add_node(int from,int to,int cost)
- {
- edge[edge_cnt]=node(from, to, cost);
- vect[from].push_back(edge_cnt++);
- }
- int inq[N], cost[N], cnt[N];
- bool spfa(int n)
- {
- memset(inq, , sizeof(inq));
- memset(cost, , sizeof(cost));
- memset(cnt, , sizeof(cnt));
- deque<int> que;
- for(int i=; i<=n; i++) que.push_back(i);
- while(!que.empty())
- {
- int x=que.front();
- que.pop_front();
- inq[x]=;
- for(int i=; i<vect[x].size(); i++)
- {
- node e=edge[vect[x][i]];
- if(cost[e.to]>cost[e.from]+e.cost)
- {
- cost[e.to]=cost[e.from]+e.cost;
- if(!inq[e.to])
- {
- inq[e.to]=;
- if(++cnt[e.to]>n) return false;
- if(!que.empty()&& cost[e.to]<cost[que.front()])
- que.push_front(e.to);
- else
- que.push_back(e.to);
- }
- }
- }
- }
- return true;
- }
- int main()
- {
- freopen("input.txt", "r", stdin);
- int t, a, b, d, n, m;
- char c;
- while(~scanf("%d%d", &n, &m))
- {
- edge_cnt=;
- for(int i=; i<=n; i++) vect[i].clear();
- memset(edge,,sizeof(edge));
- for(int i=; i<m; i++)
- {
- while(scanf("%c", &c), !isalpha(c) );
- if(c=='P')//确定的,要拆
- {
- scanf("%d %d %d", &a, &b, &d);
- add_node(a,b,d);
- add_node(b,a,-d);
- }
- else
- {
- scanf("%d %d", &a, &b);
- add_node(b,a,-);
- }
- }
- if(spfa(n)) puts("Reliable");
- else puts("Unreliable");
- }
- return ;
- }
AC代码
POJ 2983 Is the Information Reliable? 信息可靠吗 (差分约束,spfa)的更多相关文章
- 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 题解: 差分约束. 1).对于条件(P u v w),不难发现反映到图上就是: $dis[u]-dis[v]=w$,所以添加两条边 ...
- 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? 差分约束
裸差分约束. //#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #i ...
- POJ 1201 & HDU1384 & ZOJ 1508 Intervals(差分约束+spfa 求最长路径)
题目链接: POJ:http://poj.org/problem?id=1201 HDU:http://acm.hdu.edu.cn/showproblem.php? pid=1384 ZOJ:htt ...
- POJ 之 Is the Information Reliable?
B - Is the Information Reliable? Time Limit:3000MS Memory Limit:131072KB 64bit IO Format:%I6 ...
- poj Layout 差分约束+SPFA
题目链接:http://poj.org/problem?id=3169 很好的差分约束入门题目,自己刚看时学呢 代码: #include<iostream> #include<cst ...
- POJ 1364 / HDU 3666 【差分约束-SPFA】
POJ 1364 题解:最短路式子:d[v]<=d[u]+w 式子1:sum[a+b+1]−sum[a]>c — sum[a]<=sum[a+b+1]−c−1 ...
- POJ 3159 Candies(差分约束+spfa+链式前向星)
题目链接:http://poj.org/problem?id=3159 题目大意:给n个人派糖果,给出m组数据,每组数据包含A,B,C三个数,意思是A的糖果数比B少的个数不多于C,即B的糖果数 - A ...
随机推荐
- Python中编码问题?
一.键盘输入 raw_input('请输入:'.decode('utf-8').encode('gbk'))raw_input(unicode('请输入:','utf-8').encode('gbk' ...
- HDU4782 Beautiful Soup
成都赛里的一道坑爹码力题,突然间脑抽想做一下弥补一下当时的遗憾.当时没做出这道题一是因为当时只剩大概45分钟,对于这样的具有各种条件的题无从下手,二则是因为当时估算着已经有银牌了,所以就不挣扎了.但是 ...
- iOS生成本地随机验证码
原文链接:http://www.cnblogs.com/jerehedu/p/4527707.html 效果图:
- 怎样强制QQ聊天
首先复制下面这段网址: http://wp.qq.com/open_webaio.html?sigt=2d3bb7d31517da8c94a1061c6b63dd3203eb633805dcd09ec ...
- ***Xcode Interface Builder或Storyboard中可建立那两种连接?
在Xcode Interface Builder或Storyboard中,可建立到输出口(IBOutlet)和操作(方法,IBAction)的连接. IBOutlet are for output C ...
- Codeforces Round #336 (Div. 2) D. Zuma 区间dp
D. Zuma Genos recently installed the game Zuma on his phone. In Zuma there exists a line of n gems ...
- lintcode:背包问题
背包问题 在n个物品中挑选若干物品装入背包,最多能装多满?假设背包的大小为m,每个物品的大小为A[i] 样例 如果有4个物品[2, 3, 5, 7] 如果背包的大小为,可以选择的空间. 如果背包的大小 ...
- Android 核心分析之十三Android GWES之Android窗口管理
Android GWES之Android窗口管理1基本构架原理 Android的窗口管理是C/S模式的.Android中的Window是表示Top Level等顶级窗口的概念.DecorView是Wi ...
- mysql C api
1.初始化一个链接结构. 2.创建一个链接. 3.执行查询. 4.关闭链接. MYSQL* conn; 首先,声明一个conn指针指向一个MYSQL结构体,这个结构体就是一个数据库连接句柄. conn ...
- Linux SHELL脚本
在Linux系统中,虽然有各种各样的图形化接口工具,但是sell仍然是一个非常灵活的工具.Shell不仅仅是命令的收集,而且是一门非常棒的编程语言.可以通过使用shell使大量的任务自动化,shell ...