HDU-3440 House Man

news/2025/3/15 1:24:14/

本来是一道普通的差分约束题,结果特么的写完连样例都过不了。。。

后来才在差分约束的百度百科中现这段话:

1.如果将源点到各点的距离初始化为0,最终求出的最短路满足 它们之间相互最接近了;

2.如果将源点到各点的距离初始化为INF(无穷大),其中之1为0,最终求出的最短路满足 它们与该点之间相互差值最大

呵呵呵原来我是错在Dist的初始值这里啊。。。

于是第二次写……把起点的Dist设为0,其余都设为+∞

可是依旧过不了样例……

终于发现这句话的问题……差值最大……也就是说当终点在起点左边的时候我们可以知道Dist[t]-Dist[s]<0,Answer为Dist[s]-Dist[t]

然后差值最大是保证Dist[t]-Dist[s]最大,也就是保证Dist[s]-Dist[t]最小,那么Answer……

于是改成让Dist[min(s,t)]=0

接着又因为几次队列的初始化问题WA了几次。。。

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <fstream>
#include <iostream>
#include <deque>#define rep(i, l, r) for(int i = l; i <= r; i++)
#define down(i, l, r) for(int i = l; i >= r; i--)
#define N 1009
#define MAX 1<<30using namespace std;
inline int read()
{int x=0, f=1; char ch=getchar();while (ch<'0' || ch>'9') { if (ch=='-') f=-1; ch=getchar(); }while (ch>='0' && ch<='9') { x=x*10+ch-'0'; ch=getchar(); }return x*f;
}struct edge{int y, z, n;} e[N*5]; int fir[N], en;
struct node{int h, id;} s[N];
int n, k, d[N], c[N], ans;
deque <int> q;
bool b[N];
bool cmp(node a, node b) { return a.h<b.h; }void Add(int x, int y, int z)
{en++, e[en].y = y, e[en].z = z, e[en].n = fir[x], fir[x] = en;
}int main()
{int t=read(), tt=t;while (t--){rep(i, 1, n) fir[i]=0; en=0; ans=0;if (tt-t == 41) en = 0;n=read(), k=read();rep(i, 1, n) s[i].h=read(), s[i].id=i;sort(s+1, s+1+n, cmp);rep(i, 1, n-1) s[i].id>s[i+1].id ? Add(s[i+1].id, s[i].id, k) : Add(s[i].id, s[i+1].id, k);rep(i, 1, n-1) Add(i+1, i, -1);rep(i, 1, n) b[i]=0, c[i]=0, d[i]=MAX;d[min(s[n].id, s[1].id)] = 0; q.push_front(min(s[n].id, s[1].id));while (!q.empty()){int x=q.front(), o=fir[x], y=e[o].y; q.pop_front(); b[x]=0;if (c[x] > n) { ans=-1; break; }while (o){if (d[y] > d[x]+e[o].z) {d[y] = d[x]+e[o].z;if (!b[y]) b[y]=1, c[y]++, !q.empty()&&d[y]<=d[q.front()] ? q.push_front(y) : q.push_back(y);}o=e[o].n, y=e[o].y;}}while (!q.empty()) q.pop_front();if (ans!=-1) ans=abs(d[s[1].id]-d[s[n].id]);printf("Case %d: %d\n", tt-t, ans);}return 0; 
}

  

转载于:https://www.cnblogs.com/NanoApe/p/4333104.html


http://www.ppmy.cn/news/284573.html

相关文章

[hdu3440]House Man

House Man 题解 很经典的一道差分约束的题目。 我们现在有两个约束条件&#xff1a; 相邻两点的距离大于1。两个高度紧挨着的点的距离不超过d。(注意&#xff0c;两个高度相邻的点之间如果有一个更高的他会直接跳过这个更高的&#xff0c;一脸懵*) 于是我们就得到了两个约…

bzoj3440:传球游戏

Pre 真的毒瘤题&#xff0c;昨天晚上以为是一道简单的图论题&#xff0c;今天中午没有调出来&#xff0c;晚上我选择看题解发现漏了一种情况。 于是 就花了一晚上调程序。 现在时间\(21:31\) Solution 拆点是很容易想到的。 拓扑(\(Tarjan\)应该也可以,没试过)找环。 之后分类。…

hdu 3440(差分约束系统)

hdu 3440 &#xff08;1&#xff09;题意&#xff1a; 一个人从高度较低的房子跳到高度较高的房子&#xff0c;他可以跳跃的水平最远距离为D&#xff0c; 他只能从左向右跳&#xff0c;房子的编号是从1~n&#xff0c;从左向右编号。 两个房子之间只要水平距离小于D&#xff…

hdu 3440差分约束

http://acm.hdu.edu.cn/showproblem.php?pid3440 Problem Description In Fuzhou, there is a crazy super man. He can’t fly, but he could jump from housetop to housetop. Today he plans to use N houses to hone his house hopping skills. He will start at the sh…

squid web代理服务器安装与配置

目录 简介 squid安装 squid基本用户认证配置 客户端代理配置 简介 什么是squid呢&#xff1f;来看一下官方介绍&#xff1a; yum info squid.x86_64 Description : Squid is a high-performance proxy caching server for Web clients, : supporting FTP, g…

19年1月底得一些装机心得(一)

19年1月底得一些装机心得&#xff08;一&#xff09; 选购的配件购买的东西正在向我飞奔而来 前几天在B站看见UP大佬极客湾用1200装了一台性能爆炸&#xff08;物理意义上的&#xff09;的二手机子&#xff0c;CPU选用的初代至强X3440&#xff0c;显卡用的AMD的R9 290&#xff…

3440: 传球游戏

题意&#xff1a; 当被传到球之后&#xff0c;不同的人会做出不同的动作。 第1类人&#xff0c;顺着传来的方向传给下一个人。 第2类人&#xff0c;逆着传来的方向传给上一个人。 第3类人&#xff0c;顺着传来的方向传给下面第二个人。 第4类人&#xff0c;逆着传来的方向传给…

HDU 3440

比较好的一道差分约束的题目。 差分约束里面&#xff0c;我觉得最经典的两句话就是 按最短路求的值达到可能的最大&#xff0c;按最长路求的值达到可能的最小。 建图&#xff0c;比较简单&#xff0c;每个点dis[i1]>dis[i]1 所以i1->i 连条-1的边。 其次 排序 高度数组…