POJ 3159 Candies (difference constraint system), pojcandies
Address: POJ 3159
The first difference constraint .. It is done as the shortest path... Directly create a graph + spfa .. However, all the spfa + slf optimizations I use have timed out .. I saw it in the discussion board .. Replace spfa with a stack...
The Code is as follows:
#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <stdlib.h>#include <math.h>#include <ctype.h>#include <queue>#include <map>#include <set>#include <algorithm>using namespace std;const int INF=0x3f3f3f3f;int d[40000], vis[40000], source, sink, head[40000], cnt, q[400000];struct node{ int u, v, w, next;} edge[200000];void add(int u, int v, int w){ edge[cnt].v=v; edge[cnt].w=w; edge[cnt].next=head[u]; head[u]=cnt++;}void spfa(){ memset(vis,0,sizeof(vis)); memset(d,INF,sizeof(d)); d[source]=0; int top=0; q[++top]=source; while(top) { int u=q[top--]; vis[u]=0; for(int i=head[u]; i!=-1; i=edge[i].next) { int v=edge[i].v; if(d[v]>d[u]+edge[i].w) { d[v]=d[u]+edge[i].w; if(!vis[v]) { vis[v]=1; q[++top]=v; } } } } printf("%d\n",d[sink]);}int main(){ int n, m, i, u, v, w; scanf("%d%d",&n,&m); source=1; sink=n; memset(head,-1,sizeof(head)); cnt=0; while(m--) { scanf("%d%d%d",&u,&v,&w); add(u,v,w); } spfa(); return 0;}
How can we figure the longest path? The difference constraint system is what Shenma means
There are several methods for the longest path, such as converting all edge vertices into negative ones into positive ones and then doing the shortest path.
Or convert the number greater than or smaller than the number in the process of finding the shortest path.
A difference constraint system is used to solve a problem when an inequality is satisfied.
Generally, the problem is abstracted into an inequality, and then the graph is created using the edge of the inequality, and finally the shortest path or shortest path is used.
NOI1999 differential constraint system
Your definition of zero point is incorrect. If s [I] is set to the sum of the first I bit 1, then 0 <= s [1]-s [0] <= 1
Because the graph I created has an edge with a weight of 0 from 0 to any point. Why ??
0 <= s [1]-s [0] <= 1. You have ignored this constraint...