Candies(POJ 3159)

Time Limit : 3000/1500ms (Java/Other)   Memory Limit : 262144/131072K (Java/Other)

Total Submission(s) : 4   Accepted Submission(s) : 2

Problem 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

<p>The input contains a single test cases. The test cases starts with a line with two integers <i>N</i> and <i>M</i> not exceeding 30 000 and 150 000 respectively. <i>N</i> is the number of kids in the class and the kids were numbered 1 through <i>N</i>. snoopy and flymouse were always numbered 1 and <i>N</i>. Then follow <i>M</i> lines each holding three integers <i>A</i>, <i>B</i> and <i>c</i> in order, meaning that kid <i>A</i> believed that kid <i>B</i> should never get over <i>c</i> candies more than he did.</p>

Output

<p>Output one line with only the largest difference desired. The difference is guaranteed to be finite.</p>

Sample Input

2 2 1 2 5 2 1 4

Sample Output

5

Source

PKU

题意:有N个孩子分糖果,表示第B个学生比第A个学生多分到的糖果数目,不能超过C。求第N个学生最多比第1个学生能多分几个糖果。

其实就是求1到N的最短路,其中A B C即A->B的一条有向边,权值为C。假设W(A,B)=2,W(B,C)=3,W(A,C)=100,松弛操作后,W(A,C)=5符合题意。

//#include<bits/stdc++.h>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
struct Node
{
    int e;//有向边终点
    int w;//weight权值
    friend bool operator <(const Node &a,const Node &b)
    {
        return a.w>b.w;
    }
};
int main()
{
    int n,m,s,t;
    scanf("%d%d",&n,&m);
    priority_queue<Node>que;
    vector<vector<Node> >v;//构造邻接表
    v.resize(n+1);//漏掉无法AC
    int vis[30005]= {0};
    Node p;
    while(m--)
    {
        int a,b,x;
        scanf("%d%d%d",&a,&b,&x);
        p.e=b,p.w=x;
        v[a].push_back(p);
    }
    p.e=1,p.w=0;
    que.push(p);
    while(!que.empty())
    {
        p=que.top(),que.pop();
        vis[p.e]=1;//一定要写在外面
        if(p.e==n)
            break;//到达t点即可结束
        for(int i=0,j=v[p.e].size(); i<j; i++)
        {
            Node q;
            q.e=v[p.e][i].e;
            if(vis[q.e])
                continue;//过滤重复的
            q.w=p.w+v[p.e][i].w;
            que.push(q);
        }
    }
    printf("%d\n",p.w);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41061455/article/details/81366513