码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 3159 Candies(查分约束)

时间:2015-09-18 13:32:39      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:

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 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.
 
题意:flymouse是幼儿园的班长,今天老师让他发糖果,每个孩子都有自己的小私心,孩子a认为孩子b最多只能比自己多出c个糖果,不然的话他会觉得不公平,会向老师告状,snoopy是flymouse的同班同学,flymouse总是想和snoopy比较糖果个数,他想在满足所有孩子愿望的同时和snoopy的糖果个数差别最大,snoopy是1号,flymouse是n号(分发糖果是从1号开始的)。
 
那么最短路专题到这里就又引入了一个新的知识点了:查分约束(这边简单介绍一下)。
查分约束就是给出形如x-y<=k1的约束,问是否有满足的解,如:b-a<=k1,c-b<=k2,c-a<=k3,求出c-a的最大值,由前两个式子可得c-a<=k1+k2,那么现在c-a的最大值不就变成了min(k3,k1+k2),然后查分约束就变成了最短路问题。。。。。。
技术分享
注意这道题用队列会超时,用栈就不会(不知道为啥)。。。代码就是一个Spfa的一个模板,只是用的是栈
#include<stdio.h>
#include<stack>
#include<algorithm>
using namespace std;

const int N=30010;
const int INF=0x3f3f3f3f;

struct node
{
    int v, flow, next;
}no[10*N];
int dist[N], vis[N], head[N], n, k;

void Init()
{
    int i;

    for (i = 1; i <= n; i++)
    {
        vis[i] = 0;
        dist[i] = INF;
        head[i] = -1;
    }

    k = 0;
}

void Add(int a, int b, int c)
{
    no[k].v = b;
    no[k].flow = c;
    no[k].next = head[a];

    head[a] = k++;
}

void Spfa()
{
    int v, u, i;
    stack<int>S;

    S.push(1);
    dist[1] = 0;

    while (!S.empty())
    {
        v = S.top(); S.pop();

        for (i = head[v]; i != -1; i=no[i].next)
        {
            u = no[i].v;

            if (dist[u] > dist[v]+no[i].flow)
            {
                dist[u] = dist[v] + no[i].flow;

                if (!vis[u])
                {
                    S.push(u);
                    vis[u] = 1;
                }
            }
        }

        vis[v] = 0;
    }
}

int main ()
{
    int m, a, b, c;

    scanf("%d%d", &n, &m);

    Init();

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

    Spfa();

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

    return 0;
}

POJ 3159 Candies(查分约束)

标签:

原文地址:http://www.cnblogs.com/syhandll/p/4818824.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!