差分约束(例题整理)

例题一:HDU 3440 

样例输入:

3
4 4
20 30 10 40
5 6
20 34 54 10 15
4 2
10 20 16 13 

样例输出:

Case 1: 3
Case 2: 3
Case 3: -1

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cmath>
  4 #include<queue>
  5 #include<cstring>
  6 #include<cstdlib>
  7 #include<algorithm>
  8 using namespace std;
  9 const int N=101000;
 10 const int inf=0x3f3f3f3f;
 11
 12 int u,v,w,n,js,T,ha,cas;
 13 int head[N],dis[N],vis[N],s[N];
 14 queue<int > q;
 15
 16 struct node {
 17     int nxt,v,val;
 18 }e[N];
 19
 20 struct edge {
 21     int pos,w;
 22 }a[N];
 23
 24 inline int read() {
 25     int n=0,f=1;char ch=getchar();
 26     while (ch<‘0‘ || ch>‘9‘) {if(ch==‘-‘) f=-1;ch=getchar();}
 27     while (ch<=‘9‘ && ch>=‘0‘) {n=(n<<3)+(n<<1)+ch-‘0‘;ch=getchar();}
 28     return n*f;
 29 }
 30
 31 inline int cmp(edge x,edge y) {
 32     return x.w<y.w;
 33 }
 34
 35 inline void add_edge(int u,int v,int w) {
 36     e[++js].v=v,e[js].val=w;
 37     e[js].nxt=head[u],head[u]=js;
 38 }
 39
 40 inline int spfa(int x,int t) {
 41     memset(dis,0x3f,sizeof(dis));
 42     memset(vis,0,sizeof(vis));
 43     memset(s,0,sizeof(s));
 44
 45     dis[x]=0,vis[x]=1;
 46     q.push(x),s[x]++;
 47
 48     while (!q.empty()) {
 49         int u=q.front();
 50         q.pop();
 51         vis[u]=0;
 52         for(int i=head[u];i;i=e[i].nxt) {
 53             int v=e[i].v;
 54             if(dis[v]>dis[u]+e[i].val) {
 55                 dis[v]=dis[u]+e[i].val;
 56                 if(!vis[v]) {
 57                     s[v]++;
 58                     vis[v]=1,q.push(v);
 59                     if(s[v]>n) {
 60                         printf("-1\n");
 61                         exit(0);
 62                     }
 63                 }
 64             }
 65         }
 66     }
 67     return dis[t];
 68 }
 69
 70 inline int build () {
 71     sort(a+1,a+n+1,cmp);
 72     memset(e,0,sizeof(e));
 73     for(int i=1;i<n;++i) {
 74         add_edge(i+1,i,-1);
 75         int u=min(a[i].pos,a[i+1].pos);
 76         int v=max(a[i].pos,a[i+1].pos);
 77         int w=abs(a[i].pos-a[i+1].pos);
 78         if(w>ha) return 0;
 79         add_edge(u,v,ha);
 80     }
 81     return 1;
 82 }
 83
 84 int main() {
 85     T=read();
 86     while (T--) {
 87         n=read(),ha=read();cas++;
 88         for(int i=1;i<=n;++i) a[i].w=read(),a[i].pos=i;
 89         printf("Case %d: ",cas);
 90         if(!build()) {
 91             printf("-1\n");
 92             continue;
 93         }
 94         u=min(a[1].pos,a[n].pos),v=max(a[1].pos,a[n].pos);
 95         printf("%d\n",spfa(u,v));
 96     }
 97     return 0;
 98 }
 99
100 /*
101 3
102 4 4
103 20 30 10 40
104 5 6
105 20 34 54 10 15
106 4 2
107 10 20 16 13
108 */

代码实现

原文地址:https://www.cnblogs.com/Darkpurple/p/9641535.html

时间: 2024-08-08 11:56:17

差分约束(例题整理)的相关文章

【转载】夜深人静写算法(四)——差分约束

[转载]夜深人静写算法(四) - 差分约束  目录     一.引例       1.一类不等式组的解   二.最短路       1.Dijkstra       2.图的存储       3.链式前向星       4.Dijkstra + 优先队列       5.Bellman-Ford       6.SPFA       7.Floyd-Warshall   三.差分约束        1.数形结合        2.三角不等式        3.解的存在性        4.最大值

浅谈差分约束问题

