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 ...
随机推荐
- codeforces 555B Case of Fugitive
题目连接: http://codeforces.com/problemset/problem/555/B 题目大意: 有n个岛屿(岛屿在一列上,可以看做是线性的,用来描述岛屿位置的是起点与终点),m个 ...
- CalService
package org.crazyit.cal; import java.math.BigDecimal; /** * 计算业务类 * * @author yangenxiong yangenxion ...
- 线程间的参数传递 分类: linux c/c++ 2014-06-15 17:48 607人阅读 评论(0) 收藏
在多线程编程中,常常需要从主线程传递参数给子线程或在主线程中获得子线程的计算结果, 若使用全局变量实现,必然需要对临界区保护,因此导致大量的切换工作造成效率的低下: 而利用进程间的参数传递可以解决这一 ...
- Android网络连接判断与检测
转自: http://www.cnblogs.com/qingblog/archive/2012/07/19/2598983.html 本文主要内容: 1)判断是否有网络连接 2)判断WIFI网络是否 ...
- js中toFixed重写
在测试原生的toFixed发现,它在个浏览器上表现不一致,并且有些值在保留小数时得到的结果并不是想要,如在chrome下测试: 所以针对toFixed方法不准的问题,我们进行方法改造: 主要思路是:对 ...
- SimpleDateForma类
package Format_daqo; import java.text.SimpleDateFormat; import java.util.Date; public class SimpleDa ...
- applicationContext.getBean(“loginEntity”)
<!-- 指定Spring需要扫描的包,并将所有是别的类放到容器中,便于识别被注解的受托管bean --> <context:component-scan base-package= ...
- iOS Programming UINavigationController
iOS Programming UINavigationController the Settings application has multiple related screens of info ...
- windows保存tomcat的控制台日志到文件
startup.bat修改:call "%EXECUTABLE%" start %CMD_LINE_ARGS%改为:call "%EXECUTABLE%" ru ...
- asterisk-java ami5 分机状态,挂机原因之类的
这些东西网上随便一找一大堆,也只是记录下自己找的.方便以后自己复制粘贴用. 最后为啦实现分机状态在web的实时更新,我选择啦使用websocket. //获得分机状态 public static St ...