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>
#include <cstring>
#include <queue>
#include <stack>
#include <algorithm>
const int N = 210;
const int maxn = 30100;
const int maxm = 200000;
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define init(a) memset(a,0,sizeof(a))
#define MIN INT_MIN
#define MAX INT_MAX
#define LL long long
using namespace std;
int max(int a,int b){if(a>b)return a; else return b;}
int min(int a,int b){if(a<b)return a; else return b;}
const int INF=0x3f3f3f3f;
struct node
{
    int v,w;
    int next;
}edge[maxm];
int head[maxn];
bool vis[maxn];
int dis[maxn];
int cnt;
void add(int a,int b,int w)//加边
{
    edge[cnt].v=b;
    edge[cnt].w=w;
    edge[cnt].next=head[a];
    head[a]=cnt++;
}
void SPFA(int s,int n)
{
    //stack<int>stk;
    int stk[100000];
    int top = 0;

    //stk.push(s);
    stk[top++] = s;
    FOR(i,1,n+1)
    {dis[i] = INF;
    vis[i] = 0;
    }
    dis[s] = 0;
    vis[s] = 1;
    while(top!=0)
    {
        int u=stk[--top];
        //stk.pop();
        vis[u]=false;
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].v;
            if(dis[v]>dis[u]+edge[i].w)
            {
                dis[v]=dis[u]+edge[i].w;
                if(!vis[v])
                {
                    vis[v]=true;
                    stk[top++] = v;
                }
            }
        }

    }
}
void initt()
{
    cnt=0;
    memset(head,-1,sizeof(head));
}
int main()
{
    int n,m;
    int a,b,c;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        initt();
        while(m--)
        {
            scanf("%d%d%d",&a,&b,&c);
            add(a,b,c);
        }
        SPFA(1,n);
        printf("%d\n",dis[n]);
    }
    return 0;
}
时间: 2024-08-06 06:25:19

POJ 3159 Candies(SPFA+栈)差分约束的相关文章

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

POJ 3159 Candies(查分约束)

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 oft

POJ 3169 Layout (图论-差分约束)

Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6574   Accepted: 3177 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 1201 Intervals(图论-差分约束)

Intervals Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 20779   Accepted: 7863 Description You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn. Write a program that: reads the number of intervals, their end po

[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(差分约束+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(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 差分约束

链接: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