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。。。

 #include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
#define CLR(a,b) memset((a),(b),sizeof((a)))
using namespace std; const int N = ;
const int M = ;
const int inf = 0x3f3f3f3f; int n, m;
bool s[N];
int head[N];
int cnt;
struct edge{
int nex;
int v, w;
}g[M];
struct node{
int v, w;
node(int _v=,int _w=):v(_v),w(_w){}
bool operator < (const node&r)const{
return r.w < w;
}
};
void add_edge(int u,int v,int w){
g[cnt].v = v;
g[cnt].w = w;
g[cnt].nex = head[u];
head[u] = cnt++;
}
void dij(){
int i, u, v, w;
node t;
CLR(s, );
priority_queue<node>q;
q.push(node(,));
while(!q.empty()){
t = q.top(); q.pop();
u = t.v;
if(s[u]) continue;
s[u] = ;
if(u == n) break;
for(i = head[u]; ~i; i = g[i].nex){
v = g[i].v;
if(!s[v]){
w = t.w + g[i].w;
q.push(node(v, w));
}
}
}
printf("%d\n", t.w);
}
int main(){
int i, j, a, b, c;
scanf("%d %d", &n, &m);
cnt = ;
CLR(head, -);
for(i = ; i <= m; ++i){
scanf("%d %d %d", &a, &b, &c);
add_edge(a,b,c);
}
dij();
return ;
}

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

  1. POJ 3159 Candies 差分约束dij

    分析:设每个人的糖果数量是a[i] 最终就是求a[n]-a[1]的最大值 然后给出m个关系 u,v,c 表示a[u]+c>=a[v] 就是a[v]-a[u]<=c 所以对于这种情况,按照u ...

  2. POJ-3159.Candies.(差分约束 + Spfa)

    Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 40407   Accepted: 11367 Descri ...

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

    题意:n个人,m个信息,每行的信息是3个数字,A,B,C,表示B比A多出来的糖果不超过C个,问你,n号人最多比1号人多几个糖果 解题关键:差分约束系统转化为最短路,B-A>=C,建有向边即可,与 ...

  4. poj3159 Candies(差分约束)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Candies Time Limit: 1500MS   Memory Limit ...

  5. POJ3159 Candies —— 差分约束 spfa

    题目链接:http://poj.org/problem?id=3159 Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submiss ...

  6. POJ-3159(差分约束+Dijikstra算法+Vector优化+向前星优化+java快速输入输出)

    Candies POJ-3159 这里是图论的一个应用,也就是差分约束.通过差分约束变换出一个图,再使用Dijikstra算法的链表优化形式而不是vector形式(否则超时). #include< ...

  7. [poj 3159]Candies[差分约束详解][朴素的考虑法]

    题意 编号为 1..N 的人, 每人有一个数; 需要满足 dj - di <= c 求1号的数与N号的数的最大差值.(略坑: 1 一定要比 N 大的...difference...不是" ...

  8. poj 3159 Candies 差分约束

    Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 22177   Accepted: 5936 Descrip ...

  9. Candies(差分约束)

    http://poj.org/problem?id=3159 题意: flymouse是幼稚园班上的班长,一天老师给小朋友们买了一堆的糖果,由flymouse来分发,在班上,flymouse和snoo ...

随机推荐

  1. Set Font Properties On Mouse Hover Of Push Button And Text Items At Run time In Oracle Forms

    Change the font size and weight of text items and push buttons on mouse hover in Oracle Forms.   An ...

  2. 08.安装Oracle 10g和SQLServer2008(仅作学习使用VirtualBox虚拟机来安装节省电脑资源)

    1.虚拟机和宿主机共享文件夹. 2.右ctrl+F切换VirtualBox全屏 3.安装Oracle 10g 4.输入密码:root------------>下一步 5.勾选网络配置" ...

  3. albert1017 Linux下压缩某个文件夹(文件夹打包)

    albert1017 Linux下压缩某个文件夹(文件夹打包) tar -zcvf /home/xahot.tar.gz /xahottar -zcvf 打包后生成的文件名全路径 要打包的目录例子:把 ...

  4. python_way ,day22 tonardo

    python_way day22 1.tonardo 2.cookie 3.api认证 一.tonardo: a.tonardo 初识 #!/usr/bin/env python3# Created ...

  5. python_way ,day1 编译安装python3、基础及流程控制

    本节内容: 1,Python介绍发展史 2,安装 3,Hello World 4,程序 5,变量,字符编码 6,用户输入 7,模块初识 一.python介绍 python的创始人为吉多·范罗苏姆(Gu ...

  6. iOS - OC 基本语法

    1.常见文件扩展名 .c C 语言源文件 .cc..cpp C++ 语言源文件 .m Objective-C 源文件 .mm Objective-C++ 源文件 .h 头文件 .pl Perl 源文件 ...

  7. ubuntu安装jdk-6u45-linux-x64-rpm.bin

    1. 参考网址: http://www.xuebuyuan.com/2070575.html http://blog.csdn.net/csusunxgg/article/details/895602 ...

  8. apt-get下载的文件

    1. http://kurenai.elastos.org/2013/05/02/ubuntu-apt-get%E5%B7%A5%E4%BD%9C%E5%8E%9F%E7%90%86/ http:// ...

  9. VIM跳到指定行

    ngg nG :n n is the line number

  10. 在代码中修改TextView的DrawableRight图片

    TextView的xml <TextView android:id="@+id/textciew1" android:layout_width="match_par ...