Hdu 3667 Transportation(最小费用流+思路)

本文介绍了一道名为HDU 3667的算法题目,通过分析平方数的性质,提出了利用最小费用最大流算法解决该问题的方法。文章详细解释了如何根据题目要求构建图,并给出了具体的实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3667

思路:平方关系,直接建图每次增广并不是最优。。。。

1^2=1,2^2=1+3,3^2=1+3+5,4^2=1+3+5+7.......

所以,对于每条边<x,y>,若流量为c,则在x与y之间连c条边,流量均为1,费用分别为a[i],3*a[i],5*a[i].........由于每次增广时流量相同时选择最小花费的边,若该边<x,y>流量为c,则总花费为a[x]+3*a[x]+5*a[x]+.........+(2*c-1)*a[x]=a[x]*c^2,符合题意。对新图求最小费用最大流即可,若最大流小于k,则无解。

#include<cmath>
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define debu
using namespace std;
typedef long long LL;
const int maxn=200;
const int INF=0x3f3f3f3f;
struct Edge
{
    int from,to,cap,flow,cost;
    Edge(int u,int v,int c,int f,int w):from(u),to(v),cap(c),flow(f),cost(w) {}
};
queue<int> q;
vector<Edge> edges;
vector<int> G[maxn];
int n,m,k,p[maxn],a[maxn];
int inq[maxn],d[maxn];
LL cost;
void init(int n)
{
    memset(p,0,sizeof(p));
    memset(a,0,sizeof(a));
    memset(d,0,sizeof(d));
    for(int i=0; i<=n+1; i++) G[i].clear();
    edges.clear();
}
void addedges(int from,int to,int cap,int cost)
{
    edges.push_back(Edge(from,to,cap,0,cost));
    edges.push_back(Edge(to,from,0,0,-cost));
    int m=edges.size();
    G[from].push_back(m-2);
    G[to].push_back(m-1);
}
bool BF(int s,int t,int& flow,LL& cost)
{
    while(!q.empty()) q.pop();
    for(int i=0; i<=n; i++) d[i]=INF;
    memset(inq,0,sizeof(inq));
    d[s]=0,inq[s]=1,p[s]=0;
    q.push(s);
    a[s]=INF;
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        inq[u]=0;
        for(int i=0; i<G[u].size(); i++)
        {
            Edge& e=edges[G[u][i]];
            if(e.cap>e.flow&&d[e.to]>d[u]+e.cost)
            {
                d[e.to]=d[u]+e.cost;
                p[e.to]=G[u][i];
                a[e.to]=min(a[u],e.cap-e.flow);
                if(!inq[e.to])
                {
                    q.push(e.to);
                    inq[e.to]=1;
                }
            }
        }
    }
    if(d[t]==INF) return 0;
    flow+=a[t];
    cost+=(LL)d[t]*(LL)(a[t]);
    //cout<<a[t]<<" "<<d[t]<<" "<<flow<<" "<<cost<<endl;
    for(int u=t; u!=s; u=edges[p[u]].from)
    {
        edges[p[u]].flow+=a[t];
        edges[p[u]^1].flow-=a[t];
    }
    return 1;
}
int mincostmaxflow(int s,int t,LL& cost)
{
    int flow=0;
    cost=0;
    while(BF(s,t,flow,cost));
    return flow;
}
int main()
{
#ifdef debug
    freopen("in.in","r",stdin);
#endif // debug
    ios::sync_with_stdio(0);
    while(cin>>n>>m>>k)
    {
        init(n);
        for(int i=0; i<m; i++)
        {
            int x,y,c,w,tmp=1;
            cin>>x>>y>>w>>c;
            //cout<<x<<" "<<y<<" "<<w<<" "<<c<<endl;
            for(int j=1; j<=c; j++)
            {
                addedges(x,y,1,w*tmp);
                tmp+=2;
            }
        }
        addedges(0,1,k,0);
        //cout<<"123"<<endl;
        int tmp=mincostmaxflow(0,n,cost);
        //cout<<tmp<<endl;
        if(tmp<k) cout<<-1<<endl;
        else cout<<cost<<endl;
        //cout<<"flag"<<endl;
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值