HDU 3667 Transportation 费用流(拆边)

来源:互联网 发布:单片机连接共阴数码管 编辑:程序博客网 时间:2024/06/05 00:11

点击打开链接

 

Transportation

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1722    Accepted Submission(s): 684


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 21 2 1 22 1 21 2 1 12 2 21 2 1 21 2 2 2
 


 

Sample Output
4-13
 


 

Source
2010 Asia Regional Harbin
 


 

Recommend
lcy
 

 

 

题意是说N个村庄,M条路,你需要运送K单位的物品从1号村庄到第N号村庄。路与路之间的费用为a*x*x其中x为流量,a为参数,求最小费用。

要用拆变法,因为求的是最小费用流,如果这条弧的流量是1,走的肯定是cost=1的那条弧,如果流量是2,肯定走的是cost=1和cost=3的那两条弧;如果流量是3,走的肯定是cost为1,3,5那三条。如图所示:

 

 

 

#include<iostream>#include<algorithm>#include<cstring>#include<queue>#include<cstdio>using namespace std;const int MAXN=105;const int inf=1<<29;int pre[MAXN];          // pre[v] = k:在增广路上,到达点v的边的编号为kint dis[MAXN];          // dis[u] = d:从起点s到点u的路径长为dint vis[MAXN];         // inq[u]:点u是否在队列中int path[MAXN];int head[MAXN];int NE,tot,ans,max_flow,map[666][666];int n,m,k;struct node{    int u,v,cap,cost,next;} Edge[100007];void addEdge(int u,int v,int cap,int cost){    Edge[NE].u=u;    Edge[NE].v=v;    Edge[NE].cap=cap;    Edge[NE].cost=cost;    Edge[NE].next=head[u];    head[u]=NE++;    Edge[NE].v=u;    Edge[NE].u=v;    Edge[NE].cap=0;    Edge[NE].cost=-cost;    Edge[NE].next=head[v];    head[v]=NE++;}int SPFA(int s,int t)                   //  源点为0,汇点为sink。{    int i;    for(i=s;i<=t;i++) dis[i]=inf;    memset(vis,0,sizeof(vis));    memset(pre,-1,sizeof(pre));    dis[s] = 0;    queue<int>q;    q.push(s);    vis[s] =1; while(!q.empty())        //  这里最好用队列,有广搜的意思,堆栈像深搜。    {        int u =q.front();        q.pop();        for(i=head[u]; i!=-1;i=Edge[i].next)        {            int v=Edge[i].v;            if(Edge[i].cap >0&& dis[v]>dis[u]+Edge[i].cost)            {                dis[v] = dis[u] + Edge[i].cost;                pre[v] = u;                path[v]=i;                if(!vis[v])                {                    vis[v] =1;                    q.push(v);                }            }        }        vis[u] =0;    }    if(pre[t]==-1)        return 0;    return 1;}void end(int s,int t){    int u, sum = inf;    for(u=t; u!=s; u=pre[u])    {        sum = min(sum,Edge[path[u]].cap);    }    max_flow+=sum;                          //记录最大流    for(u = t; u != s; u=pre[u])    {        Edge[path[u]].cap -= sum;        Edge[path[u]^1].cap += sum;        ans += sum*Edge[path[u]].cost;     //  cost记录的为单位流量费用,必须得乘以流量。    }}int main(){    int i,j,s,t;    while(scanf("%d%d%d",&n,&m,&k)!=EOF)    {        memset(head,-1,sizeof(head));        NE=ans=max_flow=s=0;        int a,b,c,d;        while(m--)        {            scanf("%d%d%d%d",&a,&b,&c,&d);            for(int i=1;i<=d;i++)            {                addEdge(a,b,1,c*(2*i-1));            }        }        addEdge(0,1,k,0);        s=0;t=n;        while(SPFA(s,t))        {            end(s,t);        }        if(max_flow<k)printf("-1\n");        else        printf("%d\n",ans);    }    return 0;}


 

 

原创粉丝点击