首页 > 其他 > 详细

zoj 1508 Intervals 差分约束系统

时间:2015-07-31 14:57:34      阅读:212      评论:0      收藏:0      [点我收藏+]
#include "stdio.h"
#include "string.h"
#include "algorithm"
using namespace std;

struct name
{
    int u,v,w;
}e[50005];

int dist[50005];

int main()
{
    int i,j,k,n,u,v,w,mx,mn,t;
    bool f;
    while(~scanf("%d",&n))
    {
        for(i=0,mx=0,mn=1<<30;i<n;i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            mx=max(v,mx);
            mn=min(u,mn);
            e[i].u=v;
            e[i].v=u-1;
            e[i].w=-w;
        }

        for(i=0;i<=mx;i++)
                dist[i]=0;
        f=1;
        while(f)
        {
            f=0;
            for(i=0;i<n;i++)
            {
                t=dist[e[i].u]+e[i].w;
                if(t<dist[e[i].v])
                    {
                        dist[e[i].v]=t;
                        f=1;
                    }
            }

            for(i=mn;i<=mx;i++)
            {
                if(dist[i]>dist[i-1]+1)
                {
                    dist[i]=dist[i-1]+1;
                    f=1;
                }

            }

            for(i=mn;i<=mx;i++)
            {
                if(dist[i]<dist[i-1])
                {
                    dist[i-1]=dist[i];
                    f=1;
                }

            }
        }
        printf("%d\n",dist[mx]-dist[mn-1]);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

zoj 1508 Intervals 差分约束系统

原文:http://blog.csdn.net/xinag578/article/details/47168651

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!