差分约束Poj 3169 Layout

?





1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

#include <cstdio>

#include <cstdlib>

#include <cstring>

#include <algorithm>

#include <cmath>

#include <stack>

#include <queue>

#include <vector>

#include <map>

#include <string>

#include <iostream>

using
namespace std;

int
n;

const
int INF=0xfffffff;

struct
edge

{

    int
val;int
to;int
next;

}e[111111];

int
len;int
head[11111];

void
add(int
from,int
to,int
val)

{

    e[len].to=to;e[len].val=val;e[len].next=head[from];head[from]=len++;

}

int
dis[11111];

int
spfa(int
x)

{

    int
vis[11111];

    for(int
i=1;i<=n;i++) vis[i]=0;

    for(int
i=1;i<=n;i++) dis[i]=INF;

    vis[x]=1; dis[x]=0;

    queue<int> q;

    q.push(x);  int
cnt[11111]={0}; cnt[x]=1;

    while(!q.empty()){

        int
cur=q.front(); q.pop();vis[cur]=0;

        for(int
i=head[cur];i!=-1;i=e[i].next){

            int
cc=e[i].to;

            if(dis[cc]>dis[cur]+e[i].val){

                dis[cc]=dis[cur]+e[i].val;

                if(!vis[cc]){

                    cnt[cc]++;if(cnt[cc]>n) return
0;

                    q.push(cc); vis[cc]=1;

                }

            }

        }

    }

    return
1;

}

int
main()

{

    int
m,l;

    while(scanf("%d%d%d",&n,&m,&l)!=EOF){

        len=0;memset(head,-1,sizeof(head));

        for(int
i= 0;i<m;i++){

            int
a,b,c;scanf("%d%d%d",&a,&b,&c);

            if(a>b) {int
t=a;a=b;b=t;}

            add(a,b,c);

        }

        for(int
i=0;i<l;i++){

            int
a,b,c;scanf("%d%d%d",&a,&b,&c);

            if(a<b){int
t=a;a=b;b=t;};

            add(a,b,-c);

        }

        if(!spfa(1)) printf("-1\n");

        else{

            if(dis[n]==INF)

                printf("-2\n");

            else

                printf("%d\n",dis[n]);

        }

    }

    return
0;

}

  

时间: 2024-12-16 19:05:28

差分约束Poj 3169 Layout的相关文章

差分约束 poj 3169

Description Windy has a country, and he wants to build an army to protect his country. He has picked up N girls and M boys and wants to collect them to be his soldiers. To collect a soldier without any privilege, he must pay 10000 RMB. There are some

POJ 3169 Layout (差分约束+SPFA)

Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6832   Accepted: 3292 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

POJ 3169 Layout (图论-差分约束)

Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6574   Accepted: 3177 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

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

poj 3169 Layout (差分约束+Bellman )

题目链接:http://poj.org/problem?id=3169 题意:输入N, ML, MD, N默示有N个牛按1-N排成一排,ML,默示有ML行,每行输入A, B, D默示A牛和B牛最远间隔为D, MD默示有MD行,每行输入A,B,D默示A牛和B来间隔为D,求满足所有前提的1-N的最大间隔. 比较简单的差分约束,这个周周赛的A题 #include <iostream> #include <cstdlib> #include <cstdio> #include

POJ 3169 Layout (差分约束)

题意:给定一些母牛,要求一个排列,有的母牛距离不能超过w,有的距离不能小于w,问你第一个和第n个最远距离是多少. 析:以前只是听说过个算法,从来没用过,差分约束. 对于第 i 个母牛和第 i+1 个,D[i] - D[i+1] <= 0,  D[j] -D[i ]<= k, D[i] - D[j] <= - k,那么这个题就可以用差分约束来求这个不等式组了. 1.对于差分不等式,a - b <= c ,建一条 b 到 a 的权值为 c 的边,求的是最短路,得到的是最大值(本题求的就

(简单) 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

POJ 3169 Layout(差分约束系统)

题目链接:http://poj.org/problem?id=3169 题意:n头牛编号为1到n,按照编号的顺序排成一列,每两头牛的之间的距离 >= 0.这些牛的距离存在着一些约束关系:1.有ML组(u, v, w)的约束关系,表示牛[u]和牛[v]之间的距离必须 <= w.2.有MD组(u, v, w)的约束关系,表示牛[u]和牛[v]之间的距离必须 >= w.问如果这n头无法排成队伍,则输出-1,如果牛[1]和牛[n]的距离可以无限远,则输出-2,否则则输出牛[1]和牛[n]之间的最

POJ 3169 Layout (差分约束系统)

题目地址:POJ 3169 很简单的差分约束..公式很明显.当输入最大值的时候,是a-b<=c,最小值的时候是a-b>=c.然后根据这个式子用最短路求. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctyp