poj 3159 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 A, B 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
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 差分约束的更多相关文章
- 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 ...
- [poj 3159]Candies[差分约束详解][朴素的考虑法]
题意 编号为 1..N 的人, 每人有一个数; 需要满足 dj - di <= c 求1号的数与N号的数的最大差值.(略坑: 1 一定要比 N 大的...difference...不是" ...
- POJ 3159 Candies (图论,差分约束系统,最短路)
POJ 3159 Candies (图论,差分约束系统,最短路) Description During the kindergarten days, flymouse was the monitor ...
- POJ 3159 Candies(SPFA+栈)差分约束
题目链接:http://poj.org/problem?id=3159 题意:给出m给 x 与y的关系.当中y的糖数不能比x的多c个.即y-x <= c 最后求fly[n]最多能比so[1] ...
- POJ 3159 Candies(差分约束,最短路)
Candies Time Limit: 1500MS Memory Limit: 131072K Total Submissions: 20067 Accepted: 5293 Descrip ...
- POJ 3159 Candies 解题报告(差分约束 Dijkstra+优先队列 SPFA+栈)
原题地址:http://poj.org/problem?id=3159 题意大概是班长发糖果,班里面有不良风气,A希望B的糖果不比自己多C个.班长要满足小朋友的需求,而且要让自己的糖果比snoopy的 ...
- POJ 3159 Candies(差分约束+spfa+链式前向星)
题目链接:http://poj.org/problem?id=3159 题目大意:给n个人派糖果,给出m组数据,每组数据包含A,B,C三个数,意思是A的糖果数比B少的个数不多于C,即B的糖果数 - A ...
- 图论--差分约束--POJ 3159 Candies
Language:Default Candies Time Limit: 1500MS Memory Limit: 131072K Total Submissions: 43021 Accep ...
- (简单) POJ 3159 Candies,Dijkstra+差分约束。
Description During the kindergarten days, flymouse was the monitor of his class. Occasionally the he ...
随机推荐
- 关于Chrome Dev Tool
★注意点一:函数名,方法名要拼写准确;
- WCF小结
WCF总结 1.WCF(原代号为Indigo)是一个用于创建和运行分布式系统的技术集合,使用它能创建安全的.可靠的.跨平台的的分布式解决方案.它的面向服务的编程模型,整合了.Net平台下以往全部的分布 ...
- Fluent NHibernate and Spring.net
http://blog.bennymichielsen.be/2009/01/04/using-fluent-nhibernate-in-spring-net/ http://comments.gma ...
- csharp: Export or Import excel using NPOI
excel 2003: using System; using System.Collections.Generic; using System.ComponentModel; using Syste ...
- PHP(1)——学习之前做点啥准备
工欲善其事必先利其器,当然是先准备工具咯.首先硬件条件就是双核CPU以及8G内存的电脑一台,操作系统环境:windows(64bit)7+.Mac OSX 10.10+.Linux 64bit.软件环 ...
- python函数和常用模块(二),Day4
内置函数2 装饰器 字符串格式化 生成器 迭代器 递归 模块 序列化相关 time模块 datetime模块 内置函数2 callable() # 是否可以被执行,是否可以被调用 chr() # as ...
- 使用PHP搭建书虫网站
年前开始了一个新项目,书虫项目的启动,项目组里面我是负责搭建网站的.以前听说过PHP的大名,就是没怎么看过,只能从一无所知开始了. 来自PHP手册的介绍:PHP, 即“PHP: Hypertext P ...
- HTTP路由
HTTP路由 HTTP路由(译者注:Play的路径映射机制)组件负责将HTTP请求交给对应的action(一个控制器Controller的公共静态方法)处理. 对于MVC框架来说,一个HTTP请求可以 ...
- Angular 核心概念2
自定义指令 指令增强了 HTML,提供额外的功能 内置的指令基本上已经可以满足我们的绝大多数需要了 少数情况下我们有一些特殊的需要,可以通过自定义指令的方式实现 普通指令 语法 <div hel ...
- 关于字符串replace方法第二个参数探究
网上有关replace的文章很多了,这里主要聊聊它的第二个参数.阅读本文需要对replace方法有一定了解.W3school=>replace 我们要把一段字符串中的某些指定字符替换掉,第一时间 ...