poj2983
来源:互联网 发布:mysql函数与存储过程 编辑:程序博客网 时间:2024/05/17 02:50
两张图片足矣说明问题。
任何形如上述约束条件的取值问题均可转化为差分约束问题。
重点:约束图的建立。
在Ax<=B的系统中,将m*n的线性规划矩阵A看做是n个结点和m条边的图的邻接矩阵。上图中,每个节点的值是delta(v0,vi),也就是v0到vi的最短路的值。
详细来说:G(V,E),是一个带权重的有向图,其中
V={v0,v1,v2,...,vn};
E={(vi,vj):xj-xi<=bk}U{(v0,v1),(v0,v2),...(v0,vn)}.
那么,
1.图中自然就包含一个v0,用于保证图中至少含有一个点(下面代码的超级源点)。
2.边集E包含的是每个差分约束的边,再加上边(v0,vi),i=1,2,3,...,n。如果xj-xi<=bk是一个差分约束条件,则边(vi,vj)的权重是bk.所有从v0出发的边权重是0。
该题:
所谓 P A B X 可以转换为
A-B>=x && A-B<=X <=> B-A <= -X && A-B <=X
而 V A B <=> A>=B+1 <=> B + 1 <=A <=> B - A <= -1。
然后套spfa即可了。
#include <iostream>#include <algorithm>#include <vector>#include <queue>#define MAXN 2000#define MAX 999999999using namespace std;int n, m;struct Tnode {int id, len;Tnode(int a, int b) : id(a), len(b) {}};vector <Tnode> adj[MAXN];int dis[MAXN], vis[MAXN];bool hashtable[MAXN];queue <int> q;bool spfa() {memset(hashtable, false, sizeof(hashtable));memset(vis, 0, sizeof(vis));dis[0] = 0;while (!q.empty()) q.pop(); q.push(0);while (!q.empty()) {int now = q.front();q.pop();hashtable[now] = false;for (int i = 0; i < adj[now].size(); i++) {int v = adj[now][i].id;int cost = adj[now][i].len;if (dis[v] > dis[now] + cost) {dis[v] = dis[now] + cost;if (!hashtable[v]) {q.push(v);hashtable[v] = true;}if (++vis[v] >= n) return false;}}}return true;}int main() {while (cin>>n>>m) {for (int i = 0; i <= n; i++) {adj[i].clear();dis[i] = MAX;}int a, b, c;char ch;for (int i = 0; i < m; i++) {cin>>ch;if (ch == 'P') {cin>>a>>b>>c;adj[b].push_back(Tnode(a, c));adj[a].push_back(Tnode(b, -c));}else {cin>>a>>b;adj[a].push_back(Tnode(b, -1));}}for (int i = 0; i <= n; i++) adj[0].push_back(Tnode(i, 0));if (!spfa()) cout << "Unreliable" << endl;else cout << "Reliable" << endl;}}
1 0
- poj2983
- poj2983&&poj1364
- poj2983(查分约束)
- poj2983 Is the Information Reliable?
- poj2983 - Is the Information Reliable?
- POJ2983 Is the Information Reliable?
- poj2983 Is the Information Reliable?
- poj2983 Is the Information Reliable?
- poj2983 Is the Information Reliable
- POJ2983 查分约束系统
- poj2983 Is the Information Reliable?
- poj2983 Is the Information Reliable?
- 【poj2983】 Is the Information Reliable?
- poj2983 Is the Information Reliable?
- [POJ1201]Intervals && [POJ2983]Is the Information Reliable
- POJ2983——Is the Information Reliable?
- poj2983(差分约束系统)
- poj2983 差分约束 超级源点
- arm-linux-gcc编译qt库
- 阿里正逢多事之秋,最近有点烦
- 读书笔记--盗梦工厂--2014年02月25日
- ant编译Tomcat8时报错 the archive file.tar.gz doesn't exist
- SAP ABAP ALV(LVC)的一个自定义事件(F4帮助事件,回车ENTER按钮事件)的一个实例
- poj2983
- java中package与import
- jar包冲突问题排查总结
- 这样的爱不算爱
- 在登录oracle中遇到的问题
- android实现共享数据
- 计算机操作系统——设备管理(6)
- Java 7之集合类型 - 二叉排序树、平衡树、红黑树
- JQ开源框架列表