Poj 1201 intervals

Source: Internet
Author: User

Difference Constraint


The meaning of the question is simple, but it is difficult to establish constraints (Edge building ). You can initialize inf to obtain the minimum value and then output-Dis [maxn]. You can also initialize-inf to find the maximum and output dis [maxn].

When the maximum value is obtained:

Minn is the smallest and maxn is the largest.

Enter u, v Len build constraints for U-> V = Len, and finally between Minn and maxn also create I-> I-1 =-1, I-1-> I = 0.

Finally find minn-1 ~ Maxn.


The minimum value is that the edge is not changed, and the distance is changed to negative.


#include<cstdio>#include<cstring>#include<string>#include<queue>#include<algorithm>#include<map>#include<stack>#include<iostream>#include<list>#include<set>#include<cmath>#define INF 0x7fffffff#define eps 1e-6#define LL long longusing namespace std;int n,maxn,minn,m;struct lx{    int v,len;};vector<lx>g[51001];queue<int>q;void SPFA(){    bool vis[51001];    int dis[51001];    for(int i=0;i<=maxn;i++)        vis[i]=0,dis[i]=-INF;    vis[minn-1]=1,dis[minn-1]=0;    q.push(minn-1);    while(!q.empty())    {        int u=q.front();q.pop();        vis[u]=0;        for(int j=0;j<g[u].size();j++)        {            int v=g[u][j].v;            int len=g[u][j].len;//            printf("v=%d,u=%d:%d>%d+%d=\n",v,u,dis[v],dis[u],len);//                system("pause");            if(dis[v]<dis[u]+len)            {                dis[v]=dis[u]+len;                if(!vis[v])                {                    vis[v]=1;                    q.push(v);                }            }        }    }    printf("%d\n",dis[maxn]);//    for(int i=minn-1;i<=maxn;i++)//        printf("%d : %d ==\n",i,dis[i]);}int main(){    while(scanf("%d",&m)!=EOF)    {        minn=INF,maxn=0;        for(int i=0;i<51001;i++)            g[i].clear();        int u,v,len;        lx now;        while(m--)        {            scanf("%d%d%d",&u,&v,&len);            maxn=max(maxn,v);            minn=min(minn,u);            now.v=v,now.len=len;            g[u-1].push_back(now);        }        for(int i=minn;i<=maxn;i++)        {            now.v=i-1,now.len=-1;            g[i].push_back(now);            now.v=i,now.len=0;            g[i-1].push_back(now);        }//        for(int i=minn-1;i<=maxn;i++)//        {//            printf("%d-> ",i);//            for(int j=0;j<g[i].size();j++)//                printf("%d:%d  ",g[i][j].v,g[i][j].len);//            printf("\n");//        }        while(!q.empty())q.pop();        SPFA();    }}


Poj 1201 intervals

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.