(简单) 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 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?

  题目就是差分约束问题,但是这个题卡SPFA。。。。。。

代码如下:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>

using namespace std;

const int MaxN=30004;
const int MaxM=150004;
const int INF=10e9+7;

struct Node
{
    int id,val;

    Node(int _id=0,int _val=0):id(_id),val(_val) {}

    bool operator < (const Node & a) const
    {
        return val>a.val;
    }
};

struct Edge
{
    int to,next,cost;
};

Edge E[MaxM];
int head[MaxN],Ecou;
int vis[MaxN];

void Dijkstra(int lowcost[],int N,int start)
{
    priority_queue <Node> que;
    Node temp;
    int u,v,c;

    for(int i=1;i<=N;++i)
    {
        lowcost[i]=INF;
        vis[i]=0;
    }

    que.push(Node(start,0));
    lowcost[start]=0;

    while(!que.empty())
    {
        temp=que.top();
        que.pop();

        u=temp.id;

        if(vis[u])
            continue;

        vis[u]=1;

        for(int i=head[u];i!=-1;i=E[i].next)
        {
            v=E[i].to;
            c=E[i].cost;

            if(lowcost[v]>lowcost[u]+c && !vis[v])
            {
                lowcost[v]=lowcost[u]+c;
                que.push(Node(v,lowcost[v]));
            }
        }
    }
}

void init(int N)
{
    for(int i=1;i<=N;++i)
        head[i]=-1;
    Ecou=0;
}

void addEdge(int u,int v,int c)
{
    E[Ecou].to=v;
    E[Ecou].cost=c;
    E[Ecou].next=head[u];
    head[u]=Ecou++;
}

int ans[MaxN];

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

    int N,M;
    int a,b,c;

    scanf("%d %d",&N,&M);

    init(N);

    while(M--)
    {
        scanf("%d %d %d",&a,&b,&c);
        addEdge(a,b,c);
    }

    Dijkstra(ans,N,1);

    printf("%d\n",ans[N]);

    return 0;
}

时间: 2025-01-31 12:46:05

(简单) POJ 3159 Candies,Dijkstra+差分约束。的相关文章

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 3169 Layout,差分约束+SPFA。

Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 <= N <= 1,000) cows numbered 1..N standing along a straight line waiting for feed. The cows are standing in the same order as they are numbe

poj 3159 Candies dijkstra + queue

题目链接: http://poj.org/searchproblem 题目大意: 飞天鼠是班长,一天班主任买了一大包糖果,要飞天鼠分发给大家,班里面有n个人,但是学生A认为学生B比自己多的糖果数目不应该大于c,如果不满足自己的条件,学生A就会向老师告状,在这个班级里面泰迪熊的编号是1,班长的编号是n,班长想和泰迪熊的糖果相差最大,问:在满足m个学生的要求后,班长与泰迪熊的糖果相差最大是多少? 解题思路: 差分约束系统,|Xa-Xb| <= c,我们假设Xa小于Xb,把糖果的最大差当成边权,因为要

[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 3169 Layout(差分约束)

Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6549   Accepted: 3168 Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 <= N <= 1,000) cows numbered 1..N standing along a

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(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(差分约束系统)

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