(简单) 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 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+差分约束。的更多相关文章
- POJ 3159 Candies 【差分约束+Dijkstra】
<题目链接> 题目大意: 给n个人派糖果,给出m组数据,每组数据包含A,B,c 三个数,意思是A的糖果数比B少的个数不多于c,即B的糖果数 - A的糖果数<= c .最后求n 比 1 ...
- POJ 3159 Candies(差分约束,最短路)
Candies Time Limit: 1500MS Memory Limit: 131072K Total Submissions: 20067 Accepted: 5293 Descrip ...
- POJ 3159 Candies(差分约束+spfa+链式前向星)
题目链接:http://poj.org/problem?id=3159 题目大意:给n个人派糖果,给出m组数据,每组数据包含A,B,C三个数,意思是A的糖果数比B少的个数不多于C,即B的糖果数 - A ...
- POJ 3159 Candies(差分约束+最短路)题解
题意:给a b c要求,b拿的比a拿的多但是不超过c,问你所有人最多差多少 思路:在最短路专题应该能看出来是差分约束,条件是b - a <= c,也就是满足b <= a + c,和spfa ...
- POJ 3159 Candies(差分约束)
http://poj.org/problem?id=3159 题意:有向图,第一行n是点数,m是边数,每一行有三个数,前两个是有向边的起点与终点,最后一个是权值,求从1到n的最短路径. 思路:这个题让 ...
- poj 3159 Candies (差分约束)
一个叫差分约束系统的东西.如果每个点定义一个顶标x(v),x(t)-x(s)将对应着s-t的最短路径. 比如说w+a≤b,那么可以画一条a到b的有向边,权值为w,同样地给出b+w2≤c,a+w3≤c. ...
- POJ 3159 Candies 还是差分约束(栈的SPFA)
http://poj.org/problem?id=3159 题目大意: n个小朋友分糖果,你要满足他们的要求(a b x 意思为b不能超过a x个糖果)并且编号1和n的糖果差距要最大. 思路: 嗯, ...
- (简单) POJ 3169 Layout,差分约束+SPFA。
Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ ...
- poj 3159 Candies dijkstra + queue
题目链接: http://poj.org/searchproblem 题目大意: 飞天鼠是班长,一天班主任买了一大包糖果,要飞天鼠分发给大家,班里面有n个人,但是学生A认为学生B比自己多的糖果数目不应 ...
随机推荐
- cout、cerr、clog
其实大家平常常会用的主要有三个:cout.cerr.clog,首先简单介绍下三者. 这三者在C++中都是标准IO库中提供的输出工具(至于有关的重载问题在此不讨论): cout:写到标准输出的ostre ...
- Windows下MySQL分步安装图解及问题总结
MySQL是一个出色的开源数据库,在易用性和性能方面都有相当不错的表现.就作者发帖为止, MySQL官方发布的所有版本中(4.1/5.0/5.1/6.0),推荐使用稳定的MySQL5.0版本(商用). ...
- JMeter-使用Badboy录制Web测试脚本
JMeter是纯Java编写的软件功能和性.能测试工具,其录制脚本过于笨拙和复杂.而Badboy是用C++开发的动态应用测试工具,其拥有强大的屏幕录制和回放功能,同时提供图形结果分析功能,刚好弥补了J ...
- Android 系统编译
最近研究了下Android 的编译系统,下面结合编译我们自己的产品 mobot 来对整个编译系统进行必要的介绍,方便大家今 后对默认编译的修改. 先列出几个觉得重要的Make 文件: build/bu ...
- 转:webdriver驱动未在默认目录安装的firefox
刚开始用webdriver的朋友一定会苦恼它时常不能启动firefox,很多时候是因为firefox安装在默认路径下.此时,我们有些常用方法,可以解决此问题. [1] System.setProper ...
- UVALive 6887 Book Club
最大流,有向环覆盖问题. #include<cstdio> #include<cstring> #include<string> #include<cmath ...
- Angular服务器通信之:$http
$http服务提供了浏览器XMLHttpRequest对象的封装,并且作为Angular中和后台服务通信的底层服务,在此之上Angular还提供了一个可选模块ngResource支持与RESTFul的 ...
- springmvc json数据
的 @RequestMapping("/getAllEdu") @ResponseBody public void getAllEdu(HttpServletRequest req ...
- eclipse myeclipse修改工作区间 an error has occurred. see error log for more details. java.lang.nullpointerexception 问题解决
解决办法:修改项目工作空间. 修改工作空间,以前打开myEclipse时知道怎么改!现在只有找配置文件了! 步骤: MyEclipse 5.1.1 GA----->Eclipse-----> ...
- 转 Android:sp与dp(densityDpi与scaledDensity)
一般在布局上设置控件大小维度的单位采用dp,而定义字体大小的单位采用sp. dp是dip,density independent pixel,即密度无关的像素单位,说白了,就是这个维度相对于不同屏幕的 ...