[BZOJ2330] SCOI2011 Candy | Differential constraint _ difference constraint

Source: Internet
Author: User

Difference constraint problem. Learned a little difference constraint, I think it is quite simple, consider f[u]-f[v]<=c, find and the most short-circuit of the relaxation operation Spirit, the most short run after the end for a side (V,u), obviously have d[u]<=d[v]+c, is not the above difference constraints of the formula? That translates into the shortest (long) way to do it, take the shortest circuit as an example, for each f[u]-f[v]<=c, even a V to u weight of the edge of C, and then run a minimum road, if there is a negative ring that means no solution, the words ran after the point of the D value is the solution.

For the subject, D[a]==d[b], there are d[a]-d[b]>=0, d[b]-d[a]>=0

D[A]<D[B], there are d[b]-d[a]>=1

D[A]>=D[B], there are d[a]-d[b]>=0

D[A]>D[B], there are d[a]-d[b]>=1

D[A]<=D[B], there are d[b]-d[a]>=0

This requires a group and the smallest solution, and each point is >=1, so use the longest way, each point is initially assigned to 1, and then start to throw them into the queue, run the longest road can be.

For the difference constraint, regardless of the longest road or the shortest path can be solved, but we are only a set of relative size, to use this relative size to find a set of solutions that match the problem. Like this problem, the value of each point is greater than 0, so we assign the initial values 1, to ensure that in the process of running the algorithm each point value is always greater than 0, then run the longest road.

#include <iostream> #include <cstdio> #include <memory.h> #define N 100005 using namespace std; struct edge{int e,q,next;}
ED[N*2];
int n,k,i,s,t,opt,ne=0,a[n],dis[n],que[n*2],u[n],inq[n];
Long Long ANS=0LL;
	void Add (int s,int e,int q) {ed[++ne].e=e;ed[ne].q=q;
Ed[ne].next=a[s];a[s]=ne;
	BOOL SPFA () {int head=1,tail=n,get,hh=1,tt=n,j,i,to;
	for (i=1;i<=n;i++) inq[i]=dis[i]=u[i]=1,que[i]=i;
		while (HH&LT;=TT) {get=que[head++];hh++;
		if (head>200000) head=1;
				for (J=a[get];j;j=ed[j].next) if (DIS[GET]+ED[J].Q&GT;DIS[TO=ED[J].E]) {dis[to]=dis[get]+ed[j].q;
					if (!inq[to]) {if (++u[to]>=n) return false;
					tail++;tt++;
					if (tail>200000) tail=1;
					Que[tail]=to;
				Inq[to]=1;
	}} inq[get]=0;
return true;
	int main () {scanf ("%d%d", &n,&k);
	for (i=1;i<=n;i++) a[i]=0;
		for (i=1;i<=k;i++) {scanf ("%d%d%d", &opt,&s,&t);
		Switch (opt) {case 1:add (s,t,0); add (t,s,0);	Case 2:if (s==t) {puts ("-1"); return 0;}
			Add (s,t,1);
			Case 3:add (t,s,0);
				Case 4:if (s==t) {puts ("-1"); return 0;}
			Add (t,s,1);
		Case 5:add (s,t,0);
		} if (SPFA ()) {for (i=1;i<=n;i++) ans+= (int) dis[i];
	printf ("%lld", ans);
else printf ("-1"); }


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.