POJ3159 Candies(差分约束+SPFA的栈实现)

Candies
Time Limit: 1500MS Memory Limit: 131072K
Total Submissions: 29256 Accepted: 8104

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

这是kuangbin带你飞里面最短路练习的一道题,说实话,如果英语不好,看不懂题目,根本无法理解到底怎么弄,后来我看了看别人写的翻译,然后自己再回去题目仔细琢磨琢磨才明白原来是这样的一个意思。

题目大意就是,班任给了糖果,班长flymouse要去分给同学,同学都有想法,认为B所拥有的糖果不应该比A多C个以上,否则就去班任那告状。在满足各个同学的要求的前提下,建好一个图。然后flymouse喜欢和snoopy比糖果数,flymouse要最大的多于snoopy的糖果数,但是题目并没有告诉你flymouse是几号同学,snoopy是几号同学,所以说你要在建好的图里面找出最长的那一条路径,那么只需要把snoopy当成编号1的同学,flymouse当成第n个同学,他们之间的路径就是最长的了,因为这个约束条件啊,本来就是 B - A <= C,所以我们建图也是直接用c来建的,求出来的路每两个节点间的路都是最大的 — C,所以就是最长的了。

这里还要讲讲差分约束,百度一下一大堆这方面的。差分约束就是指在很多个约束条件下面,你要求出一个可行的方案。这道题的约束条件就是B - A <= C,为什么会用最短路呢?因为最短路里面的松弛操作就是  :如果  dist[终点] > dist[起始点] + cost(路长),那么就更新dist[终点],这是不是跟最短路的一模一样啊,当B - A > C也就是B > A + C,那么就不符合约束,要更新。

网上SPFA的模板真的是好少啊,也好零散啊,本弱鸡当时写SPFA用队列实现就东拼西凑了一个下午,还要慢慢理解,现在写这个栈也是一脸懵逼啊,因为我还停留在队列那个思想,队列是可以从最前面取出一个状态,把可以松弛的放到队列尾部,如果队列为空,就可以提早结束而不用像bellman那样一定要松弛n-1次。但是栈这怎么取出啊,这是先进后出的啊,怎样取出一个状态呢?但是此栈非彼栈!!!这是我们自己手写的栈,不是STL那个栈,我们这个栈通过更改栈顶坐标即可,实际上我们并没有取出来任何东西,只是每次把栈顶坐标向下移动了一格,到时候要加入路径就直接覆盖就好了。

哦对了,我自己有SPFA 队列模板,但是网上都说队列会超时,真是神奇,用栈才不会超时,而且要是手写栈。

代码如下:(东拼西凑)

#include<stdio.h>                //spfa基本上要这些头文件
#include<string.h>
using namespace std;

const int maxn=30005;
const int maxm=150005;
const int INF=0x3f3f3f3f;

int head[maxn];//每个节点的头指针 
int size;//边的个数 
int dist[maxn];//起始点到各点距离 
int vis[maxn];//在队列标志 
int Q[maxn];//手写栈 

struct node
{
	int to,next;
	int v;
}edge[maxm];

void init()
{
    memset(head,-1,sizeof(head));
    size=0;
}

void add(int a,int b,int v)
{    //有向图只需要前一半
    edge[size].to=b;
    edge[size].v=v;
    edge[size].next=head[a];
    head[a]=size++;
}

void SPFA(int start,int n)
{
    int top=0;
    for(int v=1;v<=n;v++)//???
    {
        if(v==start)
        {
            Q[top++]=v;
            vis[v]=true;
            dist[v]=0;
        }
        else
        {
            vis[v]=false;
            dist[v]=INF;
        }
    }
    while(top!=0)
    {
        int u=Q[--top];
        vis[u]=false;
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].to;
            if(dist[v]>dist[u]+edge[i].v)
            {
                dist[v]=dist[u]+edge[i].v;
                if(!vis[v])
                {
                    vis[v]=true;
                    Q[top++]=v;
                }
            }
        }
    }
}

int main()
{
	int T;
	int n,m;
	int a,b,c;
	scanf("%d%d",&n,&m);
	init();
	for(int i=0;i<m;i++)
	{
		scanf("%d%d%d",&a,&b,&c);
		add(a,b,c);
	}	
	SPFA(1,n);
	printf("%d\n",dist[n]);
	return 0;	
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值