HDU 3440 差分约束系统

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3440
题意:在水平坐标轴上给n座房子,房子与相邻房子距离至少为1,房子的高度各不相同, 从最低的房子开始, 每次跳到更高的房子, 跳n-1次最能跳到最高的房子了,但是每次跳跃的距离不能超过d。
目标是:将这些房子在一维的方向上重新摆放(房子间的相对位置不变) , 使得最矮的房子和最高的房子水平距离最大。(房子不重叠)

规模:样例数(T<=500) 房子数(1 ≤ N ≤ 1000) 跳跃的最大长度(1 ≤ D ≤1000000)
类型:差分约束系统

分析:这里有两个约束条件:
1.房子位置不变(不重叠):x(i)+1>=x(i+1)
2.相邻高度的房子距离最大为D
第二个条件建图有些麻烦,首先要对高度进行sort(),要完成这个操作最好用结构体存储高度。排序后的顶点之间相差D,建边就好。
但有一个很重要的问题,因为对房子相对位置的限制,所以建边【from,to,cost】要以(小id点,大id点,D)为准。就是说,不要直接根据高度排序后的id顺序建立(from,to)。同样的,源点和终点的id,也要根据id大小进行交换才行。

时间复杂度&&优化:
代码:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<algorithm>
#include<iostream>

using namespace std;

const int MAX_N = 1005;
const int MAX_D = 1000005;
const int inf = 1000000007;
const int mod = 1000000007;

int n,D;

struct edge{
    int from,to,cost;
};
vector<edge> es;

struct vhigh{///结构体存储高度
    int V;
    int high;
};
vhigh P[MAX_N];

bool cmp(vhigh a,vhigh b){
    return a.high<b.high;
}

int d[MAX_N];
void shortest_path(int s){///最短路是用Bellman-Ford算法
    for(int i=0;i<=n;i++){
        d[i]=inf;
    }d[s]=0;
    while(true){
        bool update=false;
        for(int i=0;i<es.size();i++){
            edge e=es[i];
//            cout<<e.from<<" "<<e.to<<" "<<e.cost<<endl;
            if(d[e.from]!=inf&&d[e.to]>d[e.from]+e.cost){
                d[e.to]=d[e.from]+e.cost;
                update=true;
            }
        }
        if(!update)break;
    }
}

bool find_negative_loop(){///判断是否是负环,负环返回true
    memset(d,0,sizeof(d));
    for(int i=0;i<n;i++){
        for(int j=0;j<es.size();j++){
            edge e=es[j];
            if(d[e.to]>d[e.from]+e.cost){
                d[e.to]=d[e.from]+e.cost;
                if(i==n-1)return true;
            }
        }
    }
    return false;
}

int main()
{
    int T;
    cin>>T;
    for(int z=1;z<=T;z++){
        cin>>n>>D;
        while(!es.empty()){es.pop_back();}///初始化数组
        for(int i=1;i<=n;i++){
            P[i].V=i;
            cin>>P[i].high;
        }
        for(int i=1;i<n;i++){///根据“条件1”建边
            edge tmp;
            tmp.from=i+1;
            tmp.to=i;
            tmp.cost=-1;
            es.push_back(tmp);
        }

        ///根据条件2建边
        sort(P+1,P+n+1,cmp);
        for(int i=1;i<n;i++){
            edge tmp;
            tmp.from=min(P[i].V,P[i+1].V);
            tmp.to=max(P[i+1].V,P[i].V);
            tmp.cost=D;
            es.push_back(tmp);
        }

        ///存在负圈则无解
        if(find_negative_loop()){
            cout<<"Case "<<z<<": "<<-1<<endl;
            continue;
        }

        ///根据id大小,交换“源点,终点”顺序
        int st=min(P[1].V,P[n].V);
        int ed=max(P[1].V,P[n].V);
        shortest_path(st);
        cout<<"Case "<<z<<": "<<d[ed]<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值