差分约束Poj3159 Candies

没负环。直接搞就行,但是 spfa 队列会超时。

?





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

#include <cstdio>

#include <cstdlib>

#include <cstring>

#include <algorithm>

#include <cmath>

#include <stack>

#include <queue>

#include <vector>

#include <map>

#include <string>

#include <iostream>

int
n;

using
namespace std;

struct
edge

{

    int
to;int
val;int
next;

}e[555555];

int
len;int
head[33333];

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

{

    e[len].to=to;e[len].val=val;

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

}

const
int INF=0xfffffff;

int
dis[44444];

void
spfa(int
x)

{

    int
vis[444444];

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

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

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

    stack<int> q;// 不知道为什么 栈能过  队列却不行。

    q.push(x);

    while(!q.empty()){

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

        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]){

                    vis[cc]=1;

                    q.push(cc);

                }

            }

        }

    }

}

int
main()

{

    int
m;

    scanf("%d%d",&n,&m);

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

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

            int
a,c,b;

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

            add(a,b,c);

        }

        spfa(1);

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

    return
0;

}

  

时间: 2024-10-29 19:10:20

差分约束Poj3159 Candies的相关文章

poj3159 Candies(差分约束,dij+heap)

poj3159 Candies 这题实质为裸的差分约束. 先看最短路模型:若d[v] >= d[u] + w, 则连边u->v,之后就变成了d[v] <= d[u] + w , 即d[v] – d[u] <= w. 再看题目给出的关系:b比a多的糖果数目不超过c个,即d[b] – d[a] <= c ,正好与上面模型一样, 所以连边a->b,最后用dij+heap求最短路就行啦. ps:我用vector一直TLE,后来改用前向星才过了orz... 1 #include&

poj3159——Candies(差分约束+SPFA堆栈)

Description During the kindergarten days, flymouse was the monitor of his class. Occasionally the head-teacher brought the kids of flymouse's class a large bag of candies and had flymouse distribute them. All the kids loved candies very much and ofte

(简单) POJ 3159 Candies,Dijkstra+差分约束。

Description During the kindergarten days, flymouse was the monitor of his class. Occasionally the head-teacher brought the kids of flymouse’s class a large bag of candies and had flymouse distribute them. All the kids loved candies very much and ofte

POJ 3159 Candies 差分约束

链接:http://poj.org/problem?id=3159 Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 24852   Accepted: 6737 Description During the kindergarten days, flymouse was the monitor of his class. Occasionally the head-teacher brought the

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

K - Candies(最短路+差分约束)

题目大意:给N个小屁孩分糖果,每个小屁孩都有一个期望,比如A最多比B多C个,再多了就不行了,会打架的,求N最多比1多几块糖 分析:就是求一个极小极大值...试试看 这里需要用到一个查分约束的东西 下面是查分约束详解: 一直不知道差分约束是什么类型题目,最近在写最短路问题就顺带看了下,原来就是给出一些形如x-y<=b不等式的约束,问你是否满足有解的问题 好神奇的是这类问题竟然可以转换成图论里的最短路径问题,下面开始详细介绍下 比如给出三个不等式,b-a<=k1,c-b<=k2,c-a<

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;

POJ 3159 Candies(SPFA+栈)差分约束

题目链接:http://poj.org/problem?id=3159 题意:给出m给 x 与y的关系.当中y的糖数不能比x的多c个.即y-x <= c  最后求fly[n]最多能比so[1] 多多少糖? 差分约束问题, 就是求1-n的最短路,  队列实现spfa 会超时了,改为栈实现,就可以 有负环时,用栈比队列快 数组开小了,不报RE,报超时 ,我晕 #include <iostream> #include <cstdlib> #include <cstdio>

poj3159 最短路(差分约束)

题意:现在需要分糖果,有n个人,现在有些人觉得某个人的糖果数不能比自己多多少个,然后问n最多能在让所有人都满意的情况下比1多多少个. 这道题其实就是差分约束题目,根据题中给出的 a 认为 b 不能比 a 多 c 个,也就是 d[b] - d[a] ≤ c,就可以建立 value 值为 c 的单向边 e(a,b) ,然后先定d[1] = 0 ,用最短路跑完得到的 d[n] 就是所求答案. 1 #include<stdio.h> 2 #include<string.h> 3 #incl