BNUOJ 3278 Candies
Candies
This problem will be judged on PKU. Original ID: 3159
64-bit integer IO format: %lld Java class name: Main
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 B should 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
Source
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
struct arc{
int to,w,next;
};
arc g[];
int head[],tot,n,m;
int d[];
bool done[];
void add(int u,int v,int w){
g[tot].to = v;
g[tot].w = w;
g[tot].next = head[u];
head[u] = tot++;
}
stack<int>stk;
void spfa(int s){
d[s] = ;
while(!stk.empty()) stk.pop();
done[s] = true;
stk.push(s);
int u,i;
while(!stk.empty()){
u = stk.top();
stk.pop();
done[u] = false;
for(i = head[u]; i != -; i = g[i].next){
if(d[g[i].to] > d[u] + g[i].w){
d[g[i].to] = d[u] + g[i].w;
if(!done[g[i].to]){
done[g[i].to] = true;
stk.push(g[i].to);
}
}
}
}
}
int main(){
int i,j,u,v,w;
while(~scanf("%d %d",&n,&m)){
for(i = ; i <= n; i++){
done[i] = false;
head[i] = -;
d[i] = INF;
}
for(tot = i = ; i < m; i++){
scanf("%d %d %d",&u,&v,&w);
add(u,v,w);
}
spfa();
printf("%d\n",d[n]);
}
return ;
}
好吧,加个优先队列版的Dijkstra吧
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define INF 0x3f3f3f3f
#define pii pair<int,int>
using namespace std;
struct arc{
int to,w,next;
};
arc g[];
int head[],d[],tot,n,m;
bool done[];
void add(int u,int v,int w){
g[tot].to = v;
g[tot].w = w;
g[tot].next = head[u];
head[u] = tot++;
}
priority_queue<pii,vector< pii >,greater< pii > >q;
void dijkstra(int s,int t){
while(!q.empty()) q.pop();
d[s] = ;
q.push(make_pair(d[s],s));
while(!q.empty()){
int u = q.top().second;
q.pop();
if(done[u]) continue;
done[u] = true;
for(int i = head[u]; i != -; i = g[i].next){
if(d[g[i].to] > d[u] + g[i].w){
d[g[i].to] = d[u] + g[i].w;
q.push(make_pair(d[g[i].to],g[i].to));
}
}
if(done[t]) return;
}
}
int main(){
int i,u,v,w;
while(~scanf("%d %d",&n,&m)){
for(i = ; i <= n; i++){
head[i] = -;
done[i] = false;
d[i] = INF;
}
for(tot = i = ; i < m; i++){
scanf("%d %d %d",&u,&v,&w);
add(u,v,w);
}
dijkstra(,n);
printf("%d\n",d[n]);
}
return ;
}
BNUOJ 3278 Candies的更多相关文章
- [ACM训练] 算法初级 之 搜索算法 之 广度优先算法BFS (POJ 3278+1426+3126+3087+3414)
BFS算法与树的层次遍历很像,具有明显的层次性,一般都是使用队列来实现的!!! 常用步骤: 1.设置访问标记int visited[N],要覆盖所有的可能访问数据个数,这里设置成int而不是bool, ...
- 【BFS】POJ 3278
POJ 3278 Catch That Cow 题目:你要去抓一头牛,给出你所在的坐标和牛所在的坐标,移动方式有两种:要么前一步或者后一步,要么移动到现在所在坐标的两倍,两种方式都要花费一分钟,问你最 ...
- 【POJ2886】Who Gets the Most Candies?-线段树+反素数
Time Limit: 5000MS Memory Limit: 131072K Case Time Limit: 2000MS Description N children are sitting ...
- BNUOJ 52325 Increasing or Decreasing 数位dp
传送门:BNUOJ 52325 Increasing or Decreasing题意:求[l,r]非递增和非递减序列的个数思路:数位dp,dp[pos][pre][status] pos:处理到第几位 ...
- bnuoj 24251 Counting Pair
一道简单的规律题,画出二维表将数字分别相加可以发现很明显的对称性 题目链接:http://www.bnuoj.com/v3/problem_show.php?pid=24251 #include< ...
- poj 3159 Candies 差分约束
Candies Time Limit: 1500MS Memory Limit: 131072K Total Submissions: 22177 Accepted: 5936 Descrip ...
- Who Gets the Most Candies?(线段树 + 反素数 )
Who Gets the Most Candies? Time Limit:5000MS Memory Limit:131072KB 64bit IO Format:%I64d &am ...
- BFS POJ 3278 Catch That Cow
题目传送门 /* BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 */ #include <cstdio> #include <iostream> #inc ...
- bnuoj 44359 快来买肉松饼
http://www.bnuoj.com/contest/problem_show.php?pid=44359 快来买肉松饼 Time Limit: 5000 ms Case Time Lim ...
随机推荐
- 通过API文档查询Math类的方法,打印出近似圆,只要给定不同半径,圆的大小就会随之发生改变
package question; import java.util.Scanner; import java.lang.Math; public class MathTest { /** * 未搞懂 ...
- [BUG]Dreamweaver6做网页的一个图片文字不清晰的问题
自己用Dreamweaver6做一个网页,使用PS做图片,为了节约下载流量,我把图片裁剪为GIF格式,通过系统自带的图片浏览器和美图看看,图片上的文字都是清晰的. 我把图片加载进入DW中后,在DW界面 ...
- 获取select里面option所有的值
1.HTML结构 <select id="test"> <option value="option-1">option-1</op ...
- Android开发学习—— 消息机制
###主线程不能被阻塞* 在Android中,主线程被阻塞会导致应用不能刷新ui界面,不能响应用户操作,用户体验将非常差* 主线程阻塞时间过长,系统会抛出ANR异常* ANR:Application ...
- RabbitMQ四:生产者--队列--消费者
AMQP协议的梳理和名词解析 建议先把上篇AMQP协议先看一遍,理解一下,由于用XMind绘图,电脑屏幕比较小,不能截取全部,如果想要全图和源代码,请下面留言....... 可以点击图片,打开到新的 ...
- poj2886 Who Gets the Most Candies?
思路: 先打反素数表,即可确定因子最多的那个数.然后模拟踢人的过程确定对应的人名.模拟的过程使用线段树优化加速. 实现: #include <cstdio> #include <cs ...
- vue报错-Error: Cannot find module '@babel/core'
vue之webpack实战的时候遇到报错,Error: Cannot find module '@babel/core' 这报错,我百度了很久,后来发现报错里面有提示,发现是我的 babel-load ...
- scala.的Enumeration枚举示例(转)
简介 在scala中没有枚举类型,但在标准类库中提供了Enumeration类来产出枚举.扩展Enumeration类后,调用value方法类初始化枚举中的可能值. 内部类value实际上是一个抽象类 ...
- iOS Programming Subclassing UITableViewCell
iOS Programming Subclassing UITableViewCell 1.Creating BNRItemCell UITableViewCell is a UIView subc ...
- JavaScript线程
JavaScript的setTimeout与setInterval是两个很容易欺骗别人感情的方法,因为我们开始常常以为调用了就会按既定的方式执行, 我想不少人都深有同感, 例如 setTimeout( ...