当前位置: 代码迷 >> 综合 >> POJ 3159 Candies(差分约束+SPAF)
  详细解决方案

POJ 3159 Candies(差分约束+SPAF)

热度:5   发布时间:2024-02-21 11:22:39.0

题意:

给n个小朋友分发糖果,但小朋友们之间有嫉妒心。接下来m行,每行三个数,分别表示小朋友A希望B得到的糖果不能比他多x个。要求你计算在满足所有小朋友的条件的情况下最多需要准备多少颗糖。

题目:

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 A, B 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.

分析:

1.这道题目是典型的差分约束系统,小于等于是最短路 大于等于是最长路,核心在于:xj-xi<=bk,会发现它类似最短路中的三角不等式d[v]<=d[u]+w[u,v],即d[v]-d[u]<=w[u,v],即从u指向v建边,num[B]<=num[A]+x。求最大值的话就是跑1-n的最短路,对应求最小值就是跑1-n的最长路。此题用队列的话会T掉,可以改成栈或者想办法优化。
2.关于差分约束选择队列还是堆栈:当存在回路,SPFA的队列实现会超时,堆栈实现可以,堆栈实现SPFA(有时候堆栈确实比较快)
都大三的老阿姨了,差分约束还不熟练,加油吧,查缺补漏中。。。。

AC代码

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int inf=0x3f3f3f3f;
const int M=3e4+10;
const int N=15e4+10;
int n,m,k,a,b,c;
int vis[M]/**在队列标志*/;
int head[M]/**每个结点的头指针(或者编号)*/;
int q[M]/**堆栈*/,dis[M];
struct node
{
    int b;int c;int next;
}edge[N];void add(int a,int b,int c)//加边
{
    edge[k].b=b;edge[k].c=c;edge[k].next=head[a];head[a]=k++;
}
void SPFA(int start,int n)
{
    int top=0;for(int i=1;i<=n;i++)//初始化{
    if(i==start){
    q[top++]=i;//入栈vis[i]=true;dis[i]=0;}else{
    vis[i]=false;dis[i]=inf;}}while(top!=0){
    int a=q[--top];vis[a]=false;for(int i=head[a];i!=-1;i=edge[i].next){
    int b=edge[i].b;if(dis[b]>dis[a]+edge[i].c){
    dis[b]=dis[a]+edge[i].c;if(!vis[b]){
    vis[b]=true;q[top++]=b;}}}}
}
int main()
{
    while(~scanf("%d%d",&n,&m)){
    k=0;//加边计数,这个不要忘memset(head,-1,sizeof(head));for(int i=0;i<m;i++){
    scanf("%d%d%d",&a,&b,&c);add(a,b,c);}SPFA(1,n);//printf("*****\n");printf("%d\n",dis[n]);//此处求n比1最多多多少糖果}return 0;
}
/**给n个人派糖果,给出m组数据,每组数据包含A、B、c 三个数, 意思是A的糖果数比B少的个数不多于c,即B的糖果数 - A的糖果数<= c 。 最后求n 比 1 最多多多少糖果。*/