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 often compared the numbers of candies they got with others. A kid A could had the idea that though it might be the case that another kid B was better than him in some aspect and therefore had a reason for deserving more candies than he did, he should never get a certain number of candies fewer than B did no matter how many candies he actually got, otherwise he would feel dissatisfied and go to the head-teacher to complain about flymouse’s biased distribution.

  snoopy shared class with flymouse at that time. flymouse always compared the number of his candies with that of snoopy’s. He wanted to make the difference between the numbers as large as possible while keeping every kid satisfied. Now he had just got another bag of candies from the head-teacher, what was the largest difference he could make out of it?

  题目就是差分约束问题,但是这个题卡SPFA。。。。。。

代码如下:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h> using namespace std; const int MaxN=;
const int MaxM=;
const int INF=10e9+; struct Node
{
int id,val; Node(int _id=,int _val=):id(_id),val(_val) {} bool operator < (const Node & a) const
{
return val>a.val;
}
}; struct Edge
{
int to,next,cost;
}; Edge E[MaxM];
int head[MaxN],Ecou;
int vis[MaxN]; void Dijkstra(int lowcost[],int N,int start)
{
priority_queue <Node> que;
Node temp;
int u,v,c; for(int i=;i<=N;++i)
{
lowcost[i]=INF;
vis[i]=;
} que.push(Node(start,));
lowcost[start]=; while(!que.empty())
{
temp=que.top();
que.pop(); u=temp.id; if(vis[u])
continue; vis[u]=; for(int i=head[u];i!=-;i=E[i].next)
{
v=E[i].to;
c=E[i].cost; if(lowcost[v]>lowcost[u]+c && !vis[v])
{
lowcost[v]=lowcost[u]+c;
que.push(Node(v,lowcost[v]));
}
}
}
} void init(int N)
{
for(int i=;i<=N;++i)
head[i]=-;
Ecou=;
} void addEdge(int u,int v,int c)
{
E[Ecou].to=v;
E[Ecou].cost=c;
E[Ecou].next=head[u];
head[u]=Ecou++;
} int ans[MaxN]; int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout); int N,M;
int a,b,c; scanf("%d %d",&N,&M); init(N); while(M--)
{
scanf("%d %d %d",&a,&b,&c);
addEdge(a,b,c);
} Dijkstra(ans,N,); printf("%d\n",ans[N]); return ;
}

(简单) POJ 3159 Candies,Dijkstra+差分约束。的更多相关文章

  1. POJ 3159 Candies 【差分约束+Dijkstra】

    <题目链接> 题目大意: 给n个人派糖果,给出m组数据,每组数据包含A,B,c 三个数,意思是A的糖果数比B少的个数不多于c,即B的糖果数 - A的糖果数<= c .最后求n 比 1 ...

  2. POJ 3159 Candies(差分约束,最短路)

    Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 20067   Accepted: 5293 Descrip ...

  3. POJ 3159 Candies(差分约束+spfa+链式前向星)

    题目链接:http://poj.org/problem?id=3159 题目大意:给n个人派糖果,给出m组数据,每组数据包含A,B,C三个数,意思是A的糖果数比B少的个数不多于C,即B的糖果数 - A ...

  4. POJ 3159 Candies(差分约束+最短路)题解

    题意:给a b c要求,b拿的比a拿的多但是不超过c,问你所有人最多差多少 思路:在最短路专题应该能看出来是差分约束,条件是b - a <= c,也就是满足b <= a + c,和spfa ...

  5. POJ 3159 Candies(差分约束)

    http://poj.org/problem?id=3159 题意:有向图,第一行n是点数,m是边数,每一行有三个数,前两个是有向边的起点与终点,最后一个是权值,求从1到n的最短路径. 思路:这个题让 ...

  6. poj 3159 Candies (差分约束)

    一个叫差分约束系统的东西.如果每个点定义一个顶标x(v),x(t)-x(s)将对应着s-t的最短路径. 比如说w+a≤b,那么可以画一条a到b的有向边,权值为w,同样地给出b+w2≤c,a+w3≤c. ...

  7. POJ 3159 Candies 还是差分约束(栈的SPFA)

    http://poj.org/problem?id=3159 题目大意: n个小朋友分糖果,你要满足他们的要求(a b x 意思为b不能超过a x个糖果)并且编号1和n的糖果差距要最大. 思路: 嗯, ...

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

    Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ ...

  9. poj 3159 Candies dijkstra + queue

    题目链接: http://poj.org/searchproblem 题目大意: 飞天鼠是班长,一天班主任买了一大包糖果,要飞天鼠分发给大家,班里面有n个人,但是学生A认为学生B比自己多的糖果数目不应 ...

随机推荐

  1. Java学习笔记之接口和抽象类

    接口(interface)1.interface创建一个接口,implements实现接口 interface jiekou{} class lie implements jiekou{}2.接口可以 ...

  2. CURL C++网络延时或者最低网速下载设置

    1.起因: 下载游戏更新包客户反应更新时间太久,要求我们网速比较低的时候就不要更新 2.解决: 因为之前用的是curl下载,所以在查看了curl.h里面的说明后使用了以下两个option实现了下载时最 ...

  3. C++中的函数指针和函数对象总结

    篇一.函数指针函数指针:是指向函数的指针变量,在C编译时,每一个函数都有一个入口地址,那么这个指向这个函数的函数指针便指向这个地址.函数指针的用途是很大的,主要有两个作用:用作调用函数和做函数的参数. ...

  4. dfs手写栈模板

    在竞赛中如果系统栈很小的话,过深的递归会让栈溢出,这个时候我们就要自己手写栈,将递归转化成手工栈. 方法其实也很简单. 基本思路上,我们就是用栈不断的pop,push.但是何时push,何时pop呢? ...

  5. Hibernate的dynamic-insert和dynamic-update的使用

    Hibernate在初始化的时候,默认按照配置为表预定义insert,delete,update,select(by id)的SQL语句放在session中,其中insert,update,selec ...

  6. JS里的CSS函数

    <title>无标题文档</title> <script> function css(obj,name,value){ if(arguments.length==2 ...

  7. C语言开发工具

    1.编写程序的工具: indent命令将自动调整C代码的缩进风格,个人比较喜欢indent -kr 2.编译C语言程序: 1.gcc编译器: gcc是一个ANSI C兼容编译器,C++编译器也可以编译 ...

  8. lucene3.6.1 经典案例 入门教程 (包含从文件中读取content)

    转载http://liqita.iteye.com/blog/1676664 第一步:下载lucene的核心包 lucene-core-3.6.1-javadoc.jar (3.5 MB) lucen ...

  9. J2SE网络编程之 TCP与UDP

    1.什么是TCP TCP(Transmission Control Protocol传输控制协议)是一种面向连接的.可靠的.基于字节流的通信协议,位于传输层.这三个特点中,面向连接就如同打电话,双方的 ...

  10. vbs 解析html文档的方法

    vbs 解析html文档的方法(htmlfile) 投稿:mdxy-dxy 字体:[增加 减小] 类型:转载 时间:2014-06-18我要评论 关于VBS采集,网上流行比较多的方法都是正则,其实 h ...