学习笔记----差分约束系统 POJ 2983 Is the Information Reliable?

总结一下:如果一个系统由n个变量和m个约束条件组成,其中每个约束条件形如xj-xi<=bk(i,j∈[1,n],k∈[1,m]),则称其为差分约束系统(system of difference constraints)。亦即,差分约束系统是求解关于一组变量的特殊不等式组的方法。
求解差分约束系统,可以转化成图论的单源最短路径(或最长路径)问题。
观察xj-xi<=bk,会发现它类似最短路中的三角不等式d[v]<=d[u]+w[u,v],即d[v]-d[u]<=w[u,v]。因此,以每个变量xi为结点,对于约束条件xj-xi<=bk,连接一条边(i,j),边权为bk。我们再增加一个源点s,s与所有定点相连,边权均为0。对这个图,以s为源点运行Bellman-ford算法(或SPFA算法),最终{d[ i]}即为一组可行解。地址连接:http://baike.baidu.com/link?url=LJEcrGOn0qnPkqRurNp_cNIVspO5GWygEIY3QYoHTfrvfzYrrupjCR6TiE7nf8W21PWvIUjcTnXwmOA5T8VE0_

差分约束,我的理解就是根据条件进行逼近,如果条件越多越接近。裸的这些题目主要关键步骤就是找到他们之间的关系,列出他们之间的明确的关系,与一些隐含的条件。然后建立系统在求最短路径。

以POJ  2983 Is the Information Reliable?为例,我写一下自己的感受。

题目的大意是:给你n个点,然后给你m个条件让你确定在这m个条件下由n个点组成的最短路是不是存在负环,如果是的话,就说明这些情报是假的。否则是对的。P代表明确的情报下a,b的距离是确定的,V代表不明确的情报,此时a,b之间的距离至少是1.

分析一下:
如果情报不明确则有不等式:d[a]-d[b] >= 1<=> d[b] <= d[a]-1这样一个偏序关系就出来了。
如国情报明确择有:d[a]-d[b] = x;也可以写成是:d[a] >= x+d[b] && d[b] <= d[a]-x; 
所以就得到了这么一个偏序关系:d[b]<=d[a]-1 || d[b] <= d[a]-x;

注意处理的时候d[a]--->d[b] == x, d[b]---->d[a] == -x;
Is the Information Reliable?
Time Limit: 3000MS Memory Limit: 131072K
Total Submissions: 10264 Accepted: 3197

Description

The galaxy war between the Empire Draco and the Commonwealth of Zibu broke out 3 years ago. Draco established a line of defense called Grot. Grot is a straight line with Ndefense stations. Because of the cooperation of the stations, Zibu’s Marine Glory cannot march any further but stay outside the line.

A mystery Information Group X benefits form selling information to both sides of the war. Today you the administrator of Zibu’s Intelligence Department got a piece of information about Grot’s defense stations’ arrangement from Information Group X. Your task is to determine whether the information is reliable.

The information consists of M tips. Each tip is either precise or vague.

Precise tip is in the form of P A B X, means defense station A is X light-years north of defense station B.

Vague tip is in the form of V A B, means defense station A is in the north of defense station B, at least 1 light-year, but the precise distance is unknown.

Input

There are several test cases in the input. Each test case starts with two integers N (0 < N ≤ 1000) and M (1 ≤ M ≤ 100000).The next M line each describe a tip, either in precise form or vague form.

Output

Output one line for each test case in the input. Output “Reliable” if It is possible to arrange N defense stations satisfying all the M tips, otherwise output “Unreliable”.

Sample Input

3 4
P 1 2 1
P 2 3 1
V 1 3
P 1 3 1
5 5
V 1 2
V 2 3
V 3 4
V 4 5
V 3 5

Sample Output

Unreliable
Reliable
代码如下:
<pre class="html" name="code">#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <map>
#define M 100000
#define INF 1 << 30;

using namespace std;


struct node
{
    int u, v, w;
}f[1500000];

int d[10100];
int n, m;
int t, flat;

void bellman_ford()
{
    int i, j;
    for(i = 0; i < n; i++)
    {
        flat = 0;
        for(j = 0; j < t; j++)
        {
            if(d[f[j].u] > d[f[j].v]-f[j].w)
            {
                d[f[j].u] = d[f[j].v] - f[j].w;
                flat = 1;
            }
        }
        if(!flat)
            break;
    }
    if(flat)
        cout<<"Unreliable"<<endl;
    else
        cout<<"Reliable"<<endl;
    return;
}

int main()
{
    int i;
    char s;
    int a, b, x;
    while(cin>>n>>m)
    {
        t = 0;
        memset(d , 0 , sizeof(d));
        for(i = 0; i < m; i++)
        {
            getchar();
            scanf("%c",&s);
            if(s == 'V')
            {
                scanf("%d %d",&a, &b);
                f[t].u = a;
                f[t].v = b;
                f[t++].w = 1;
            }
            else if(s == 'P')
            {
                scanf("%d %d %d",&a, &b, &x);
                f[t].u = a;
                f[t].v = b;
                f[t++].w = x;
                f[t].u = b;
                f[t].v = a;
                f[t++].w = -x;
            }
        }
        bellman_ford();
    }
    return 0;
}

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值