Simple POJ 3169 Layout, differential constraint +SPFA.

Source: Internet
Author: User

Description

Like everyone else, the cows like to stand close to their friends when the queuing for feed. FJ has n (2 <= n <= $) cows numbered 1..N standing along a straight line waiting for feed. The cows was standing in the same order as they was numbered, and since they can be rather pushy, it was possible that Or more cows can line up at exactly the same location (that's, if we think of each cow as being located at some Coordinat E on a number line and then it was possible for the or more cows to share the same coordinate).

Some cows like each of the other and want to is within a certain distance of each and the line. Some really dislike each, and want to is separated by at least a certain distance. A List of ML (1 <= ml <=) constraints describes which cows like all other and the maximum distance by which They may separated; A subsequent list of MD constraints (1 <= MD <=) tells which cows dislike each other and the minimum distance By which they must is separated.

Your job is to compute, if possible, the maximum possible distance between cow 1 and cow N that satisfies the distance con    Straints.  Test instructions is xi-xj<c and then asks for the maximum value of xn-x1, the difference constraint problem ... Build and then SPFA just fine ... The code is as follows:
#include <stdio.h>#include<string.h>#include<iostream>#include<algorithm>#include<vector>#include<queue>#include<Set>#include<map>#include<string>#include<math.h>#include<stdlib.h>#include<time.h>using namespacestd;Const intmaxn=1010;Const intmaxm=30100;Const intinf=1000000009;structedge{intTo,next,cost;}; Edge E[MAXM];intHead[maxn],ecou;BOOLVIS[MAXN];intCOUNODE[MAXN];voidInitintN) {Ecou=0;  for(intI=1; i<=n;++i) {Head[i]=-1; Counode[i]=0; Vis[i]=0; }}voidAddedge (intUintVintW) {e[ecou].to=v; E[ecou].cost=W; E[ecou].next=Head[u]; Head[u]=ecou++;}BOOLSPFA (intLowcost[],intNintstart) {    intt,v; Queue<int>que;  for(intI=1; i<=n;++i) lowcost[i]=INF; Lowcost[start]=0;    Que.push (start); Counode[start]=1; Vis[start]=1;  while(!Que.empty ()) {T=Que.front ();        Que.pop (); Vis[t]=0;  for(inti=head[t];i!=-1; i=E[i].next) {v=e[i].to; if(lowcost[v]>lowcost[t]+e[i].cost) {Lowcost[v]=lowcost[t]+E[i].cost; if(!Vis[v]) {Vis[v]=1; COUNODE[V]+=1;                    Que.push (v); if(counode[v]>N)return 0; }            }        }    }    return 1;}intANS[MAXN];intMain () {//freopen ("In.txt", "R", stdin); //freopen ("OUT.txt", "w", stdout);        intN,ML,MD; intA,b,c; scanf (" %d%d%d",&n,&ml,&MD);    Init (N);  for(intI=1; i<=ml;++i) {scanf (" %d%d%d",&a,&b,&c);    Addedge (A,B,C); }     for(intI=1; i<=md;++i) {scanf (" %d%d%d",&a,&b,&c); Addedge (B,a,-c); }     for(intI=1; i<=n-1;++i) Addedge (i+1I0); if(! SPFA (Ans,n,1)) printf ("-1\n"); Else if(ans[n]!=INF) printf ("%d\n", Ans[n]); Elseprintf ("-2\n"); return 0;}
View Code

Simple POJ 3169 Layout, differential constraint +SPFA.

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.