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

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?

Input

The input contains a single test cases. The test cases starts with a line with two integers N and M not exceeding 30 000 and 150 000 respectively. N is the number of kids in the class and the kids were numbered 1 through N.
snoopy and flymouse were always numbered 1 and N. Then follow M lines each holding three integers AB and c in order, meaning that kid A believed that kid Bshould never get over c candies
more than he did.

Output

Output one line with only the largest difference desired. The difference is guaranteed to be finite.

Sample Input

2 2
1 2 5
2 1 4

Sample Output

5

Hint

32-bit signed integer type is capable of doing all arithmetic.

Source

先输入n,m

接下来m行,每行输入A,B,C

输入A B C,表示孩子B最多比孩子A多C块蛋糕,问孩子1与孩子N最多相差多少块蛋糕!

Tips:

不能用queue<>队列来做,模拟队列可以!

#include "stdio.h"
#include "string.h"
//#include "queue"
//using namespace std; #define N 30005 //图中点的个数
#define INF 0x3fffffff struct node
{
int x,y;
int weight;
int next;
}edge[5*N]; int n,m;
int dist[N];
bool mark[N];
int head[N],idx;
int stack[5*N]; void Init();
void SPFA();
void Add(int x,int y,int k); int main()
{
int i;
int x,y,k;
scanf("%d %d",&n,&m);
Init();
for(i=0; i<m; ++i)
{
scanf("%d %d %d",&x,&y,&k); /***y-x<=k***从点x到点y建边,权值为k**/
Add(x,y,k);
}
SPFA();
printf("%d\n",dist[n]);
return 0;
} void Init()
{
idx = 0;
memset(head,-1,sizeof(head));
} void Add(int x,int y,int k)
{
edge[idx].x = x;
edge[idx].y = y;
edge[idx].weight = k;
edge[idx].next = head[x];
head[x] = idx++;
} void SPFA() //这题只能模拟队列~
{
int i;
int x,y;
memset(mark,false,sizeof(mark));
for(i=1; i<=n; ++i) dist[i] = INF;
int top = 0;
//queue<int> q;
stack[++top] = 1;//q.push(1);
mark[1] = true;
dist[1] = 0;
while(top)
{
x = stack[top--];
//q.pop();
for(i=head[x]; i!=-1; i=edge[i].next)
{
y = edge[i].y;
if(dist[y] > dist[x] + edge[i].weight)
{
dist[y] = dist[x] + edge[i].weight;
if(!mark[y])
{
mark[y] = true;
stack[++top] = y;//q.push(y);
}
}
}
mark[x] = false;
}
}

poj 3159 Candies 差分约束的更多相关文章

  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[差分约束详解][朴素的考虑法]

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

  3. POJ 3159 Candies (图论,差分约束系统,最短路)

    POJ 3159 Candies (图论,差分约束系统,最短路) Description During the kindergarten days, flymouse was the monitor ...

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

    题目链接:http://poj.org/problem?id=3159 题意:给出m给 x 与y的关系.当中y的糖数不能比x的多c个.即y-x <= c  最后求fly[n]最多能比so[1] ...

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

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

  6. POJ 3159 Candies 解题报告(差分约束 Dijkstra+优先队列 SPFA+栈)

    原题地址:http://poj.org/problem?id=3159 题意大概是班长发糖果,班里面有不良风气,A希望B的糖果不比自己多C个.班长要满足小朋友的需求,而且要让自己的糖果比snoopy的 ...

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

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

  8. 图论--差分约束--POJ 3159 Candies

    Language:Default Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 43021   Accep ...

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

    Description During the kindergarten days, flymouse was the monitor of his class. Occasionally the he ...

随机推荐

  1. thread_Disruptor

    转自 知乎 https://zhuanlan.zhihu.com/p/21355046 order从client端传入,decode后进行matching,一旦存在可成交的价格,就要publish到t ...

  2. ADO.NET 增 删 改 查

    ADO.NET:(数据访问技术)就是将C#和MSSQL连接起来的一个纽带 可以通过ADO.NET将内存中的临时数据写入到数据库中 也可以将数据库中的数据提取到内存中供程序调用 ADO.NET所有数据访 ...

  3. ajax请求跨域问题

    ajax跨域,这个是面试的时候常被问到,也是在做项目的时候会遇到的问题,在之前的项目中就有遇到过,这里根据经验写了三种分享下 1.使用中间层过渡的方式 简单来说就是"后台代理",把 ...

  4. 小白初学ABP框架,着实累啊

    这几天在学习ABP相关的知识AutoMapper ,AngularJS,Less,DI(dependencyInjection),EntityFramework code first以及相关NuGet ...

  5. 基于java的socket编程

    #开头的废话#学习java已经半个月了,原本在抠教材里面的字眼时,觉得教材好厚,要看完不知道要到猴年马月去了.突然在网上看到一个教程,里面老师说学编程语言书不用太细看,看个大概,知道里面讲些什么就好, ...

  6. Android 手机卫士14--Widget窗口小部件AppWidgetProvider

    1.AndroidManifest.xml根据窗体小部件广播接受者关键字android.appwidget.action.APPWIDGET_UPDATE 搜索android:resource=&qu ...

  7. SharpGL学习笔记(十九) 摄像机漫游

    所谓的摄像机漫游,就是可以在场景中来回走动. 现实中,我们通过眼睛观察东西,身体移动带动眼睛移动观察身边的事物,这也是在漫游. 在OpenGL中我们使用函数LookAt()来操作摄像机在三维场景中进行 ...

  8. rails provide与content_for的区别

    页面渲染时:provide先执行,但找到一个provide之后就不再查找 content_for 顺序执行,在哪个位置,就等之前的渲染完后才执行.但是要等到所有的content被查找完后一块返回,也就 ...

  9. ASP.NET MVC 微信公共平台开发之验证消息的真实性

    ASP.NET MVC 微信公共平台开发 验证消息的真实性 在MVC Controller所在项目中添加过滤器,在过滤器中重写 public override void OnActionExecuti ...

  10. 安卓开发_浅谈SubMenu(子菜单)

    子菜单,即点击菜单后出现一个菜单栏供选择 创建子菜单的步骤: (1) 覆盖Activity的onCreateOptionsMenu()方法,调用Menu的addSubMenu()方法来添加子菜单 (2 ...