hdu3667 Transportation

本文针对HDU3667运输问题进行了详细解析,这是一个涉及多个城市及带有成本约束的路径寻找问题。文章介绍了一种通过拆边处理非线性费用流的方法,并给出了具体的实现代码。

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

http://www.elijahqi.win/2017/12/17/hdu3667-transportation/
Problem Description
There are N cities, and M directed roads connecting them. Now you want to transport K units of goods from city 1 to city N. There are many robbers on the road, so you must be very careful. The more goods you carry, the more dangerous it is. To be more specific, for each road i, there is a coefficient ai. If you want to carry x units of goods along this road, you should pay ai * x2 dollars to hire guards to protect your goods. And what’s worse, for each road i, there is an upper bound Ci, which means that you cannot transport more than Ci units of goods along this road. Please note you can only carry integral unit of goods along each road.
You should find out the minimum cost to transport all the goods safely.

Input
There are several test cases. The first line of each case contains three integers, N, M and K. (1 <= N <= 100, 1 <= M <= 5000, 0 <= K <= 100). Then M lines followed, each contains four integers (ui, vi, ai, Ci), indicating there is a directed road from city ui to vi, whose coefficient is ai and upper bound is Ci. (1 <= ui, vi <= N, 0 < ai <= 100, Ci <= 5)

Output

Output one line for each test case, indicating the minimum cost. If it is impossible to transport all the K units of goods, output -1.

Sample Input
2 1 2 1 2 1 2 2 1 2 1 2 1 1 2 2 2 1 2 1 2 1 2 2 2

Sample Output
4 -1 3

Source
2010 Asia Regional Harbin

一开始我天真的以为这就是一个裸的费用流 但是zhx(orz)把我秒杀了 这费用明明是平方啊 那怎么办呢 我一直想的都是拆点 然而却很麻烦 还是zhx大佬强啊 直接拆边 给每个边的容量都给1 然后费用就是 这个权值-上个权值他们中间费用的差值啊 %%%

#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define inf 0x3f3f3f3f
#define N 110
using namespace std;
inline int read(){
    int x=0;char ch=getchar();
    while(ch<'0'||ch>'9') ch=getchar();
    while(ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=getchar();}
    return x;
} 
struct node{
    int y,z,next,c;
}data[55000];bool flag[N];
int n,m,k,num,h[N],f[N],path[N],pre[N];
inline void insert1(int x,int y,int z,int c){
    data[++num].y=y;data[num].next=h[x];h[x]=num;data[num].c=c;data[num].z=z;
    data[++num].y=x;data[num].next=h[y];h[y]=num;data[num].c=-c;data[num].z=0;
}
inline bool spfa(){
    memset(f,0x3f,sizeof(f));memset(flag,0,sizeof(flag));queue<int>q;memset(pre,-1,sizeof(pre));q.push(0);flag[0]=1;f[0]=0;
    while(!q.empty()){
        int x=q.front();q.pop();flag[x]=0;
        for (int i=h[x];i;i=data[i].next){
            int y=data[i].y,z=data[i].z,c=data[i].c;
            if (f[x]+c<f[y]&&z){
                f[y]=f[x]+c;pre[y]=x;path[y]=i;
                if (!flag[y]) flag[y]=1,q.push(y);
            }
        }
    }if (pre[n]==-1) return 0;else return 1;
}
int main(){
    freopen("hdu3667.in","r",stdin);
    while(~scanf("%d%d%d",&n,&m,&k)){
        memset(h,0,sizeof(h));num=1;
        for (int i=1;i<=m;++i){
            int x=read(),y=read(),c=read(),z=read();
            for (int j=1;j<=z;++j) insert1(x,y,1,(2*j-1)*c);
        }int ans=0,ans1=0;insert1(0,1,k,0);
        while(spfa()){
            int minn=inf,now=n;
            while(now) minn=min(minn,data[path[now]].z),now=pre[now];ans+=minn;now=n;
            while(now){ans1+=data[path[now]].c*minn;data[path[now]].z-=minn;data[path[now]^1].z+=minn;now=pre[now];}
        }
        if(ans!=k) printf("-1\n");else printf("%d\n",ans1);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值