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<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<queue>
 5 #include<vector>
 6 #include<set>
 7 #define CLR(a,b) memset((a),(b),sizeof((a)))
 8 using namespace std;
 9
10 const int N = 30001;
11 const int M = 150001;
12 const int inf = 0x3f3f3f3f;
13
14 int n, m;
15 bool s[N];
16 int head[N];
17 int cnt;
18 struct edge{
19     int nex;
20     int v, w;
21 }g[M];
22 struct node{
23     int v, w;
24     node(int _v=0,int _w=0):v(_v),w(_w){}
25     bool operator < (const node&r)const{
26         return r.w < w;
27     }
28 };
29 void add_edge(int u,int v,int w){
30     g[cnt].v = v;
31     g[cnt].w = w;
32     g[cnt].nex = head[u];
33     head[u] = cnt++;
34 }
35 void dij(){
36     int i, u, v, w;
37     node t;
38     CLR(s, 0);
39     priority_queue<node>q;
40     q.push(node(1,0));
41     while(!q.empty()){
42         t = q.top(); q.pop();
43         u = t.v;
44         if(s[u]) continue;
45         s[u] = 1;
46         if(u == n) break;
47         for(i = head[u]; ~i; i = g[i].nex){
48             v = g[i].v;
49             if(!s[v]){
50                 w = t.w + g[i].w;
51                 q.push(node(v, w));
52             }
53         }
54     }
55     printf("%d\n", t.w);
56 }
57 int main(){
58     int i, j, a, b, c;
59     scanf("%d %d", &n, &m);
60     cnt = 0;
61     CLR(head, -1);
62     for(i = 1; i <= m; ++i){
63         scanf("%d %d %d", &a, &b, &c);
64         add_edge(a,b,c);
65     }
66     dij();
67     return 0;
68 }

时间: 2024-12-05 12:09:37

poj3159 Candies(差分约束,dij+heap)的相关文章

poj3159 Candies(差分约束)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Candies Time Limit: 1500MS   Memory Limit: 131072K Description During the kindergarten days, flymouse was the monitor of his class. Occasionally the head-teacher brought the kids of flymouse’

[poj3159]Candies(差分约束+链式前向星dijkstra模板)

题意:n个人,m个信息,每行的信息是3个数字,A,B,C,表示B比A多出来的糖果不超过C个,问你,n号人最多比1号人多几个糖果 解题关键:差分约束系统转化为最短路,B-A>=C,建有向边即可,与dijkstra中的d[v]>=d[u]+C相同,即可求解. #include<cstdio> #include<cstring> #include<algorithm> #include<cstdlib> #include<iostream>

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

poj 3159 candies (差分约束 spfa+stack)

http://poj.org/problem?id=3159 题意:一个班有n个人 每人分到若干糖果 且u的糖果数不能比v少w个 求第1个人与第n个人最大数量差 照着模板spfa+queue果断tle了 之后照着题解说的把queue改成stack就过了 但是还不明白为什么会快 而且如果用数组直接模拟会比stl更快 #include<cstdio> #include<cstring> #include<iostream> #include<algorithm>

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

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

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

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