poj 3159 Candies

题目链接:http://poj.org/problem?id=3159


Candies

Time Limit: 1500MS   Memory Limit: 131072K
Total Submissions: 22516   Accepted: 6047

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 throughN.
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

Hint

32-bit signed integer type is capable of doing all arithmetic.

Source

POJ Monthly--2006.12.31, Sempr

最短路径的经典题,用堆优化一下就完了。。算做是稍微复习下吧。

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<queue>
#include<stack>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#include<cstdlib>
#define CLR(A) memset(A,0,sizeof(A))
using namespace std;
struct Node{
    int k,w;
};
bool operator <(const Node &a,const Node &b){return a.w>b.w;}
priority_queue<Node> q;
vector<vector<Node> > g;
bool used[30010]={0};
int main(){
    int n,m;
    Node p;
    while(~scanf("%d%d",&n,&m)){
        g.clear();
        g.resize(n+1);
        CLR(used);
        for(int i=1;i<=m;i++){
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            p.k=b;p.w=c;
            g[a].push_back(p);
        }
        p.k=1;p.w=0;
        q.push(p);
        while(!q.empty()){
            p=q.top();q.pop();
            if(used[p.k]) continue;
            used[p.k]=true;
            if(p.k==n) break;
            Node tmp;
            for(int i=0;i<g[p.k].size();i++){
                tmp.k=g[p.k][i].k;
                if(used[tmp.k]) continue;
                tmp.w=p.w+g[p.k][i].w;
                q.push(tmp);
            }
        }
        printf("%d\n",p.w);
    }
    return 0;
}
时间: 2024-12-23 10:29:50

poj 3159 Candies的相关文章

[2016-04-08][POJ][3159][Candies]

时间:2016-04-08 18:05:09 星期五 题目编号:[2016-04-08][POJ][3159][Candies] 题目大意:n个小孩,m条信息,每条信息a ,b ,c,表示b小孩得到的糖不能比a小孩c个,问n小孩比1小孩最多能多多少个, 分析:直接就是求1到n的最短路(如果不是最短路,那么其他边,总有一种情况不能满足最短路这条边的情况,),直接跑一次Dijkstra 遇到的问题:使用'vector'存图TLE了,改成用数组实现的邻接表才A #include <queue> #i

POJ 3159 Candies 差分约束

链接:http://poj.org/problem?id=3159 Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 24852   Accepted: 6737 Description During the kindergarten days, flymouse was the monitor of his class. Occasionally the head-teacher brought the

POJ 3159 Candies(差分约束系统)

题目地址:POJ 3159 第一发差分约束的题..就当作最短路来做了...直接建图+spfa..不过我用的spfa+slf优化都超时..看了讨论区里的..把spfa换成栈就过了... 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include

poj 3159 Candies(dijstra优化非vector写法)

题目链接:http://poj.org/problem?id=3159 题意:给n个人派糖果,给出m组数据,每组数据包含A,B,c 三个数,意思是A的糖果数比B少的个数不多于c,即B的糖果数 - A的糖果数<= c . 最后求n比1最多多多少糖果. 可以从条件中的 B-A<=c及B<=A+c最后要达成这个条件就是要当B>A+c时B=A+c即可 所以差不多就是求最短路.这题中还有一些优化比如 if(vis[u]) continue;这个避免了u点的重复查找 #include <

POJ 3159 Candies(SPFA+栈)差分约束

题目链接:http://poj.org/problem?id=3159 题意:给出m给 x 与y的关系.当中y的糖数不能比x的多c个.即y-x <= c  最后求fly[n]最多能比so[1] 多多少糖? 差分约束问题, 就是求1-n的最短路,  队列实现spfa 会超时了,改为栈实现,就可以 有负环时,用栈比队列快 数组开小了,不报RE,报超时 ,我晕 #include <iostream> #include <cstdlib> #include <cstdio>

poj 3159 candies (差分约束 spfa+stack)

http://poj.org/problem?id=3159 题意:一个班有n个人 每人分到若干糖果 且u的糖果数不能比v少w个 求第1个人与第n个人最大数量差 照着模板spfa+queue果断tle了 之后照着题解说的把queue改成stack就过了 但是还不明白为什么会快 而且如果用数组直接模拟会比stl更快 #include<cstdio> #include<cstring> #include<iostream> #include<algorithm>

POJ 3159 Candies(SPFA+栈)

题目链接:http://poj.org/problem?id=3159 题意:给出m给 x 与y的关系,其中y的糖数不能比x的多c个,即y-x <= c  最后求fly[n]最多能比so[1] 多多少糖? 差分约束问题, 就是求1-n的最短路,  队列实现spfa 会超时了,改为栈实现,即可 有负环时,用栈比队列快 数组开小了,不报RE,报超时 ,我晕 #include <iostream> #include <cstdlib> #include <cstdio>

POJ 3159 Candies(差分约束+spfa+链式前向星)

题目链接:http://poj.org/problem?id=3159 题目大意:给n个人派糖果,给出m组数据,每组数据包含A,B,C三个数,意思是A的糖果数比B少的个数不多于C,即B的糖果数 - A的糖果数<=C . 最后求n 比 1 最多多多少颗糖果. 解题思路:经典差分约束的题目,具体证明看这里<数与图的完美结合——浅析差分约束系统>. 不妨将糖果数当作距离,把相差的最大糖果数看成有向边AB的权值,我们得到 dis[B]-dis[A]<=w(A,B).看到这里,我们可以联想到

(简单) POJ 3159 Candies,Dijkstra+差分约束。

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 ofte