POJ-3159.Candies.(差分约束 + Spfa)
Candies
| Time Limit: 1500MS | Memory Limit: 131072K | |
| Total Submissions: 40407 | Accepted: 11367 |
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 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
Hint
Source
#include <cstdio>
#include <cstring>
#include <stack>
using namespace std; const int maxn = , maxe = , INF = 0x3f3f3f3f;
struct node {
int to, w, next;
}edge[maxe];
int num, head[maxn], dist[maxn];
bool vis[maxn]; void addedge(int u, int v, int cost) {
edge[num].to = v;
edge[num].w = cost;
edge[num].next = head[u];
head[u] = num ++;
} void spfa(int start, int n) {
stack <int> Q;
for(int v = ; v <= n; v ++) {
if(v == start) {
Q.push(v);
vis[v] = true;
dist[v] = ;
}
else {
vis[v] = false;
dist[v] = INF;
}
}
while(!Q.empty()) {
int u = Q.top(); Q.pop();
vis[u] = false;
for(int i = head[u]; i != -; i = edge[i].next) {
int v = edge[i].to;
if(dist[v] > dist[u] + edge[i].w) {
dist[v] = dist[u] + edge[i].w;
if(!vis[v]) {
vis[v] = true;
Q.push(v);
}
}
}
}
} int main () {
int n, m, a, b, cost;
while(~scanf("%d %d", &n, &m)) {
num = ;
memset(head, -, sizeof head);
for(int i = ; i <= m; i ++) {
scanf("%d %d %d", &a, &b, &cost);
addedge(a, b, cost);
}
spfa(, n);
printf("%d\n", dist[n]);
}
return ;
}
或者用数组模拟栈也可以:
#include <cstdio>
#include <cstring>
using namespace std; const int maxn = , maxe = , INF = 0x3f3f3f3f;
struct node {
int to, w, next;
}edge[maxe];
int num, head[maxn], dist[maxn];
int Q[maxn];
bool vis[maxn]; void addedge(int u, int v, int cost) {
edge[num].to = v;
edge[num].w = cost;
edge[num].next = head[u];
head[u] = num ++;
} void spfa(int start, int n) {
int top = ;
for(int v = ; v <= n; v ++) {
if(v == start) {
Q[top ++] = v;
vis[v] = true;
dist[v] = ;
}
else {
vis[v] = false;
dist[v] = INF;
}
}
while(top != ) {
int u = Q[-- top];
vis[u] = false;
for(int i = head[u]; i != -; i = edge[i].next) {
int v = edge[i].to;
if(dist[v] > dist[u] + edge[i].w) {
dist[v] = dist[u] + edge[i].w;
if(!vis[v]) {
vis[v] = true;
Q[top ++] = v;
}
}
}
}
} int main () {
int n, m, a, b, cost;
while(~scanf("%d %d", &n, &m)) {
num = ;
memset(head, -, sizeof head);
for(int i = ; i <= m; i ++) {
scanf("%d %d %d", &a, &b, &cost);
addedge(a, b, cost);
}
spfa(, n);
printf("%d\n", dist[n]);
}
return ;
}
POJ-3159.Candies.(差分约束 + Spfa)的更多相关文章
- [poj 3159]Candies[差分约束详解][朴素的考虑法]
题意 编号为 1..N 的人, 每人有一个数; 需要满足 dj - di <= c 求1号的数与N号的数的最大差值.(略坑: 1 一定要比 N 大的...difference...不是" ...
- 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 差分约束
Candies Time Limit: 1500MS Memory Limit: 131072K Total Submissions: 22177 Accepted: 5936 Descrip ...
- POJ——3159Candies(差分约束SPFA+前向星+各种优化)
Candies Time Limit: 1500MS Memory Limit: 131072K Total Submissions: 28071 Accepted: 7751 Descrip ...
- POJ3159 Candies —— 差分约束 spfa
题目链接:http://poj.org/problem?id=3159 Candies Time Limit: 1500MS Memory Limit: 131072K Total Submiss ...
- POJ——1364King(差分约束SPFA判负环+前向星)
King Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 11946 Accepted: 4365 Description ...
- O - Layout(差分约束 + spfa)
O - Layout(差分约束 + spfa) Like everyone else, cows like to stand close to their friends when queuing f ...
- POJ 3159 Candies (图论,差分约束系统,最短路)
POJ 3159 Candies (图论,差分约束系统,最短路) Description During the kindergarten days, flymouse was the monitor ...
- 【poj3169】【差分约束+spfa】
题目链接http://poj.org/problem?id=3169 题目大意: 一些牛按序号排成一条直线. 有两种要求,A和B距离不得超过X,还有一种是C和D距离不得少于Y,问可能的最大距离.如果没 ...
随机推荐
- iOS设置截图背景图片透明
/** 设置图片背景为透明 */- (UIImage *)imageToTransparent { // 分配内存 const int imageWidth = self.size.width; co ...
- SSM框架-MyBatis框架数据库的增删查改操作
话不多说,在User.xml文件中主要写一下操作数据库的sql语句,增,删,查,改是最常见的数据库操作 User.xml文件下:
- Magic xpa 3.x很容易将数据导出到Excel中
Magic xpa 3.x很方便的将表中数据导出到Excel文件中,还可以自动将表中数据生成各种图表. 通过使用自带的打印数据内部事即可实现. 1.首先将打印数据任务属性设置为“是”. 2.可使用主程 ...
- Linux高级指令
一.hostname指令 作用:操作服务器的主机名(读取,设置) #hostname 作用:表示输出完整的主机名 #hostname -f 作用:表示输出当前主机名中的FQDN(权限定域名 ...
- JavaScript数组方法--concat、push
利用了两天的时间,使用typescript和原生js重构了一下JavaScript中数组对象的主要方法,可以移步github查看. 这里,按照MDN上的文档顺序,再重新学习一下数组方法吧. conca ...
- python 中list的深拷贝和浅拷贝
注意:这里提到是list的隐式转换例如 a=[1,2,3]def test(n):n[2] = n[2]*2c=a此时是浅拷贝,当调用test(c)时,可以看到a也发生了变化c=[1,4,3]a=[1 ...
- 转载:深入浅出Zookeeper(一) Zookeeper架构及FastLeaderElection机制
转载至 http://www.jasongj.com/zookeeper/fastleaderelection/: 原创文章,转载请务必将下面这段话置于文章开头处.本文转发自技术世界,原文链接 htt ...
- python-day19 Django模板,路由分发,ORM
@获取文件所有数据 request.FILES: request.POST.get('fafafa')#拿到文件名: user = request.POST.get('user',None)#用get ...
- oracle DML语句
DML语句 1. 插入数据 创建一个新表 create table new_cust as select * from customers --使用insert语句添加行 /* 确定要插入的行所在的 ...
- 操作 实例 / dom
响应式:数据改变时会触发其他联动.例如:模板中的数据绑定:计算属性的重新计算: ---------------------------------------------------- vm.$par ...