HDU - 6767 New Equipments


传送门:HDU - 6767

思路分析

实际上只需要找对每个点贡献最小的n个设备就行了,因为是二次函数形式,所以找对称轴附近的n个点就行了,每次spfa得到的就是答案

AC代码

#include <bits/stdc++.h>
#define fi first
#define se second
#define ll long long
#define pb push_back
#define mp make_pair
#define fun function
#define sz(x) (x).size()
#define lowbit(x) (x)&(-x)
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))

namespace FastIO {
#define BUF_SIZE 100000
#define OUT_SIZE 100000
    bool IOerror=0;
    inline char nc() {
        static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
        if(p1==pend) {
            p1=buf;
            pend=buf+fread(buf,1,BUF_SIZE,stdin);
            if(pend==p1) {
                IOerror=1;
                return -1;
            }
        }
        return *p1++;
    }
    inline bool blank(char ch) {
        return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';
    }
    template<class T> inline bool read(T &x) {
        bool sign=0;
        char ch=nc();
        x=0;
        for(; blank(ch); ch=nc());
        if(IOerror)return false;
        if(ch=='-')sign=1,ch=nc();
        for(; ch>='0'&&ch<='9'; ch=nc())x=x*10+ch-'0';
        if(sign)x=-x;
        return true;
    }
    template<class T,class... U>bool read(T& h,U&... t) {
        return read(h)&&read(t...);
    }
#undef OUT_SIZE
#undef BUF_SIZE
};
using namespace std;
using namespace FastIO;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

#define int long long
const int INF = 0x3f3f3f3f;
const int N=3e4+10;
const int M=1e6+10;

struct node {
    int v,flow,w,next;
} e[M];

int head[N],cnt;
int n,m,s,t,k,maxflow,mincost,path[N],pre[N];
int dis[N],vis[N];
map<int,int>MP;
int tot;

inline void ade(int u,int v,int f,int w) {
    e[++cnt].v=v;
    e[cnt].flow=f;
    e[cnt].w=w;
    e[cnt].next=head[u];
    head[u]=cnt;

}

inline void add(int u,int v,int f,int w) {
    ade(u,v,f,w);
    ade(v,u,0,-w);
}

inline bool spfa() {
    mem(dis,INF);
    mem(vis,0);
    mem(pre,-1);
    dis[s]=0;
    vis[s]=1;
    queue<int>q;
    q.push(s);
    while(!q.empty()) {
        int u=q.front();
        vis[u]=0;
        q.pop();
        for(int i=head[u]; i; i=e[i].next) {
            int v=e[i].v;
            int w=e[i].w;
            if(e[i].flow && dis[v]>dis[u]+w) {
                dis[v]=dis[u]+w;
                pre[v]=u;
                path[v]=i;
                if(!vis[v]) {
                    q.push(v);
                    vis[v]=1;
                }
            }
        }
    }
    return pre[t]!=-1;
}

vector<int>ans;
inline void EK() {
    while(spfa()) {
        int mi=INF;
        for(int i=t; i!=s; i=pre[i])
            mi=min(mi,e[path[i]].flow);
        for(int i=t; i!=s; i=pre[i]) {
            e[path[i]].flow-=mi;
            e[path[i]^1].flow+=mi;
        }
        maxflow+=mi;
        mincost+=dis[t]*mi;
        ans.pb(mincost);
    }
}

inline get(int x){
    if(!MP[x]) MP[x]=++tot;
    return MP[x];
}


signed main() {

#ifdef xiaofan
    freopen("1.in","r",stdin);
    freopen("1.out","w",stdout);
#endif

    int T;
    read(T);
    while(T--){
        read(n,m);
        cnt=1;
        tot=n+1;
        mem(head,0);
        MP.clear();
        s=N-1;
        t=N-2;
        mincost = maxflow = 0;
        for(int i=1;i<=n;i++){
            int a,b,c;
            read(a,b,c);
            int mid = (-b)/(2*a);
            int now=max(mid-n-2,1LL);
            for(int j=1;j<=2*n+2;j++){
                int x=now+j-1;
                add(i,get(x),1,a*x*x+b*x+c);
            }    
            add(s,i,1,0);
        }
        ans.clear();
        for(auto i:MP) add(i.se,t,1,0);
        EK();
        for(int i=0;i<n;i++) printf("%lld%c",ans[i],i==n-1?'\n':' ');
    } 




    return 0;
}


文章作者: 小凡
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 小凡 !
评论
  目录
隐藏
{% if theme.sakura.enable %}{% endif %}