Candies

Time Limit: 1500ms
Memory Limit: 131072KB

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 AB 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

 
解题:一个很裸的差分约束问题!直接求1到n的最短路
 
至于为什么spfa要用栈而不是队列,因为一直超时!vector没法用,用了就超时!
 
 #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的更多相关文章

  1. [ACM训练] 算法初级 之 搜索算法 之 广度优先算法BFS (POJ 3278+1426+3126+3087+3414)

    BFS算法与树的层次遍历很像,具有明显的层次性,一般都是使用队列来实现的!!! 常用步骤: 1.设置访问标记int visited[N],要覆盖所有的可能访问数据个数,这里设置成int而不是bool, ...

  2. 【BFS】POJ 3278

    POJ 3278 Catch That Cow 题目:你要去抓一头牛,给出你所在的坐标和牛所在的坐标,移动方式有两种:要么前一步或者后一步,要么移动到现在所在坐标的两倍,两种方式都要花费一分钟,问你最 ...

  3. 【POJ2886】Who Gets the Most Candies?-线段树+反素数

    Time Limit: 5000MS Memory Limit: 131072K Case Time Limit: 2000MS Description N children are sitting ...

  4. BNUOJ 52325 Increasing or Decreasing 数位dp

    传送门:BNUOJ 52325 Increasing or Decreasing题意:求[l,r]非递增和非递减序列的个数思路:数位dp,dp[pos][pre][status] pos:处理到第几位 ...

  5. bnuoj 24251 Counting Pair

    一道简单的规律题,画出二维表将数字分别相加可以发现很明显的对称性 题目链接:http://www.bnuoj.com/v3/problem_show.php?pid=24251 #include< ...

  6. poj 3159 Candies 差分约束

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

  7. Who Gets the Most Candies?(线段树 + 反素数 )

    Who Gets the Most Candies? Time Limit:5000MS     Memory Limit:131072KB     64bit IO Format:%I64d &am ...

  8. BFS POJ 3278 Catch That Cow

    题目传送门 /* BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 */ #include <cstdio> #include <iostream> #inc ...

  9. bnuoj 44359 快来买肉松饼

    http://www.bnuoj.com/contest/problem_show.php?pid=44359 快来买肉松饼 Time Limit: 5000 ms     Case Time Lim ...

随机推荐

  1. 洛谷 P2048 [NOI2010]超级钢琴 || Fantasy

    https://www.luogu.org/problemnew/show/P2048 http://www.lydsy.com/JudgeOnline/problem.php?id=2006 首先计 ...

  2. URAL1389. Roadworks(dp)

    1389 算个简单的树形DP吧 不知道是不是数据太水 竟然一A了 就是对于当前节点有没有被选中就行选最优 有没有被选中的意思是有没有与它相连的边被选中 #include <iostream> ...

  3. Nginx server_name 正则泛域名反向代理两例

    最近在学习Nginx搭建负载均衡系统,感觉系统部署方式的思路瞬间开阔了很多. 负载均衡服务器的后端服务器上各自有一套功能相同的WEB管理系统,主要作用是方便的对各自服务器的IIS站点及服务器防火墙测量 ...

  4. Hadoop YARN学习之Hadoop框架演进历史简述

    Hadoop YARN学习之Hadoop框架演进历史简述(1) 1. Hadoop在其发展的过程中经历了多个阶段: 阶段0:Ad Hoc集群时代 标志着Hadoop的起源,集群以Ad Hoc.单用户方 ...

  5. R in action读书笔记(17)第十二章 重抽样与自助法

    12.4 置换检验点评 除coin和lmPerm包外,R还提供了其他可做置换检验的包.perm包能实现coin包中的部分功能,因此可作为coin包所得结果的验证.corrperm包提供了有重复测量的相 ...

  6. JavaScript——分页

  7. css选择器的对比样式代码精简

    通常就分为这三大类:* 选定所有对象.通配选择符(Universal Selector)通常不建议使用通配选择符,因为它会遍历并命中文档中所有的元素,出于性能考虑,需酌情使用一.标签选择器,以开始标签 ...

  8. 关于SQL Server数据表的五种约束

    1.主键约束(PRIMARY KEY) 主键约束可以在表中定义一个主键值,它可以唯一确定表中每一条记录,每个表中只能有一个主键约束(只能有一个主键约束的意思并不是说受主键约束的列只能有一个),并且受主 ...

  9. apropos - 在 whatis 数据库中查找字符串

    总览 (SYNOPSIS) apropos keyword ... 描述 (DESCRIPTION) apropos 命令在一些特定的包含系统命令的简短描述的数据库文件里查找关键字, 然后把结果送到标 ...

  10. wireshark mqtt协议解析

    local tcp_dissector_table = DissectorTable.get("tcp.port") local ws_dissector_table = Diss ...