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. MVC增删查改,从数据库到后台,到前端,整个复习一下

    就当是记笔记吧,这里,就不讲什么版式了,首先上数据库脚本,这个是我这次练习用到的数据库脚本: USE [DB_USERS] GO /****** Object: Table [dbo].[Studen ...

  2. 常用库nuget包集合

    ColorConsole htmlagilitypack.1.4.9.5 经测试效率比 CsQueryLaster 高 csvhelper Extend Devlib系列一套 itextsharp l ...

  3. [转载]Ubuntu14.04 LTS更新源

    不同的网络状况连接以下源的速度不同, 建议在添加前手动验证以下源的连接速度(ping下就行),选择最快的源可以节省大批下载时间. 首先备份源列表: sudo cp /etc/apt/sources.l ...

  4. ViewPager和Fragment的组合使用

    如图是效果图用的是Viewpager和fragment来实现的主界面 不过其中的预加载我没有解决 如下是代码代码比较简单 package com.ithello.dingding; import ja ...

  5. 管理系统的前端解决方案:Pagurian V1.3发布

    Pagurian 一个管理系统的前端解决方案, 致力于让前端设计,开发,测试,发布更简单. 功能简介 Pagurian 适用于Web管理级的项目 基于Sea.js遵循CMD规范,友好的模块定义,使业务 ...

  6. ALV常用参数详细描述

    调用功能模块: CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY' EXPORTING i_interface_check        = ''               ...

  7. Spark GraphX学习资料

    <Spark GraphX 大规模图计算和图挖掘> http://book.51cto.com/art/201408/450049.htm http://www.csdn.net/arti ...

  8. Sharepoint学习笔记—习题系列--70-573习题解析 -(Q107-Q110)

    Question 107You are creating a custom workflow action that will be used in Microsoft SharePoint Desi ...

  9. [leetcode] Bitwise AND of Numbers Range

    Bitwise AND of Numbers Range Given a range [m, n] where 0 <= m <= n <= 2147483647, return t ...

  10. 内置对象Clob对从数据库表中取的字符大对象CLOB类型的列值进行读取操作

    package readclobDemo.bao; import java.io.IOException; import java.io.Reader; import java.sql.Clob; i ...