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. PWA之serviceWorker应用

    1.serviceWorker介绍service worker是一段运行在浏览器后台的JavaScript脚本,在页面中注册并安装成功后,它可以拦截和处理网络请求,实现缓存资源并可在离线时响应用户的请 ...

  2. ORA-14074: partition bound must collate higher than that of the last partition

    There is a error happen in crotab: CREATE parttion report ORA-14074:ORA-14074: partition bound must ...

  3. [转]Azure 表存储和 Windows Azure SQL Database - 比较与对照

    本文转自:https://msdn.microsoft.com/library/azure/jj553018 更新时间: 2014年10月 作者:Valery Mizonov 和 Seth Manhe ...

  4. Android基础夯实--重温动画(三)之初识Property Animation

    每个人都有一定的理想,这种理想决定着他的努力和判断的方向.就在这个意义上,我从来不把安逸和快乐看作生活目的的本身--这种伦理基础,我叫它猪栏的理想.--爱因斯坦 一.摘要 Property Anima ...

  5. VBox虚拟机安装debian

    决定在win7上装一个Linux虚拟机用作Linux开发学习,虽然win7下已经有了Cygwin,还是想在一个比较完整的环境下.前面装过Ubuntu发现界面太笨重了,考虑重新换一个,同时比较喜欢apt ...

  6. 使用Win7 64位旗舰版光盘映像安装Windows Home basic 64位操作系统

    工作当中需要安装Windows home basic 64位操作系统,苦于手头没有该版本的安装光盘,也没时间下载其安装映像.因此,在现有资源“cn_windows_7_ultimate_with_sp ...

  7. ZooKeeper系列(二)

    Zookeeper的环境配置 一.Zookeeper的搭建方式 Zookeeper安装方式有三种,单机模式和集群模式以及伪集群模式. 1.单机模式:Zookeeper只运行在一台服务器上,适合测试环境 ...

  8. python游戏开发:pygame中的IO、数据

    一.python输入输出 1.输出 python一次可以打印多个变量,只要用一个逗号将每个变量隔开就可以了.比如: A = 123B = "ABC"C = 456D = " ...

  9. H5 canvas 直线和三角形

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. js 复制文字、 复制链接到粘贴板

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...