差分约束 差分约束是解决这样一类问题 给出\(n\)个形如\(x[j]-x[i]<=k\)的式子,求\(x[n]-x[1]\)的最大/最小值 思路 其实这个问题是挺套路的 我们把给出的式子变一下 \(x[j]-x[i]<=k\) \(x[j]<=x[i]+k\) 我们不难联想到图论中最短路的性质 假设\(d[x]\)表示\(1\)到\(x\)的最短路 那么对于任意一条边\((u,v)\) 有\(d[v]<=d[u]+k\)(k表示边权) 可能有些抽象,举个例子 经过计算不难得到三个

差分约束详解&amp;&amp;锣鼓SCOI2011糖果题解

差分约束系统: 如果一个系统由n个变量和m个约束条件组成,形成m个形如ai-aj≤k的不等式(i,j∈[1,n],k为常数),则称其为差分约束系统(system of difference constraints).亦即,差分约束系统是求解关于一组变量的特殊不等式组的方法. ——度娘. 然而并没有看懂.. 通俗来说,满足差分约束的条件是题目中给了你多个ai-aj<=(>=,<,>之类)的条件,要求同时满足这些条件并求极值的问题. 内么,怎么同时满足这些问题呢? 假如我们以这个东西为

UVa11478 - Halum(差分约束)

  Problem H Halum Time Limit : 3 seconds   You are given a directed graph G(V,E) with a set of vertices and edges. Each edge (i,j) that connects some vertex i to vertex j has an integer cost associated with that edge. Define the operation Halum(v, d)

差分约束-zoj-2770

Burn the Linked Camp It is well known that, in the period of The Three Empires, Liu Bei, the emperor of the Shu Empire, was defeated by Lu Xun, a general of the Wu Empire. The defeat was due to Liu Bei's wrong decision that he divided his large troop

poj1275--Cashier Employment(差分约束)

poj1275:题目链接 题目大意:给出24个数,第i个数表示在(i,i+1)小时内需要的人数,之后m个数,代表m个人前来应聘,其中每个人工作连续的8小时,给出应聘的人开始工作的时间,问最少需要雇佣的人数(可以在某个时间段中人数多于需要的人数) 差分约束: 1.题目需要求什么,就找什么之间的关系(二项式),比如,题目求雇佣的人数,就找出雇佣人数之间的关系,s[i]代表从0到i一共雇佣的人数 2.注意0的问题,和总和的问题. 3.判断的问题,不能存在环,和不能违背要求的值 又因为题中雇佣人数会形成

培训补坑(day5:最小生成树+负环判断+差分约束)

补坑补坑((╯‵□′)╯︵┻━┻) 内容真的多... 一个一个来吧. 首先是最小生成树. 先讲一下生成树的定义 生成树就是在一张图上选取一些边,使得整个图上所有的点都连通. 那么我们要求的最小生成树有两种算法可以求:1.prim算法,2.kruskal算法 我们先讲讲prim算法 prim算法有点像最短路中的dijstra,操作都几乎一样,原理就是从所有在队列中的点出发,找到最小的一条边,并把它连起来,这样子能够保证局部最优性. 在此我不讲这种算法,不过有兴趣的人可以去学一学. 我重点推出的是k

【差分约束】

这里有一大堆不等式或者等式形成的限制条件,题目通常会问问你是否存在合法方案或者让你求出合法方案.----------差分约束 ·前言:       一个概括的定义是,一些不等式组可以视作一个差分约束系统.简单而言,就是给出许多不等式,然后我们需要给每个未知数填上值,使它们满足所有给出的关于它们的不等式--这正是这类问题的求解过程.由于这一过程类似于最短路算法中的松弛操作,因此解决差分约束问题成了最短路算法的一个美妙而重要用途. ·一个例题        这里呢有一个长度为n的序列,现在给出m条信

POJ1201 Intervals (差分约束)

You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn. Write a program that: reads the number of intervals, their end points and integers c1, ..., cn from the standard input, computes the minimal size of a set Z of integers wh

差分约束讲解

差分约束讲解 --by ysy 1.前置知识 ????????因为差分约束是基于\(spfa\)的一种解不等式,或等式组的技巧,所以差分约束的前置知识就是\(spfa\)和对不等式的简单小变换. 2.讲解 ?????????因为差分约束只是一个技巧,所以在这里我先讲解技巧,之后再讲解例题. 建图技巧 ?????????我们将不等式组分为两种:\(A \le B+val\)以及\(A\ge B+val\). ?????????现在讨论第一种\(A\le B+val\),我们在建图时\(B \rig