Layout---poj3169(差分约束+最短路spfa)

题目链接:http://poj.org/problem?id=3169

有n头牛站成一排 在他们之间有一些牛的关系比较好,所以彼此之间的距离不超过一定距离;也有一些关系不好的牛,希望彼此之间的距离大于等于一定距离;

关系好的有ml个(A B D)表示A牛和B牛之间的距离<=D 关系不好的有md个(A,B,D)表示A牛和B牛之间的距离>=D,求在满足条件的排列中,求出牛1和牛n的最大距离;

如果距离可以是无限大输出-2,如果不存在这样的排列输出-1;

我们用d[i]表示第i头牛的位置,因为牛是按照编号排列顺序的,

所以         d[i]-d[i-1]>=0; --------- d[i-1]-d[i]<=0;(i到i-1的距离是0)

关系好的     d[B]-d[A] <= C; --------- d[B]-d[A]<=C;(A到B的距离是C)

关系不好的  d[B]-d[a]>=C; ----------- d[A]-d[B]<=-C;(B到A的距离是-C)

我们要求是的d[n]-d[1]<=x中的x;

刚好满足差分约束系统,所以直接建图求1-n的最短路即可;如果存在负环则不存在这样的序列,用spfa判断负环,如果距离为无穷大说明可以是任意值,

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <queue>
#include <stack>
#include <algorithm>
#include <map>
#include <string>
typedef long long LL;
#define INF 0x3f3f3f3f
#define met(a, b) memset(a, b, sizeof(a))
#define N 1500

using namespace std;

int n, md, ml, G[N][N], vis[N], dist[N], cnt[N];

int spfa(int s)
{
    for(int i=1; i<=n; i++)
        dist[i] = INF;
    met(vis, 0);
    met(cnt, 0);
    queue<int>Q;
    Q.push(s);
    vis[s] = 1;
    dist[s] = 0;
    cnt[s]++;
    while(!Q.empty())
    {
        int p = Q.front();Q.pop();
        vis[p] = 0;
        cnt[p] ++;
        for(int i=1; i<=n; i++)
        {
            if(dist[i]>dist[p]+G[p][i])
            {
                dist[i] = dist[p]+G[p][i];
                if(!vis[i])
                {
                    vis[i] = 1;
                    cnt[i]++;
                    if(cnt[i]>=n)
                        return -1;
                    Q.push(i);
                }
            }
        }
    }
    if(dist[n] == INF) return -2;
    return dist[n];
}

int main()
{
    while(scanf("%d %d %d", &n, &ml, &md) != EOF)
    {
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=n; j++)
                G[i][j] = INF;
            G[i][i] = G[i][i-1] = 0;
        }
        int u, v, w;
        for(int i=1; i<=ml; i++)
        {
            scanf("%d %d %d", &u, &v, &w);
            G[u][v] = w;
        }
        for(int i=1; i<=md; i++)
        {
            scanf("%d %d %d", &u, &v, &w);
            G[v][u] = -w;
        }

        int ans = spfa(1);
        printf("%d\n", ans);
    }
    return 0;
}

时间: 2024-10-06 02:14:52

Layout---poj3169(差分约束+最短路spfa)的相关文章

poj 3169 Layout(差分约束)

Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6549   Accepted: 3168 Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 <= N <= 1,000) cows numbered 1..N standing along a

poj3169——Layout(差分约束+SPFA判断负环)

Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 <= N <= 1,000) cows numbered 1..N standing along a straight line waiting for feed. The cows are standing in the same order as they are numbe

(简单) POJ 3169 Layout,差分约束+SPFA。

Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 <= N <= 1,000) cows numbered 1..N standing along a straight line waiting for feed. The cows are standing in the same order as they are numbe

Candies(差分约束_栈+SPFA)

CandiesCrawling in process... Crawling failed Time Limit:1500MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I64u Submit Status Description During the kindergarten days, flymouse was the monitor of his class. Occasionally the head-teacher b

UVA 11478 - Halum(差分约束+最短路)

UVA 11478 - Halum 题目链接 题意:给定一个有向图,每次操作可以选择一个结点,把以这个点为起点的边权值+d,以这个边为终点的-d,问经过操作后,能得到的边权最小的最大值是多少,并且要判但是否无穷大或无解 思路:转化为差分约束,设一条边,他增加的权值为sum(u)减少了sum(v),那么二分答案x,得到一个不等式sum(u) - sum(v) + w(u, v) >= x,变形后得到sum(v) - sum(u) <= w(u, v) - x,这样就转化为了差分约束,直接bell

POJ3169 差分约束 线性

Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12522   Accepted: 6032 Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 <= N <= 1,000) cows numbered 1..N standing along

POJ 题目3169 Layout(差分约束)

Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8354   Accepted: 4017 Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 <= N <= 1,000) cows numbered 1..N standing along a

poj1201 Intervals,差分约束问题,spfa

题目大意: 有一个序列,题目用n个整数组合 [ai,bi,ci]来描述它,[ai,bi,ci]表示在该序列中处于[ai,bi]这个区间的整数至少有ci个.如果存在这样的序列,请求出满足题目要求的最短的序列长度是多少.如果不存在则输出 -1. 输入:第一行包括一个整数n,表示区间个数,以下n行每行描述这些区间,第i+1行三个整数ai,bi,ci,由空格隔开,其中0<=ai<=bi<=50000 而且 1<=ci<=bi-ai+1. 输出:一行,输出满足要求的序列的长度的最小值.

poj3159 差分约束 spfa

1 //Accepted 2692 KB 1282 ms 2 //差分约束 -->最短路 3 //TLE到死,加了输入挂,手写queue 4 #include <cstdio> 5 #include <cstring> 6 #include <iostream> 7 #include <queue> 8 #include <cmath> 9 #include <algorithm> 10 using namespace std;