"51nod 1340" Metro Loop

Source: Internet
Author: User

Topic

There is a subway loop, the loop has N stations, marking the 0,1,2,...,n-1. This loop is a one-way street, with a total of n-edged components, that is, from 0 to 1, 1 to 2,. K to K+1,...,n-2 to N-1,n-1 to 0 each has an edge. Define the distance between the two stations, the distance between station A and station B dis (A, a) refers to the full length of the =dis (A, B) (a,a+1) +dis (a+1,a+2) +, which travels from a station along a one-way street to B. +dis (k,k+1 mod N). +dis (b-1,b). Hint, a>b when the path is a->a+1-> N-1->0->...->b, note that this is a loop. Two people have provided you with some information about the loop:
Its first person provides M1 information, each message: site Ai to site Bi distance is at least Di, that is, dis (AI,BI) >=Di;
And the second person provides M2 message, each message: site AI to site bi distance is di, that is, dis (AI,BI) <=di.
In addition, it is known that the distance between two adjacent stations is at least 1 km, and the distance between all sites is an integer kilometer.
Please calculate the total length of this metro link according to the above information, and output this quantity if there is an infinite number of possible output-1.

Analysis

To see the information of a bunch of inequalities, the first thought is the differential constraint,
When the total length s is determined, we can do the difference constraint, judge whether there is a negative ring to determine the legality
Connecting Edge

    对于dis(Ai,Bi)>=Di,        如果Ai<Bi,Bi向Ai连一条的边,边权为-Di        如果Ai>Bi,Bi向Ai连一条的边,边权为s-Di    对于dis(ai,bi)<=di,        如果ai<bi,ai向bi连一条的边,边权为Di        如果ai>bi,ai向bi连一条的边,边权为-s+Di    相邻点距离至少为1,类似第一种情况

Then with the Bellman_ford, after the n-1 relaxation operation,
If the slack operation is still possible, i.e. there is an edge (a, B), Dis[a]+v (A, b) <dis[b], there is a negative ring, it is not legal.
Instead.

Consider how to find the number of scenarios,
We find the Benquan of each edge a function,
For n-1 relaxation operations, the coefficient k for each s of each point is recorded \ (dis[x][k]\), indicating that when walking to X, the coefficient of S is the minimum value of K (dis[x][k]\)
A monotonic stack is then maintained for each edge (a, B), indicating which factor of s is controlling the value of each segment s (see Program in detail)
Then for a section of the range of s, substituting s, if the function value of point A + (A, B) the function value of the >b point, then this section of the range is legal for this edge. With a prefix and an array, the number of legal edges for this range of domains is 1.
When the number of legal edges for a range of domains is the total number of edges, this range of S is legal, ans plus the number.

#include <cmath> #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring > #include <algorithm> #include <queue> #include <map> #include <bitset> #include <set> #include <vector>const int mo=1e9+7;const int n=55;using namespace Std;int t,n,m1,m2,tot,num,vv;long long Dis[N][N *2],inf;struct list{long long x,v;}    Lis[n*n*n*2];struct line{Long Long l,r,k,b;    Long long f (long long x) {return k*x+b; }}s1[n*n*n*2],t1[n*n*n*2];long Long min (Long long X,long long y) {return x<y?x:y;} Long Long max (long long X,long long y) {return x>y?x:y;}    Long long up (long long Tb,long long tk) {if (tk<0) TK=-TK,TB=-TB;    Long Long TT=TB/TK; Return tt+ (TT*TK&LT;TB);}    Long long Tb,long long tk {if (tk<0) TK=-TK,TB=-TB;    Long Long TT=TB/TK; Return tt-(TT*TK&GT;TB);}   void push (line *a,int &num,line t) {while (num && t.f (A[NUM].L) <=a[num].f (A[NUM].L)) num--; if (num) T.l=down (-(a[num].b-t.b), A[NUM].K-T.K) +1,a[num].r=t.l-1; a[++num]=t;}    Void put (Long long L,long long R) {lis[++num]= (list) {l,1}; lis[++num]= (list) {r+1,-1};}    void check (line s1,line t1) {Long long L=max (S1.L,T1.L), R=min (S1.R,T1.R);    if (l>r) return;    Line t={0,0,s1.k-t1.k,s1.b-t1.b};    if (T.F (l) <0 && T.f (r) <0) return; Put (T.F (l) >=0?l:up (-T.B,T.K), T.f (R) >=0?r:down (-T.B,T.K));}    struct edge{long long x,y,v,k; void BF () {for (int i=0;i<=2*n;i++) if (i+k>=0 && i+k<=2*n) dis[y][i+k]=min (dis[y][i+    K],DIS[X][I]+V);        } void Calc () {int n1=0,n2=0;            for (int. i=2*n;i>=0;i--) {if (DIS[X][I]&LT;INF/2) push (S1,N1, (line) {n,inf,i+k-n,dis[x][i]+v});        if (DIS[Y][I]&LT;INF/2) push (T1,N2, (line) {n,inf,i-n,dis[y][i]});    } for (int k1=1,k2=1;k1<=n1 && k2<=n2;s1[k1].r<=t1[k2].r?k1++:k2++) Check (S1[K1],T1[K2]); }}e[n<<2];bool CMP (ListX,list y) {return x.x<y.x;} int main () {for (scanf ("%d", &t);    t--;)        {scanf ("%d%d%d", &n,&m1,&m2);        memset (dis,1,sizeof (dis));        memset (lis,0,sizeof (LIS));        inf=dis[0][0],dis[0][n]=0;        tot=0;        for (int i=0;i<n;i++) e[++tot]= (Edge) {(i+1)%n,i,-1,i==n-1};        Long Long x,y,v;            for (int i=1;i<=m1;i++) {scanf ("%lld%lld%lld", &x,&y,&v);        E[++tot]= (Edge) {y,x,-v, (x>y)};            } for (int i=1;i<=m2;i++) {scanf ("%lld%lld%lld", &x,&y,&v);        E[++tot]= (Edge) {x,y,v,-(x>y)}; } for (int i=1;i<n;i++) for (int j=1;j<=tot;j++) e[j].        BF ();        for (int i=1;i<=tot;i++) E[i].calc ();        Sort (lis+1,lis+1+num,cmp);        lis[num+1]= (list) {inf+1,0};        Long Long sum=0,ans=0;        for (int i=1;i<=num;i++) ans+= ((SUM+=LIS[I].V) ==tot) * (lis[i+1].x-lis[i].x);    printf ("%lld\n", Ans>=inf/4?-1:ans);}} 

"51nod 1340" Metro Loop

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.