poj 1201 Intervals( spfa + 差分约束)

首先感谢 y @ The Angry Teletubbies 

http://www.cppblog.com/y346491470/articles/152876.html

某牛人的总结

http://www.cnblogs.com/sysuwhj/archive/2011/01/26/1945773.html

#include<stdio.h>
#include<stack>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 500005
#define inf 99999999

int head[N],cost[N],nxt[N],pnt[N],e;
int dist[N],vis[N];
void add(int u,int v,int c)
{
    pnt[e]=v;cost[e]=c;nxt[e]=head[u];head[u]=e++;
}
int spfa(int s,int e)
{

    for(int i=e;i<=s;i++)
    {
        dist[i]=-1;vis[i]=0;
    }
    dist[s]=0;vis[s]=1;
    int Q[N],top=1;Q[0]=s;
    while(top)
    {
        int u=Q[--top];vis[u]=0;
        for(int i=head[u];i!=-1;i=nxt[i])
        {
            int v=pnt[i];
            if(dist[v]<dist[u]+cost[i])
            {
                dist[v]=dist[u]+cost[i];
                if(!vis[v])
                {
                    vis[v]=1;
                    Q[top++]=v;
                }
            }
        }
    }
    return dist[e];
}

int main()
{
    int n;scanf("%d",&n);
    int l=inf,r=-1;
    memset(head,-1,sizeof(head));
    e=0;
    while(n--)
    {
        int u,v,c; scanf("%d%d%d",&u,&v,&c);
        add(v,--u,c);
        l=min(l,u); r=max(r,v);
    }
    for(int i=l;i<r;i++)
    {
        add(i+1,i,0);
        add(i,i+1,-1);
    }
    cout<<spfa(r,l)<<endl;
    return 0;
}
Just a little, maybe change the world
原文地址:https://www.cnblogs.com/skyming/p/2489429.html