您的位置:首页 > 其它

uva1486 Transportation

2017-02-22 17:23 323 查看
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();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  费用流