Candies--差分约束系统

Candies
Time Limit: 1500MS Memory Limit: 131072K
Total Submissions: 29682 Accepted: 8224

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.

Source

题目链接:http://poj.org/problem?id=3159


我对于这个题实在是不知道说什么了,前天下午我用了一个下午的时间学了差分约束系统,并复习了spfa算法,自我感觉良好,气势冲冲的去挑了一个差分约束系统的入门题开始做,然后TLE!!!我的天呐,怎一个尬字了得QAQ,我仔细的检查了我的算法,恩,没有错,咦,难道还有比spfa快的最短路算法,就算是有,那也应该是很高级的吧,并应该有那么多人做出来啊,那一刻,我开始怀疑人生了。。。然后实在是没有找出来哪里可以优化,问了小豪菊苣,菊苣说这个题让我不要用队列来做,让我试着模拟队列,我想难道是stl费时间了?不应该卡这个吧,本着人道主义原则,手敲了一发队列,恩,赤裸裸的wa,豪巨又发话了,恩,这个题数据挺多的,我就把数组开到了900万,恩,红果果的TLE-_-|||好吧,我又换成了循环队列,咦,wa,然而已经是半夜,豪巨已经呼呼了,我就去问了我亲爱的静哥,然后静哥说让我用优先队列试试,这好办,我又换成了优先队列,然后TLE。。。优先队列的内部实现我也不会啊,实属无奈,看了题解,我到现在都不知道为什么用栈来代替队列就对了。。。还快了那么多,1500ms的题500ms多一点就过了,我的内心是崩溃的TAT。。


差分约束系统呢,网上一搜一大堆,不过不太好懂,我在这里放上几个链接,我就是看这个看懂的

http://www.cnblogs.com/khbcsu/p/3877659.html

http://blog.csdn.net/hjt_fathomless/article/details/52463723

http://www.cnblogs.com/void/archive/2011/08/26/2153928.html


这几个讲的挺明白的,我也大体懂了差分约束系统是用来解决什么类的题目的。下面附上题解链接,到底为什么用栈呢!

http://www.cnblogs.com/FreeAquar/archive/2011/07/07/2100575.html


最后附上渣渣的代码。。。若有大神路过,谢大神指正

#include <cstdio>
#include <cstring>
#include <iostream>
#define inf 0x3f3f3f3f
using namespace std;
struct node{
    int pre;
    int v,w;
}edge[1100000];
int p[1000000],nEdge;
bool vis[1000000];
int dis[1000000];
int sta[1000000];
void connect(int u,int v,int w){//前向星
    nEdge++;
    edge[nEdge].v=v;
    edge[nEdge].w=w;
    edge[nEdge].pre=p[u];
    p[u]=nEdge;
};
void Init(){//初始化
    memset(p,-1,sizeof(p));
    memset(dis,inf,sizeof(dis));
    memset(vis,false,sizeof(vis));
    nEdge=0;
}
void spfa(int s,int n){//最短路
    int top=0;
    dis[s]=0;
    sta[top]=1;
    top++;
    while(top){
        int t=sta[top-1];
        top--;
        vis[t]=false;
        for(int i=p[t];~i;i=edge[i].pre){
            if(dis[edge[i].v]>dis[t]+edge[i].w){
                dis[edge[i].v]=dis[t]+edge[i].w;
                if(!vis[edge[i].v]){
                    sta[top++]=edge[i].v;
                    vis[edge[i].v]=true;
                }
            }
        }
    }
    printf("%d\n",dis[n]);
}
int main(){
    int n,m;
    while(~scanf("%d%d",&n,&m)){
        Init();
        for(int i=0;i<m;i++){
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            connect(a,b,c);
        }
        spfa(1,n);
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值