hdu3667Transportation费用流

Transportation

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


 

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

 

Recommend

lcy   |   We have carefully selected several similar problems for you:  3661 3664 3666 3669 3668 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>

#include<vector>
#include<queue>
#define inf 1e9
#define maxn 20005
using namespace std;
struct edge
{
    int from,to,cap,flow,cost;
    edge()
    {

    }
    edge(int f,int t,int ca,int fl,int co):from(f),to(t),cap(ca),flow(fl),cost(co){}
};
struct mcnf
{
    int n,m,s,t;
    vector<edge>edges;
    vector<int>g[maxn];
    bool inq[maxn];
    int d[maxn];
    int p[maxn];
    int a[maxn];
    void init(int n,int s,int t)
    {
        this->n=n;
        this->s=s;
        this->t=t;
        edges.clear();
        for(int i=0;i<n;i++)
        g[i].clear();
    }
    void addedge(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));
        m=edges.size();
        g[from].push_back(m-2);
        g[to].push_back(m-1);
    }
    bool bellford(int &flow,int &cost)
    {for(int i=0;i<n;i++)
    d[i]=inf;
    memset(inq,0,sizeof(inq));
    d[s]=0;
    a[s]=inf;
    inq[s]=true;
    p[s]=0;
    queue<int>q;
    q.push(s);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        inq[u]=false;
        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]=true;
                }
            }
        }

    }
    if(d[t]==inf)
    return false;
    flow+=a[t];
    cost+=a[t]*d[t];
    int u=t;
    while(u!=s)
    {
        edges[p[u]].flow+=a[t];
        edges[p[u]^1].flow-=a[t];
        u=edges[p[u]].from;

    }
    return true;

    }
    int mincost(int num)
    {
        int flow=0,cost=0;
        while(bellford(flow,cost));
        return flow==num?cost:-1;


    }
}MM;
int t;
int n,m,k;
int main()
{while(~scanf("%d%d%d",&n,&m,&k))
{int src=0,dst=n;
MM.init(n+1,src,dst);
MM.addedge(src,1,k,0);
    while(m--)
    {int u,v,a,c;
    scanf("%d%d%d%d",&u,&v,&a,&c);
    for(int i=1;i<=c;i++)
        MM.addedge(u,v,1,a*(2*i-1));

    }
    printf("%d\n",MM.mincost(k));
}
}

猜你喜欢

转载自blog.csdn.net/sdauguanweihong/article/details/85471616