uva1486 Transportation

137 篇文章 0 订阅

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 a i . If you want to carry x units of
goods along this road, you should pay a i x 2 dollars to hire guards
to protect your goods. And what’s worse, for each road i , there is an
upper bound C i , which means that you cannot transport more than C i
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 ( u i ; v i ; a i ;
C i ) , indicating there is a directed road from city u i to v i ,
whose coefficient is a i and upper bound is C i . ( 1 u i ; v i N
, 0 < a i 100 , C i 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 ’

因为 ci 很小,直接拆成容量为 1 ci条边,计算出每条边相应的权值。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int s=105,t=106,oo=0x3f3f3f3f,mod=107;
int fir[110],ne[200010],to[200010],w[200010],c[200010],
que[110],in[110],minw[110],len[110],fa[110],
n,m,k,num;
void add(int u,int v,int x,int y)
{
    num++;
    ne[num*2]=fir[u];
    fir[u]=num*2;
    to[num*2]=v;
    w[num*2]=x;
    c[num*2]=y;
    ne[num*2+1]=fir[v];
    fir[v]=num*2+1;
    to[num*2+1]=u;
    w[num*2+1]=0;
    c[num*2+1]=-y;
}
void init()
{
    int u,v,a,c;
    memset(fir,0,sizeof(fir));
    num=0;
    while (m--)
    {
        scanf("%d%d%d%d",&u,&v,&a,&c);
        for (int i=1;i<=c;i++)
            add(u,v,1,a*i*i-a*(i-1)*(i-1));
    }
    add(s,1,k,0);
    add(n,t,k,0);
}
void solve()
{
    int u,v,hd,tl,ans=0;
    while (1)
    {
        hd=0,tl=1;
        in[s]=1;
        que[0]=s;
        memset(minw,0,sizeof(minw));
        minw[s]=oo;
        memset(len,0x3f,sizeof(len));
        len[s]=0;
        while (hd!=tl)
        {
            u=que[hd++];
            hd%=mod;
            for (int i=fir[u];i;i=ne[i])
                if (w[i]&&len[v=to[i]]>len[u]+c[i])
                {
                    len[v]=len[u]+c[i];
                    minw[v]=min(minw[u],w[i]);
                    fa[v]=i;
                    if (!in[v])
                    {
                        in[v]=1;
                        que[tl++]=v;
                        tl%=mod;
                    }
                }
            in[u]=0;
        }
        if (!minw[t]) break;
        for (int i=fa[t];i;i=fa[to[i^1]])
        {
            w[i]-=minw[t];
            w[i^1]+=minw[t];
        }
        ans+=minw[t]*len[t];
    }
    if (w[fir[s]]) printf("-1\n");
    else printf("%d\n",ans);
}
int main()
{
    while (scanf("%d%d%d",&n,&m,&k)==3)
    {
        init();
        solve();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值